efb259f96f3c56157998ca379f38f8d4037d643e
[youtube-dl] / youtube_dl / extractor / streamango.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_chr
8 from ..utils import (
9     determine_ext,
10     ExtractorError,
11     int_or_none,
12     js_to_json,
13 )
14
15
16 class StreamangoIE(InfoExtractor):
17     _VALID_URL = r'https?://(?:www\.)?(?:streamango\.com|fruithosts\.net)/(?:f|embed)/(?P<id>[^/?#&]+)'
18     _TESTS = [{
19         'url': 'https://streamango.com/f/clapasobsptpkdfe/20170315_150006_mp4',
20         'md5': 'e992787515a182f55e38fc97588d802a',
21         'info_dict': {
22             'id': 'clapasobsptpkdfe',
23             'ext': 'mp4',
24             'title': '20170315_150006.mp4',
25         }
26     }, {
27         # no og:title
28         'url': 'https://streamango.com/embed/foqebrpftarclpob/asdf_asd_2_mp4',
29         'info_dict': {
30             'id': 'foqebrpftarclpob',
31             'ext': 'mp4',
32             'title': 'foqebrpftarclpob',
33         },
34         'params': {
35             'skip_download': True,
36         },
37         'skip': 'gone',
38     }, {
39         'url': 'https://streamango.com/embed/clapasobsptpkdfe/20170315_150006_mp4',
40         'only_matching': True,
41     }, {
42         'url': 'https://fruithosts.net/f/mreodparcdcmspsm/w1f1_r4lph_2018_brrs_720p_latino_mp4',
43         'only_matching': True,
44     }]
45
46     def _real_extract(self, url):
47         def decrypt_src(encoded, val):
48             ALPHABET = '=/+9876543210zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA'
49             encoded = re.sub(r'[^A-Za-z0-9+/=]', '', encoded)
50             decoded = ''
51             sm = [None] * 4
52             i = 0
53             str_len = len(encoded)
54             while i < str_len:
55                 for j in range(4):
56                     sm[j % 4] = ALPHABET.index(encoded[i])
57                     i += 1
58                 char_code = ((sm[0] << 0x2) | (sm[1] >> 0x4)) ^ val
59                 decoded += compat_chr(char_code)
60                 if sm[2] != 0x40:
61                     char_code = ((sm[1] & 0xf) << 0x4) | (sm[2] >> 0x2)
62                     decoded += compat_chr(char_code)
63                 if sm[3] != 0x40:
64                     char_code = ((sm[2] & 0x3) << 0x6) | sm[3]
65                     decoded += compat_chr(char_code)
66             return decoded
67
68         video_id = self._match_id(url)
69
70         webpage = self._download_webpage(url, video_id)
71
72         title = self._og_search_title(webpage, default=video_id)
73
74         formats = []
75         for format_ in re.findall(r'({[^}]*\bsrc\s*:\s*[^}]*})', webpage):
76             mobj = re.search(r'(src\s*:\s*[^(]+\(([^)]*)\)[\s,]*)', format_)
77             if mobj is None:
78                 continue
79
80             format_ = format_.replace(mobj.group(0), '')
81
82             video = self._parse_json(
83                 format_, video_id, transform_source=js_to_json,
84                 fatal=False) or {}
85
86             mobj = re.search(
87                 r'([\'"])(?P<src>(?:(?!\1).)+)\1\s*,\s*(?P<val>\d+)',
88                 mobj.group(1))
89             if mobj is None:
90                 continue
91
92             src = decrypt_src(mobj.group('src'), int_or_none(mobj.group('val')))
93             if not src:
94                 continue
95
96             ext = determine_ext(src, default_ext=None)
97             if video.get('type') == 'application/dash+xml' or ext == 'mpd':
98                 formats.extend(self._extract_mpd_formats(
99                     src, video_id, mpd_id='dash', fatal=False))
100             else:
101                 formats.append({
102                     'url': src,
103                     'ext': ext or 'mp4',
104                     'width': int_or_none(video.get('width')),
105                     'height': int_or_none(video.get('height')),
106                     'tbr': int_or_none(video.get('bitrate')),
107                 })
108
109         if not formats:
110             error = self._search_regex(
111                 r'<p[^>]+\bclass=["\']lead[^>]+>(.+?)</p>', webpage,
112                 'error', default=None)
113             if not error and '>Sorry' in webpage:
114                 error = 'Video %s is not available' % video_id
115             if error:
116                 raise ExtractorError(error, expected=True)
117
118         self._sort_formats(formats)
119
120         return {
121             'id': video_id,
122             'url': url,
123             'title': title,
124             'formats': formats,
125         }