Merge branch 'master' of https://github.com/DarkstaIkers/youtube-dl into DarkstaIkers...
[youtube-dl] / youtube_dl / extractor / go.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     int_or_none,
9     determine_ext,
10     parse_age_limit,
11     urlencode_postdata,
12     ExtractorError,
13 )
14
15
16 class GoIE(InfoExtractor):
17     _BRANDS = {
18         'abc': '001',
19         'freeform': '002',
20         'watchdisneychannel': '004',
21         'watchdisneyjunior': '008',
22         'watchdisneyxd': '009',
23     }
24     _VALID_URL = r'https?://(?:(?P<sub_domain>%s)\.)?go\.com/(?:[^/]+/)*(?:vdka(?P<id>\w+)|season-\d+/\d+-(?P<display_id>[^/?#]+))' % '|'.join(_BRANDS.keys())
25     _TESTS = [{
26         'url': 'http://abc.go.com/shows/castle/video/most-recent/vdka0_g86w5onx',
27         'info_dict': {
28             'id': '0_g86w5onx',
29             'ext': 'mp4',
30             'title': 'Sneak Peek: Language Arts',
31             'description': 'md5:7dcdab3b2d17e5217c953256af964e9c',
32         },
33         'params': {
34             # m3u8 download
35             'skip_download': True,
36         },
37     }, {
38         'url': 'http://abc.go.com/shows/after-paradise/video/most-recent/vdka3335601',
39         'only_matching': True,
40     }]
41
42     def _real_extract(self, url):
43         sub_domain, video_id, display_id = re.match(self._VALID_URL, url).groups()
44         if not video_id:
45             webpage = self._download_webpage(url, display_id)
46             video_id = self._search_regex(r'data-video-id=["\']VDKA(\w+)', webpage, 'video id')
47         brand = self._BRANDS[sub_domain]
48         video_data = self._download_json(
49             'http://api.contents.watchabc.go.com/vp2/ws/contents/3000/videos/%s/001/-1/-1/-1/%s/-1/-1.json' % (brand, video_id),
50             video_id)['video'][0]
51         title = video_data['title']
52
53         formats = []
54         for asset in video_data.get('assets', {}).get('asset', []):
55             asset_url = asset.get('value')
56             if not asset_url:
57                 continue
58             format_id = asset.get('format')
59             ext = determine_ext(asset_url)
60             if ext == 'm3u8':
61                 video_type = video_data.get('type')
62                 if video_type == 'lf':
63                     entitlement = self._download_json(
64                         'https://api.entitlement.watchabc.go.com/vp2/ws-secure/entitlement/2020/authorize.json',
65                         video_id, data=urlencode_postdata({
66                             'video_id': video_data['id'],
67                             'video_type': video_type,
68                             'brand': brand,
69                             'device': '001',
70                         }))
71                     errors = entitlement.get('errors', {}).get('errors', [])
72                     if errors:
73                         error_message = ', '.join([error['message'] for error in errors])
74                         raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message), expected=True)
75                     asset_url += '?' + entitlement['uplynkData']['sessionKey']
76                 formats.extend(self._extract_m3u8_formats(
77                     asset_url, video_id, 'mp4', m3u8_id=format_id or 'hls', fatal=False))
78             else:
79                 formats.append({
80                     'format_id': format_id,
81                     'url': asset_url,
82                     'ext': ext,
83                 })
84         self._sort_formats(formats)
85
86         subtitles = {}
87         for cc in video_data.get('closedcaption', {}).get('src', []):
88             cc_url = cc.get('value')
89             if not cc_url:
90                 continue
91             ext = determine_ext(cc_url)
92             if ext == 'xml':
93                 ext = 'ttml'
94             subtitles.setdefault(cc.get('lang'), []).append({
95                 'url': cc_url,
96                 'ext': ext,
97             })
98
99         thumbnails = []
100         for thumbnail in video_data.get('thumbnails', {}).get('thumbnail', []):
101             thumbnail_url = thumbnail.get('value')
102             if not thumbnail_url:
103                 continue
104             thumbnails.append({
105                 'url': thumbnail_url,
106                 'width': int_or_none(thumbnail.get('width')),
107                 'height': int_or_none(thumbnail.get('height')),
108             })
109
110         return {
111             'id': video_id,
112             'title': title,
113             'description': video_data.get('longdescription') or video_data.get('description'),
114             'duration': int_or_none(video_data.get('duration', {}).get('value'), 1000),
115             'age_limit': parse_age_limit(video_data.get('tvrating', {}).get('rating')),
116             'episode_number': int_or_none(video_data.get('episodenumber')),
117             'series': video_data.get('show', {}).get('title'),
118             'season_number': int_or_none(video_data.get('season', {}).get('num')),
119             'thumbnails': thumbnails,
120             'formats': formats,
121             'subtitles': subtitles,
122         }