]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/arkena.py
9da2bfd5e0591de219c401e2aab04cc493d01adf
[yt-dlp.git] / yt_dlp / extractor / arkena.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 ExtractorError,
6 float_or_none,
7 int_or_none,
8 parse_iso8601,
9 parse_qs,
10 try_get,
11 )
12
13
14 class ArkenaIE(InfoExtractor):
15 _VALID_URL = r'''(?x)
16 https?://
17 (?:
18 video\.(?:arkena|qbrick)\.com/play2/embed/player\?|
19 play\.arkena\.com/(?:config|embed)/avp/v\d/player/media/(?P<id>[^/]+)/[^/]+/(?P<account_id>\d+)
20 )
21 '''
22 _TESTS = [{
23 'url': 'https://video.qbrick.com/play2/embed/player?accountId=1034090&mediaId=d8ab4607-00090107-aab86310',
24 'md5': '97f117754e5f3c020f5f26da4a44ebaf',
25 'info_dict': {
26 'id': 'd8ab4607-00090107-aab86310',
27 'ext': 'mp4',
28 'title': 'EM_HT20_117_roslund_v2.mp4',
29 'timestamp': 1608285912,
30 'upload_date': '20201218',
31 'duration': 1429.162667,
32 'subtitles': {
33 'sv': 'count:3',
34 },
35 },
36 }, {
37 'url': 'https://play.arkena.com/embed/avp/v2/player/media/b41dda37-d8e7-4d3f-b1b5-9a9db578bdfe/1/129411',
38 'only_matching': True,
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 = self._match_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 = parse_qs(url)
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 media = self._download_json(
76 'https://video.qbrick.com/api/v1/public/accounts/%s/medias/%s' % (account_id, video_id),
77 video_id, query={
78 # https://video.qbrick.com/docs/api/examples/library-api.html
79 'fields': 'asset/resources/*/renditions/*(height,id,language,links/*(href,mimeType),type,size,videos/*(audios/*(codec,sampleRate),bitrate,codec,duration,height,width),width),created,metadata/*(title,description),tags',
80 })
81 metadata = media.get('metadata') or {}
82 title = metadata['title']
83
84 duration = None
85 formats = []
86 thumbnails = []
87 subtitles = {}
88 for resource in media['asset']['resources']:
89 for rendition in (resource.get('renditions') or []):
90 rendition_type = rendition.get('type')
91 for i, link in enumerate(rendition.get('links') or []):
92 href = link.get('href')
93 if not href:
94 continue
95 if rendition_type == 'image':
96 thumbnails.append({
97 'filesize': int_or_none(rendition.get('size')),
98 'height': int_or_none(rendition.get('height')),
99 'id': rendition.get('id'),
100 'url': href,
101 'width': int_or_none(rendition.get('width')),
102 })
103 elif rendition_type == 'subtitle':
104 subtitles.setdefault(rendition.get('language') or 'en', []).append({
105 'url': href,
106 })
107 elif rendition_type == 'video':
108 f = {
109 'filesize': int_or_none(rendition.get('size')),
110 'format_id': rendition.get('id'),
111 'url': href,
112 }
113 video = try_get(rendition, lambda x: x['videos'][i], dict)
114 if video:
115 if not duration:
116 duration = float_or_none(video.get('duration'))
117 f.update({
118 'height': int_or_none(video.get('height')),
119 'tbr': int_or_none(video.get('bitrate'), 1000),
120 'vcodec': video.get('codec'),
121 'width': int_or_none(video.get('width')),
122 })
123 audio = try_get(video, lambda x: x['audios'][0], dict)
124 if audio:
125 f.update({
126 'acodec': audio.get('codec'),
127 'asr': int_or_none(audio.get('sampleRate')),
128 })
129 formats.append(f)
130 elif rendition_type == 'index':
131 mime_type = link.get('mimeType')
132 if mime_type == 'application/smil+xml':
133 formats.extend(self._extract_smil_formats(
134 href, video_id, fatal=False))
135 elif mime_type == 'application/x-mpegURL':
136 formats.extend(self._extract_m3u8_formats(
137 href, video_id, 'mp4', 'm3u8_native',
138 m3u8_id='hls', fatal=False))
139 elif mime_type == 'application/hds+xml':
140 formats.extend(self._extract_f4m_formats(
141 href, video_id, f4m_id='hds', fatal=False))
142 elif mime_type == 'application/dash+xml':
143 formats.extend(self._extract_f4m_formats(
144 href, video_id, f4m_id='hds', fatal=False))
145 elif mime_type == 'application/vnd.ms-sstr+xml':
146 formats.extend(self._extract_ism_formats(
147 href, video_id, ism_id='mss', fatal=False))
148 self._sort_formats(formats)
149
150 return {
151 'id': video_id,
152 'title': title,
153 'description': metadata.get('description'),
154 'timestamp': parse_iso8601(media.get('created')),
155 'thumbnails': thumbnails,
156 'subtitles': subtitles,
157 'duration': duration,
158 'tags': media.get('tags'),
159 'formats': formats,
160 }