]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/cloudflarestream.py
0a6073403439d0f388b2ec6451e97a62aa51274f
[yt-dlp.git] / yt_dlp / extractor / cloudflarestream.py
1 import base64
2 import re
3
4 from .common import InfoExtractor
5
6
7 class CloudflareStreamIE(InfoExtractor):
8 _DOMAIN_RE = r'(?:cloudflarestream\.com|(?:videodelivery|bytehighway)\.net)'
9 _EMBED_RE = r'embed\.%s/embed/[^/]+\.js\?.*?\bvideo=' % _DOMAIN_RE
10 _ID_RE = r'[\da-f]{32}|[\w-]+\.[\w-]+\.[\w-]+'
11 _VALID_URL = r'''(?x)
12 https?://
13 (?:
14 (?:watch\.)?%s/|
15 %s
16 )
17 (?P<id>%s)
18 ''' % (_DOMAIN_RE, _EMBED_RE, _ID_RE)
19 _TESTS = [{
20 'url': 'https://embed.cloudflarestream.com/embed/we4g.fla9.latest.js?video=31c9291ab41fac05471db4e73aa11717',
21 'info_dict': {
22 'id': '31c9291ab41fac05471db4e73aa11717',
23 'ext': 'mp4',
24 'title': '31c9291ab41fac05471db4e73aa11717',
25 },
26 'params': {
27 'skip_download': True,
28 },
29 }, {
30 'url': 'https://watch.cloudflarestream.com/9df17203414fd1db3e3ed74abbe936c1',
31 'only_matching': True,
32 }, {
33 'url': 'https://cloudflarestream.com/31c9291ab41fac05471db4e73aa11717/manifest/video.mpd',
34 'only_matching': True,
35 }, {
36 'url': 'https://embed.videodelivery.net/embed/r4xu.fla9.latest.js?video=81d80727f3022488598f68d323c1ad5e',
37 'only_matching': True,
38 }]
39
40 @staticmethod
41 def _extract_urls(webpage):
42 return [
43 mobj.group('url')
44 for mobj in re.finditer(
45 r'<script[^>]+\bsrc=(["\'])(?P<url>(?:https?:)?//%s(?:%s).*?)\1' % (CloudflareStreamIE._EMBED_RE, CloudflareStreamIE._ID_RE),
46 webpage)]
47
48 def _real_extract(self, url):
49 video_id = self._match_id(url)
50 domain = 'bytehighway.net' if 'bytehighway.net/' in url else 'videodelivery.net'
51 base_url = 'https://%s/%s/' % (domain, video_id)
52 if '.' in video_id:
53 video_id = self._parse_json(base64.urlsafe_b64decode(
54 video_id.split('.')[1] + '==='), video_id)['sub']
55 manifest_base_url = base_url + 'manifest/video.'
56
57 formats = self._extract_m3u8_formats(
58 manifest_base_url + 'm3u8', video_id, 'mp4',
59 'm3u8_native', m3u8_id='hls', fatal=False)
60 formats.extend(self._extract_mpd_formats(
61 manifest_base_url + 'mpd', video_id, mpd_id='dash', fatal=False))
62 self._sort_formats(formats)
63
64 return {
65 'id': video_id,
66 'title': video_id,
67 'thumbnail': base_url + 'thumbnails/thumbnail.jpg',
68 'formats': formats,
69 }