Merge pull request #7045 from remitamine/ign
[youtube-dl] / youtube_dl / extractor / beeg.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..compat import (
5     compat_chr,
6     compat_ord,
7     compat_urllib_parse_unquote,
8 )
9 from ..utils import (
10     int_or_none,
11     parse_iso8601,
12 )
13
14
15 class BeegIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?beeg\.com/(?P<id>\d+)'
17     _TEST = {
18         'url': 'http://beeg.com/5416503',
19         'md5': '46c384def73b33dbc581262e5ee67cef',
20         'info_dict': {
21             'id': '5416503',
22             'ext': 'mp4',
23             'title': 'Sultry Striptease',
24             'description': 'md5:d22219c09da287c14bed3d6c37ce4bc2',
25             'timestamp': 1391813355,
26             'upload_date': '20140207',
27             'duration': 383,
28             'tags': list,
29             'age_limit': 18,
30         }
31     }
32
33     def _real_extract(self, url):
34         video_id = self._match_id(url)
35
36         video = self._download_json(
37             'http://beeg.com/api/v5/video/%s' % video_id, video_id)
38
39         def split(o, e):
40             def cut(s, x):
41                 n.append(s[:x])
42                 return s[x:]
43             n = []
44             r = len(o) % e
45             if r > 0:
46                 o = cut(o, r)
47             while len(o) > e:
48                 o = cut(o, e)
49             n.append(o)
50             return n
51
52         def decrypt_key(key):
53             # Reverse engineered from http://static.beeg.com/cpl/1105.js
54             a = '5ShMcIQlssOd7zChAIOlmeTZDaUxULbJRnywYaiB'
55             e = compat_urllib_parse_unquote(key)
56             o = ''.join([
57                 compat_chr(compat_ord(e[n]) - compat_ord(a[n % len(a)]) % 21)
58                 for n in range(len(e))])
59             return ''.join(split(o, 3)[::-1])
60
61         def decrypt_url(encrypted_url):
62             encrypted_url = self._proto_relative_url(
63                 encrypted_url.replace('{DATA_MARKERS}', ''), 'http:')
64             key = self._search_regex(
65                 r'/key=(.*?)%2Cend=', encrypted_url, 'key', default=None)
66             if not key:
67                 return encrypted_url
68             return encrypted_url.replace(key, decrypt_key(key))
69
70         formats = []
71         for format_id, video_url in video.items():
72             if not video_url:
73                 continue
74             height = self._search_regex(
75                 r'^(\d+)[pP]$', format_id, 'height', default=None)
76             if not height:
77                 continue
78             formats.append({
79                 'url': decrypt_url(video_url),
80                 'format_id': format_id,
81                 'height': int(height),
82             })
83         self._sort_formats(formats)
84
85         title = video['title']
86         video_id = video.get('id') or video_id
87         display_id = video.get('code')
88         description = video.get('desc')
89
90         timestamp = parse_iso8601(video.get('date'), ' ')
91         duration = int_or_none(video.get('duration'))
92
93         tags = [tag.strip() for tag in video['tags'].split(',')] if video.get('tags') else None
94
95         return {
96             'id': video_id,
97             'display_id': display_id,
98             'title': title,
99             'description': description,
100             'timestamp': timestamp,
101             'duration': duration,
102             'tags': tags,
103             'formats': formats,
104             'age_limit': 18,
105         }