[go] Relax video id regex (closes #11937)
[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(
47                 # There may be inner quotes, e.g. data-video-id="'VDKA3609139'"
48                 # from http://freeform.go.com/shows/shadowhunters/episodes/season-2/1-this-guilty-blood
49                 r'data-video-id=["\']*VDKA(\w+)', webpage, 'video id')
50         brand = self._BRANDS[sub_domain]
51         video_data = self._download_json(
52             'http://api.contents.watchabc.go.com/vp2/ws/contents/3000/videos/%s/001/-1/-1/-1/%s/-1/-1.json' % (brand, video_id),
53             video_id)['video'][0]
54         title = video_data['title']
55
56         formats = []
57         for asset in video_data.get('assets', {}).get('asset', []):
58             asset_url = asset.get('value')
59             if not asset_url:
60                 continue
61             format_id = asset.get('format')
62             ext = determine_ext(asset_url)
63             if ext == 'm3u8':
64                 video_type = video_data.get('type')
65                 if video_type == 'lf':
66                     entitlement = self._download_json(
67                         'https://api.entitlement.watchabc.go.com/vp2/ws-secure/entitlement/2020/authorize.json',
68                         video_id, data=urlencode_postdata({
69                             'video_id': video_data['id'],
70                             'video_type': video_type,
71                             'brand': brand,
72                             'device': '001',
73                         }))
74                     errors = entitlement.get('errors', {}).get('errors', [])
75                     if errors:
76                         error_message = ', '.join([error['message'] for error in errors])
77                         raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message), expected=True)
78                     asset_url += '?' + entitlement['uplynkData']['sessionKey']
79                 formats.extend(self._extract_m3u8_formats(
80                     asset_url, video_id, 'mp4', m3u8_id=format_id or 'hls', fatal=False))
81             else:
82                 formats.append({
83                     'format_id': format_id,
84                     'url': asset_url,
85                     'ext': ext,
86                 })
87         self._sort_formats(formats)
88
89         subtitles = {}
90         for cc in video_data.get('closedcaption', {}).get('src', []):
91             cc_url = cc.get('value')
92             if not cc_url:
93                 continue
94             ext = determine_ext(cc_url)
95             if ext == 'xml':
96                 ext = 'ttml'
97             subtitles.setdefault(cc.get('lang'), []).append({
98                 'url': cc_url,
99                 'ext': ext,
100             })
101
102         thumbnails = []
103         for thumbnail in video_data.get('thumbnails', {}).get('thumbnail', []):
104             thumbnail_url = thumbnail.get('value')
105             if not thumbnail_url:
106                 continue
107             thumbnails.append({
108                 'url': thumbnail_url,
109                 'width': int_or_none(thumbnail.get('width')),
110                 'height': int_or_none(thumbnail.get('height')),
111             })
112
113         return {
114             'id': video_id,
115             'title': title,
116             'description': video_data.get('longdescription') or video_data.get('description'),
117             'duration': int_or_none(video_data.get('duration', {}).get('value'), 1000),
118             'age_limit': parse_age_limit(video_data.get('tvrating', {}).get('rating')),
119             'episode_number': int_or_none(video_data.get('episodenumber')),
120             'series': video_data.get('show', {}).get('title'),
121             'season_number': int_or_none(video_data.get('season', {}).get('num')),
122             'thumbnails': thumbnails,
123             'formats': formats,
124             'subtitles': subtitles,
125         }