[ie/dplay] Fix extractors (#10471)
[yt-dlp3.git] / yt_dlp / extractor / uplynk.py
blobe7d816ef4f6e02a52e7a32faa804e6cd6da17a0c
1 import re
3 from .common import InfoExtractor
4 from ..utils import (
5 ExtractorError,
6 float_or_none,
7 smuggle_url,
8 traverse_obj,
9 unsmuggle_url,
10 update_url_query,
14 class UplynkBaseIE(InfoExtractor):
15 _UPLYNK_URL_RE = r'''(?x)
16 https?://[\w-]+\.uplynk\.com/(?P<path>
17 ext/[0-9a-f]{32}/(?P<external_id>[^/?&]+)|
18 (?P<id>[0-9a-f]{32})
19 )\.(?:m3u8|json)
20 (?:.*?\bpbs=(?P<session_id>[^&]+))?'''
22 def _extract_uplynk_info(self, url):
23 uplynk_content_url, smuggled_data = unsmuggle_url(url, {})
24 mobj = re.match(self._UPLYNK_URL_RE, uplynk_content_url)
25 if not mobj:
26 raise ExtractorError('Necessary parameters not found in Uplynk URL')
27 path, external_id, video_id, session_id = mobj.group('path', 'external_id', 'id', 'session_id')
28 display_id = video_id or external_id
29 headers = traverse_obj(
30 smuggled_data, {'Referer': 'Referer', 'Origin': 'Origin'}, casesense=False)
31 formats, subtitles = self._extract_m3u8_formats_and_subtitles(
32 f'http://content.uplynk.com/{path}.m3u8', display_id, 'mp4', headers=headers)
33 if session_id:
34 for f in formats:
35 f['extra_param_to_segment_url'] = f'pbs={session_id}'
36 asset = self._download_json(
37 f'http://content.uplynk.com/player/assetinfo/{path}.json', display_id)
38 if asset.get('error') == 1:
39 msg = asset.get('msg') or 'unknown error'
40 raise ExtractorError(f'{self.IE_NAME} said: {msg}', expected=True)
42 return {
43 'id': asset['asset'],
44 'title': asset['desc'],
45 'thumbnail': asset.get('default_poster_url'),
46 'duration': float_or_none(asset.get('duration')),
47 'uploader_id': asset.get('owner'),
48 'formats': formats,
49 'subtitles': subtitles,
53 class UplynkIE(UplynkBaseIE):
54 IE_NAME = 'uplynk'
55 _VALID_URL = UplynkBaseIE._UPLYNK_URL_RE
56 _TEST = {
57 'url': 'http://content.uplynk.com/e89eaf2ce9054aa89d92ddb2d817a52e.m3u8',
58 'info_dict': {
59 'id': 'e89eaf2ce9054aa89d92ddb2d817a52e',
60 'ext': 'mp4',
61 'title': '030816-kgo-530pm-solar-eclipse-vid_web.mp4',
62 'uploader_id': '4413701bf5a1488db55b767f8ae9d4fa',
63 'duration': 530.2739166666679,
64 'thumbnail': r're:^https?://.*\.jpg$',
66 'params': {
67 'skip_download': 'm3u8',
71 def _real_extract(self, url):
72 return self._extract_uplynk_info(url)
75 class UplynkPreplayIE(UplynkBaseIE):
76 IE_NAME = 'uplynk:preplay'
77 _VALID_URL = r'https?://[\w-]+\.uplynk\.com/preplay2?/(?P<path>ext/[0-9a-f]{32}/(?P<external_id>[^/?&]+)|(?P<id>[0-9a-f]{32}))\.json'
79 def _real_extract(self, url):
80 url, smuggled_data = unsmuggle_url(url, {})
81 path, external_id, video_id = self._match_valid_url(url).groups()
82 display_id = video_id or external_id
83 preplay = self._download_json(url, display_id)
84 content_url = f'http://content.uplynk.com/{path}.m3u8'
85 session_id = preplay.get('sid')
86 if session_id:
87 content_url = update_url_query(content_url, {'pbs': session_id})
88 return self._extract_uplynk_info(smuggle_url(content_url, smuggled_data))