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