]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/beeg.py
Update changelog.
[youtubedl] / 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 urljoin,
13 )
14
15
16 class BeegIE(InfoExtractor):
17 _VALID_URL = r'https?://(?:www\.)?beeg\.com/(?P<id>\d+)'
18 _TEST = {
19 'url': 'http://beeg.com/5416503',
20 'md5': 'a1a1b1a8bc70a89e49ccfd113aed0820',
21 'info_dict': {
22 'id': '5416503',
23 'ext': 'mp4',
24 'title': 'Sultry Striptease',
25 'description': 'md5:d22219c09da287c14bed3d6c37ce4bc2',
26 'timestamp': 1391813355,
27 'upload_date': '20140207',
28 'duration': 383,
29 'tags': list,
30 'age_limit': 18,
31 }
32 }
33
34 def _real_extract(self, url):
35 video_id = self._match_id(url)
36
37 webpage = self._download_webpage(url, video_id)
38
39 cpl_url = self._search_regex(
40 r'<script[^>]+src=(["\'])(?P<url>(?:/static|(?:https?:)?//static\.beeg\.com)/cpl/\d+\.js.*?)\1',
41 webpage, 'cpl', default=None, group='url')
42
43 cpl_url = urljoin(url, cpl_url)
44
45 beeg_version, beeg_salt = [None] * 2
46
47 if cpl_url:
48 cpl = self._download_webpage(
49 self._proto_relative_url(cpl_url), video_id,
50 'Downloading cpl JS', fatal=False)
51 if cpl:
52 beeg_version = int_or_none(self._search_regex(
53 r'beeg_version\s*=\s*([^\b]+)', cpl,
54 'beeg version', default=None)) or self._search_regex(
55 r'/(\d+)\.js', cpl_url, 'beeg version', default=None)
56 beeg_salt = self._search_regex(
57 r'beeg_salt\s*=\s*(["\'])(?P<beeg_salt>.+?)\1', cpl, 'beeg salt',
58 default=None, group='beeg_salt')
59
60 beeg_version = beeg_version or '2185'
61 beeg_salt = beeg_salt or 'pmweAkq8lAYKdfWcFCUj0yoVgoPlinamH5UE1CB3H'
62
63 for api_path in ('', 'api.'):
64 video = self._download_json(
65 'https://%sbeeg.com/api/v6/%s/video/%s'
66 % (api_path, beeg_version, video_id), video_id,
67 fatal=api_path == 'api.')
68 if video:
69 break
70
71 def split(o, e):
72 def cut(s, x):
73 n.append(s[:x])
74 return s[x:]
75 n = []
76 r = len(o) % e
77 if r > 0:
78 o = cut(o, r)
79 while len(o) > e:
80 o = cut(o, e)
81 n.append(o)
82 return n
83
84 def decrypt_key(key):
85 # Reverse engineered from http://static.beeg.com/cpl/1738.js
86 a = beeg_salt
87 e = compat_urllib_parse_unquote(key)
88 o = ''.join([
89 compat_chr(compat_ord(e[n]) - compat_ord(a[n % len(a)]) % 21)
90 for n in range(len(e))])
91 return ''.join(split(o, 3)[::-1])
92
93 def decrypt_url(encrypted_url):
94 encrypted_url = self._proto_relative_url(
95 encrypted_url.replace('{DATA_MARKERS}', ''), 'https:')
96 key = self._search_regex(
97 r'/key=(.*?)%2Cend=', encrypted_url, 'key', default=None)
98 if not key:
99 return encrypted_url
100 return encrypted_url.replace(key, decrypt_key(key))
101
102 formats = []
103 for format_id, video_url in video.items():
104 if not video_url:
105 continue
106 height = self._search_regex(
107 r'^(\d+)[pP]$', format_id, 'height', default=None)
108 if not height:
109 continue
110 formats.append({
111 'url': decrypt_url(video_url),
112 'format_id': format_id,
113 'height': int(height),
114 })
115 self._sort_formats(formats)
116
117 title = video['title']
118 video_id = video.get('id') or video_id
119 display_id = video.get('code')
120 description = video.get('desc')
121
122 timestamp = parse_iso8601(video.get('date'), ' ')
123 duration = int_or_none(video.get('duration'))
124
125 tags = [tag.strip() for tag in video['tags'].split(',')] if video.get('tags') else None
126
127 return {
128 'id': video_id,
129 'display_id': display_id,
130 'title': title,
131 'description': description,
132 'timestamp': timestamp,
133 'duration': duration,
134 'tags': tags,
135 'formats': formats,
136 'age_limit': self._rta_search(webpage),
137 }