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