[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / abcotvs.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import (
9     dict_get,
10     int_or_none,
11     try_get,
12 )
13
14
15 class ABCOTVSIE(InfoExtractor):
16     IE_NAME = 'abcotvs'
17     IE_DESC = 'ABC Owned Television Stations'
18     _VALID_URL = r'https?://(?P<site>abc(?:7(?:news|ny|chicago)?|11|13|30)|6abc)\.com(?:(?:/[^/]+)*/(?P<display_id>[^/]+))?/(?P<id>\d+)'
19     _TESTS = [
20         {
21             'url': 'http://abc7news.com/entertainment/east-bay-museum-celebrates-vintage-synthesizers/472581/',
22             'info_dict': {
23                 'id': '472548',
24                 'display_id': 'east-bay-museum-celebrates-vintage-synthesizers',
25                 'ext': 'mp4',
26                 'title': 'East Bay museum celebrates synthesized music',
27                 'description': 'md5:24ed2bd527096ec2a5c67b9d5a9005f3',
28                 'thumbnail': r're:^https?://.*\.jpg$',
29                 'timestamp': 1421118520,
30                 'upload_date': '20150113',
31             },
32             'params': {
33                 # m3u8 download
34                 'skip_download': True,
35             },
36         },
37         {
38             'url': 'http://abc7news.com/472581',
39             'only_matching': True,
40         },
41         {
42             'url': 'https://6abc.com/man-75-killed-after-being-struck-by-vehicle-in-chester/5725182/',
43             'only_matching': True,
44         },
45     ]
46     _SITE_MAP = {
47         '6abc': 'wpvi',
48         'abc11': 'wtvd',
49         'abc13': 'ktrk',
50         'abc30': 'kfsn',
51         'abc7': 'kabc',
52         'abc7chicago': 'wls',
53         'abc7news': 'kgo',
54         'abc7ny': 'wabc',
55     }
56
57     def _real_extract(self, url):
58         site, display_id, video_id = re.match(self._VALID_URL, url).groups()
59         display_id = display_id or video_id
60         station = self._SITE_MAP[site]
61
62         data = self._download_json(
63             'https://api.abcotvs.com/v2/content', display_id, query={
64                 'id': video_id,
65                 'key': 'otv.web.%s.story' % station,
66                 'station': station,
67             })['data']
68         video = try_get(data, lambda x: x['featuredMedia']['video'], dict) or data
69         video_id = compat_str(dict_get(video, ('id', 'publishedKey'), video_id))
70         title = video.get('title') or video['linkText']
71
72         formats = []
73         m3u8_url = video.get('m3u8')
74         if m3u8_url:
75             formats = self._extract_m3u8_formats(
76                 video['m3u8'].split('?')[0], display_id, 'mp4', m3u8_id='hls', fatal=False)
77         mp4_url = video.get('mp4')
78         if mp4_url:
79             formats.append({
80                 'abr': 128,
81                 'format_id': 'https',
82                 'height': 360,
83                 'url': mp4_url,
84                 'width': 640,
85             })
86         self._sort_formats(formats)
87
88         image = video.get('image') or {}
89
90         return {
91             'id': video_id,
92             'display_id': display_id,
93             'title': title,
94             'description': dict_get(video, ('description', 'caption'), try_get(video, lambda x: x['meta']['description'])),
95             'thumbnail': dict_get(image, ('source', 'dynamicSource')),
96             'timestamp': int_or_none(video.get('date')),
97             'duration': int_or_none(video.get('length')),
98             'formats': formats,
99         }
100
101
102 class ABCOTVSClipsIE(InfoExtractor):
103     IE_NAME = 'abcotvs:clips'
104     _VALID_URL = r'https?://clips\.abcotvs\.com/(?:[^/]+/)*video/(?P<id>\d+)'
105     _TEST = {
106         'url': 'https://clips.abcotvs.com/kabc/video/214814',
107         'info_dict': {
108             'id': '214814',
109             'ext': 'mp4',
110             'title': 'SpaceX launch pad explosion destroys rocket, satellite',
111             'description': 'md5:9f186e5ad8f490f65409965ee9c7be1b',
112             'upload_date': '20160901',
113             'timestamp': 1472756695,
114         },
115         'params': {
116             # m3u8 download
117             'skip_download': True,
118         },
119     }
120
121     def _real_extract(self, url):
122         video_id = self._match_id(url)
123         video_data = self._download_json('https://clips.abcotvs.com/vogo/video/getByIds?ids=' + video_id, video_id)['results'][0]
124         title = video_data['title']
125         formats = self._extract_m3u8_formats(
126             video_data['videoURL'].split('?')[0], video_id, 'mp4')
127         self._sort_formats(formats)
128
129         return {
130             'id': video_id,
131             'title': title,
132             'description': video_data.get('description'),
133             'thumbnail': video_data.get('thumbnailURL'),
134             'duration': int_or_none(video_data.get('duration')),
135             'timestamp': int_or_none(video_data.get('pubDate')),
136             'formats': formats,
137         }