Merge branch 'bilibili' of https://github.com/PeterDing/youtube-dl into PeterDing...
[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 )
12
13
14 class GoIE(InfoExtractor):
15     _BRANDS = {
16         'abc': '001',
17         'freeform': '002',
18         'watchdisneychannel': '004',
19         'watchdisneyjunior': '008',
20         'watchdisneyxd': '009',
21     }
22     _VALID_URL = r'https?://(?:(?P<sub_domain>%s)\.)?go\.com/.*?vdka(?P<id>\w+)' % '|'.join(_BRANDS.keys())
23     _TESTS = [{
24         'url': 'http://abc.go.com/shows/castle/video/most-recent/vdka0_g86w5onx',
25         'info_dict': {
26             'id': '0_g86w5onx',
27             'ext': 'mp4',
28             'title': 'Sneak Peek: Language Arts',
29             'description': 'md5:7dcdab3b2d17e5217c953256af964e9c',
30         },
31         'params': {
32             # m3u8 download
33             'skip_download': True,
34         },
35     }, {
36         'url': 'http://abc.go.com/shows/after-paradise/video/most-recent/vdka3335601',
37         'only_matching': True,
38     }]
39
40     def _real_extract(self, url):
41         sub_domain, video_id = re.match(self._VALID_URL, url).groups()
42         video_data = self._download_json(
43             'http://api.contents.watchabc.go.com/vp2/ws/contents/3000/videos/%s/001/-1/-1/-1/%s/-1/-1.json' % (self._BRANDS[sub_domain], video_id),
44             video_id)['video'][0]
45         title = video_data['title']
46
47         formats = []
48         for asset in video_data.get('assets', {}).get('asset', []):
49             asset_url = asset.get('value')
50             if not asset_url:
51                 continue
52             format_id = asset.get('format')
53             ext = determine_ext(asset_url)
54             if ext == 'm3u8':
55                 formats.extend(self._extract_m3u8_formats(
56                     asset_url, video_id, 'mp4', m3u8_id=format_id or 'hls', fatal=False))
57             else:
58                 formats.append({
59                     'format_id': format_id,
60                     'url': asset_url,
61                     'ext': ext,
62                 })
63         self._sort_formats(formats)
64
65         subtitles = {}
66         for cc in video_data.get('closedcaption', {}).get('src', []):
67             cc_url = cc.get('value')
68             if not cc_url:
69                 continue
70             ext = determine_ext(cc_url)
71             if ext == 'xml':
72                 ext = 'ttml'
73             subtitles.setdefault(cc.get('lang'), []).append({
74                 'url': cc_url,
75                 'ext': ext,
76             })
77
78         thumbnails = []
79         for thumbnail in video_data.get('thumbnails', {}).get('thumbnail', []):
80             thumbnail_url = thumbnail.get('value')
81             if not thumbnail_url:
82                 continue
83             thumbnails.append({
84                 'url': thumbnail_url,
85                 'width': int_or_none(thumbnail.get('width')),
86                 'height': int_or_none(thumbnail.get('height')),
87             })
88
89         return {
90             'id': video_id,
91             'title': title,
92             'description': video_data.get('longdescription') or video_data.get('description'),
93             'duration': int_or_none(video_data.get('duration', {}).get('value'), 1000),
94             'age_limit': parse_age_limit(video_data.get('tvrating', {}).get('rating')),
95             'episode_number': int_or_none(video_data.get('episodenumber')),
96             'series': video_data.get('show', {}).get('title'),
97             'season_number': int_or_none(video_data.get('season', {}).get('num')),
98             'thumbnails': thumbnails,
99             'formats': formats,
100             'subtitles': subtitles,
101         }