]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/glomex.py
85ffa4c0524f7237903da1182ab5ee0d88bc4125
[yt-dlp.git] / yt_dlp / extractor / glomex.py
1 import re
2 import urllib.parse
3
4 from .common import InfoExtractor
5 from ..utils import (
6 determine_ext,
7 extract_attributes,
8 ExtractorError,
9 int_or_none,
10 parse_qs,
11 smuggle_url,
12 unescapeHTML,
13 unsmuggle_url,
14 )
15
16
17 class GlomexBaseIE(InfoExtractor):
18 _DEFAULT_ORIGIN_URL = 'https://player.glomex.com/'
19 _API_URL = 'https://integration-cloudfront-eu-west-1.mes.glomex.cloud/'
20
21 @staticmethod
22 def _smuggle_origin_url(url, origin_url):
23 if origin_url is None:
24 return url
25 return smuggle_url(url, {'origin': origin_url})
26
27 @classmethod
28 def _unsmuggle_origin_url(cls, url, fallback_origin_url=None):
29 defaults = {'origin': fallback_origin_url or cls._DEFAULT_ORIGIN_URL}
30 unsmuggled_url, data = unsmuggle_url(url, default=defaults)
31 return unsmuggled_url, data['origin']
32
33 def _get_videoid_type(self, video_id):
34 _VIDEOID_TYPES = {
35 'v': 'video',
36 'pl': 'playlist',
37 'rl': 'related videos playlist',
38 'cl': 'curated playlist',
39 }
40 prefix = video_id.split('-')[0]
41 return _VIDEOID_TYPES.get(prefix, 'unknown type')
42
43 def _download_api_data(self, video_id, integration, current_url=None):
44 query = {
45 'integration_id': integration,
46 'playlist_id': video_id,
47 'current_url': current_url or self._DEFAULT_ORIGIN_URL,
48 }
49 video_id_type = self._get_videoid_type(video_id)
50 return self._download_json(
51 self._API_URL,
52 video_id, 'Downloading %s JSON' % video_id_type,
53 'Unable to download %s JSON' % video_id_type,
54 query=query)
55
56 def _download_and_extract_api_data(self, video_id, integration, current_url):
57 api_data = self._download_api_data(video_id, integration, current_url)
58 videos = api_data['videos']
59 if not videos:
60 raise ExtractorError('no videos found for %s' % video_id)
61 videos = [self._extract_api_data(video, video_id) for video in videos]
62 return videos[0] if len(videos) == 1 else self.playlist_result(videos, video_id)
63
64 def _extract_api_data(self, video, video_id):
65 if video.get('error_code') == 'contentGeoblocked':
66 self.raise_geo_restricted(countries=video['geo_locations'])
67
68 formats, subs = [], {}
69 for format_id, format_url in video['source'].items():
70 ext = determine_ext(format_url)
71 if ext == 'm3u8':
72 formats_, subs_ = self._extract_m3u8_formats_and_subtitles(
73 format_url, video_id, 'mp4', m3u8_id=format_id,
74 fatal=False)
75 formats.extend(formats_)
76 self._merge_subtitles(subs_, target=subs)
77 else:
78 formats.append({
79 'url': format_url,
80 'format_id': format_id,
81 })
82 if video.get('language'):
83 for fmt in formats:
84 fmt['language'] = video['language']
85 self._sort_formats(formats)
86
87 images = (video.get('images') or []) + [video.get('image') or {}]
88 thumbnails = [{
89 'id': image.get('id'),
90 'url': f'{image["url"]}/profile:player-960x540',
91 'width': 960,
92 'height': 540,
93 } for image in images if image.get('url')]
94 self._remove_duplicate_formats(thumbnails)
95
96 return {
97 'id': video.get('clip_id') or video_id,
98 'title': video.get('title'),
99 'description': video.get('description'),
100 'thumbnails': thumbnails,
101 'duration': int_or_none(video.get('clip_duration')),
102 'timestamp': video.get('created_at'),
103 'formats': formats,
104 'subtitles': subs,
105 }
106
107
108 class GlomexIE(GlomexBaseIE):
109 IE_NAME = 'glomex'
110 IE_DESC = 'Glomex videos'
111 _VALID_URL = r'https?://video\.glomex\.com/[^/]+/(?P<id>v-[^-]+)'
112 _INTEGRATION_ID = '19syy24xjn1oqlpc'
113
114 _TESTS = [{
115 'url': 'https://video.glomex.com/sport/v-cb24uwg77hgh-nach-2-0-sieg-guardiola-mit-mancity-vor-naechstem-titel',
116 'md5': 'cec33a943c4240c9cb33abea8c26242e',
117 'info_dict': {
118 'id': 'v-cb24uwg77hgh',
119 'ext': 'mp4',
120 'title': 'md5:38a90cedcfadd72982c81acf13556e0c',
121 'description': 'md5:1ea6b6caff1443fcbbba159e432eedb8',
122 'duration': 29600,
123 'timestamp': 1619895017,
124 'upload_date': '20210501',
125 },
126 }]
127
128 def _real_extract(self, url):
129 video_id = self._match_id(url)
130 return self.url_result(
131 GlomexEmbedIE.build_player_url(video_id, self._INTEGRATION_ID, url),
132 GlomexEmbedIE.ie_key(), video_id)
133
134
135 class GlomexEmbedIE(GlomexBaseIE):
136 IE_NAME = 'glomex:embed'
137 IE_DESC = 'Glomex embedded videos'
138 _BASE_PLAYER_URL = '//player.glomex.com/integration/1/iframe-player.html'
139 _BASE_PLAYER_URL_RE = re.escape(_BASE_PLAYER_URL).replace('/1/', r'/[^/]/')
140 _VALID_URL = rf'https?:{_BASE_PLAYER_URL_RE}\?([^#]+&)?playlistId=(?P<id>[^#&]+)'
141
142 _TESTS = [{
143 'url': 'https://player.glomex.com/integration/1/iframe-player.html?integrationId=4059a013k56vb2yd&playlistId=v-cfa6lye0dkdd-sf',
144 'md5': '68f259b98cc01918ac34180142fce287',
145 'info_dict': {
146 'id': 'v-cfa6lye0dkdd-sf',
147 'ext': 'mp4',
148 'timestamp': 1635337199,
149 'duration': 133080,
150 'upload_date': '20211027',
151 'description': 'md5:e741185fc309310ff5d0c789b437be66',
152 'title': 'md5:35647293513a6c92363817a0fb0a7961',
153 },
154 }, {
155 'url': 'https://player.glomex.com/integration/1/iframe-player.html?origin=fullpage&integrationId=19syy24xjn1oqlpc&playlistId=rl-vcb49w1fb592p&playlistIndex=0',
156 'info_dict': {
157 'id': 'rl-vcb49w1fb592p',
158 },
159 'playlist_count': 100,
160 }, {
161 'url': 'https://player.glomex.com/integration/1/iframe-player.html?playlistId=cl-bgqaata6aw8x&integrationId=19syy24xjn1oqlpc',
162 'info_dict': {
163 'id': 'cl-bgqaata6aw8x',
164 },
165 'playlist_mincount': 2,
166 }]
167
168 @classmethod
169 def build_player_url(cls, video_id, integration, origin_url=None):
170 query_string = urllib.parse.urlencode({
171 'playlistId': video_id,
172 'integrationId': integration,
173 })
174 return cls._smuggle_origin_url(f'https:{cls._BASE_PLAYER_URL}?{query_string}', origin_url)
175
176 @classmethod
177 def _extract_urls(cls, webpage, origin_url):
178 # https://docs.glomex.com/publisher/video-player-integration/javascript-api/
179 quot_re = r'["\']'
180
181 regex = fr'''(?x)
182 <iframe[^>]+?src=(?P<q>{quot_re})(?P<url>
183 (?:https?:)?{cls._BASE_PLAYER_URL_RE}\?(?:(?!(?P=q)).)+
184 )(?P=q)'''
185 for mobj in re.finditer(regex, webpage):
186 url = unescapeHTML(mobj.group('url'))
187 if cls.suitable(url):
188 yield cls._smuggle_origin_url(url, origin_url)
189
190 regex = fr'''(?x)
191 <glomex-player [^>]+?>|
192 <div[^>]* data-glomex-player=(?P<q>{quot_re})true(?P=q)[^>]*>'''
193 for mobj in re.finditer(regex, webpage):
194 attrs = extract_attributes(mobj.group(0))
195 if attrs.get('data-integration-id') and attrs.get('data-playlist-id'):
196 yield cls.build_player_url(attrs['data-playlist-id'], attrs['data-integration-id'], origin_url)
197
198 # naive parsing of inline scripts for hard-coded integration parameters
199 regex = fr'''(?x)
200 (?P<is_js>dataset\.)?%s\s*(?(is_js)=|:)\s*
201 (?P<q>{quot_re})(?P<id>(?:(?!(?P=q)).)+)(?P=q)\s'''
202 for mobj in re.finditer(r'(?x)<script[^<]*>.+?</script>', webpage):
203 script = mobj.group(0)
204 integration_id = re.search(regex % 'integrationId', script)
205 if not integration_id:
206 continue
207 playlist_id = re.search(regex % 'playlistId', script)
208 if playlist_id:
209 yield cls.build_player_url(playlist_id, integration_id, origin_url)
210
211 def _real_extract(self, url):
212 url, origin_url = self._unsmuggle_origin_url(url)
213 playlist_id = self._match_id(url)
214 integration = parse_qs(url).get('integrationId', [None])[0]
215 if not integration:
216 raise ExtractorError('No integrationId in URL', expected=True)
217 return self._download_and_extract_api_data(playlist_id, integration, origin_url)