Start moving to ytdl-org
[youtube-dl] / youtube_dl / extractor / arkena.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_urlparse
8 from ..utils import (
9     determine_ext,
10     ExtractorError,
11     float_or_none,
12     int_or_none,
13     mimetype2ext,
14     parse_iso8601,
15     strip_jsonp,
16 )
17
18
19 class ArkenaIE(InfoExtractor):
20     _VALID_URL = r'''(?x)
21                         https?://
22                             (?:
23                                 video\.arkena\.com/play2/embed/player\?|
24                                 play\.arkena\.com/(?:config|embed)/avp/v\d/player/media/(?P<id>[^/]+)/[^/]+/(?P<account_id>\d+)
25                             )
26                         '''
27     _TESTS = [{
28         'url': 'https://play.arkena.com/embed/avp/v2/player/media/b41dda37-d8e7-4d3f-b1b5-9a9db578bdfe/1/129411',
29         'md5': 'b96f2f71b359a8ecd05ce4e1daa72365',
30         'info_dict': {
31             'id': 'b41dda37-d8e7-4d3f-b1b5-9a9db578bdfe',
32             'ext': 'mp4',
33             'title': 'Big Buck Bunny',
34             'description': 'Royalty free test video',
35             'timestamp': 1432816365,
36             'upload_date': '20150528',
37             'is_live': False,
38         },
39     }, {
40         'url': 'https://play.arkena.com/config/avp/v2/player/media/b41dda37-d8e7-4d3f-b1b5-9a9db578bdfe/1/129411/?callbackMethod=jQuery1111023664739129262213_1469227693893',
41         'only_matching': True,
42     }, {
43         'url': 'http://play.arkena.com/config/avp/v1/player/media/327336/darkmatter/131064/?callbackMethod=jQuery1111002221189684892677_1469227595972',
44         'only_matching': True,
45     }, {
46         'url': 'http://play.arkena.com/embed/avp/v1/player/media/327336/darkmatter/131064/',
47         'only_matching': True,
48     }, {
49         'url': 'http://video.arkena.com/play2/embed/player?accountId=472718&mediaId=35763b3b-00090078-bf604299&pageStyling=styled',
50         'only_matching': True,
51     }]
52
53     @staticmethod
54     def _extract_url(webpage):
55         # See https://support.arkena.com/display/PLAY/Ways+to+embed+your+video
56         mobj = re.search(
57             r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//play\.arkena\.com/embed/avp/.+?)\1',
58             webpage)
59         if mobj:
60             return mobj.group('url')
61
62     def _real_extract(self, url):
63         mobj = re.match(self._VALID_URL, url)
64         video_id = mobj.group('id')
65         account_id = mobj.group('account_id')
66
67         # Handle http://video.arkena.com/play2/embed/player URL
68         if not video_id:
69             qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
70             video_id = qs.get('mediaId', [None])[0]
71             account_id = qs.get('accountId', [None])[0]
72             if not video_id or not account_id:
73                 raise ExtractorError('Invalid URL', expected=True)
74
75         playlist = self._download_json(
76             'https://play.arkena.com/config/avp/v2/player/media/%s/0/%s/?callbackMethod=_'
77             % (video_id, account_id),
78             video_id, transform_source=strip_jsonp)['Playlist'][0]
79
80         media_info = playlist['MediaInfo']
81         title = media_info['Title']
82         media_files = playlist['MediaFiles']
83
84         is_live = False
85         formats = []
86         for kind_case, kind_formats in media_files.items():
87             kind = kind_case.lower()
88             for f in kind_formats:
89                 f_url = f.get('Url')
90                 if not f_url:
91                     continue
92                 is_live = f.get('Live') == 'true'
93                 exts = (mimetype2ext(f.get('Type')), determine_ext(f_url, None))
94                 if kind == 'm3u8' or 'm3u8' in exts:
95                     formats.extend(self._extract_m3u8_formats(
96                         f_url, video_id, 'mp4', 'm3u8_native',
97                         m3u8_id=kind, fatal=False, live=is_live))
98                 elif kind == 'flash' or 'f4m' in exts:
99                     formats.extend(self._extract_f4m_formats(
100                         f_url, video_id, f4m_id=kind, fatal=False))
101                 elif kind == 'dash' or 'mpd' in exts:
102                     formats.extend(self._extract_mpd_formats(
103                         f_url, video_id, mpd_id=kind, fatal=False))
104                 elif kind == 'silverlight':
105                     # TODO: process when ism is supported (see
106                     # https://github.com/ytdl-org/youtube-dl/issues/8118)
107                     continue
108                 else:
109                     tbr = float_or_none(f.get('Bitrate'), 1000)
110                     formats.append({
111                         'url': f_url,
112                         'format_id': '%s-%d' % (kind, tbr) if tbr else kind,
113                         'tbr': tbr,
114                     })
115         self._sort_formats(formats)
116
117         description = media_info.get('Description')
118         video_id = media_info.get('VideoId') or video_id
119         timestamp = parse_iso8601(media_info.get('PublishDate'))
120         thumbnails = [{
121             'url': thumbnail['Url'],
122             'width': int_or_none(thumbnail.get('Size')),
123         } for thumbnail in (media_info.get('Poster') or []) if thumbnail.get('Url')]
124
125         return {
126             'id': video_id,
127             'title': title,
128             'description': description,
129             'timestamp': timestamp,
130             'is_live': is_live,
131             'thumbnails': thumbnails,
132             'formats': formats,
133         }