[douyutv] Add new extractor
[youtube-dl] / youtube_dl / extractor / letv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import datetime
5 import re
6 import time
7
8 from .common import InfoExtractor
9 from ..compat import (
10     compat_urlparse,
11     compat_urllib_parse,
12 )
13 from ..utils import (
14     determine_ext,
15     ExtractorError,
16     parse_iso8601,
17 )
18
19
20 class LetvIE(InfoExtractor):
21     _VALID_URL = r'http://www\.letv\.com/ptv/vplay/(?P<id>\d+).html'
22
23     _TESTS = [{
24         'url': 'http://www.letv.com/ptv/vplay/22005890.html',
25         'md5': 'cab23bd68d5a8db9be31c9a222c1e8df',
26         'info_dict': {
27             'id': '22005890',
28             'ext': 'mp4',
29             'title': '第87届奥斯卡颁奖礼完美落幕 《鸟人》成最大赢家',
30             'timestamp': 1424747397,
31             'upload_date': '20150224',
32             'description': 'md5:a9cb175fd753e2962176b7beca21a47c',
33         }
34     }, {
35         'url': 'http://www.letv.com/ptv/vplay/1415246.html',
36         'info_dict': {
37             'id': '1415246',
38             'ext': 'mp4',
39             'title': '美人天下01',
40             'description': 'md5:f88573d9d7225ada1359eaf0dbf8bcda',
41         },
42         'expected_warnings': [
43             'publish time'
44         ]
45     }]
46     # http://www.letv.com/ptv/vplay/1118082.html
47     # This video is available only in Mainland China
48
49     @staticmethod
50     def urshift(val, n):
51         return val >> n if val >= 0 else (val + 0x100000000) >> n
52
53     # ror() and calc_time_key() are reversed from a embedded swf file in KLetvPlayer.swf
54     def ror(self, param1, param2):
55         _loc3_ = 0
56         while _loc3_ < param2:
57             param1 = self.urshift(param1, 1) + ((param1 & 1) << 31)
58             _loc3_ += 1
59         return param1
60
61     def calc_time_key(self, param1):
62         _loc2_ = 773625421
63         _loc3_ = self.ror(param1, _loc2_ % 13)
64         _loc3_ = _loc3_ ^ _loc2_
65         _loc3_ = self.ror(_loc3_, _loc2_ % 17)
66         return _loc3_
67
68     def _real_extract(self, url):
69         media_id = self._match_id(url)
70         page = self._download_webpage(url, media_id)
71         params = {
72             'id': media_id,
73             'platid': 1,
74             'splatid': 101,
75             'format': 1,
76             'tkey': self.calc_time_key(int(time.time())),
77             'domain': 'www.letv.com'
78         }
79         play_json = self._download_json(
80             'http://api.letv.com/mms/out/video/playJson?' + compat_urllib_parse.urlencode(params),
81             media_id, 'playJson data')
82
83         # Check for errors
84         playstatus = play_json['playstatus']
85         if playstatus['status'] == 0:
86             flag = playstatus['flag']
87             if flag == 1:
88                 msg = 'Country %s auth error' % playstatus['country']
89             else:
90                 msg = 'Generic error. flag = %d' % flag
91             raise ExtractorError(msg, expected=True)
92
93         playurl = play_json['playurl']
94
95         formats = ['350', '1000', '1300', '720p', '1080p']
96         dispatch = playurl['dispatch']
97
98         urls = []
99         for format_id in formats:
100             if format_id in dispatch:
101                 media_url = playurl['domain'][0] + dispatch[format_id][0]
102
103                 # Mimic what flvxz.com do
104                 url_parts = list(compat_urlparse.urlparse(media_url))
105                 qs = dict(compat_urlparse.parse_qs(url_parts[4]))
106                 qs.update({
107                     'platid': '14',
108                     'splatid': '1401',
109                     'tss': 'no',
110                     'retry': 1
111                 })
112                 url_parts[4] = compat_urllib_parse.urlencode(qs)
113                 media_url = compat_urlparse.urlunparse(url_parts)
114
115                 url_info_dict = {
116                     'url': media_url,
117                     'ext': determine_ext(dispatch[format_id][1])
118                 }
119
120                 if format_id[-1:] == 'p':
121                     url_info_dict['height'] = format_id[:-1]
122
123                 urls.append(url_info_dict)
124
125         publish_time = parse_iso8601(self._html_search_regex(
126             r'发布时间&nbsp;([^<>]+) ', page, 'publish time', fatal=False),
127             delimiter=' ', timezone=datetime.timedelta(hours=8))
128         description = self._html_search_meta('description', page, fatal=False)
129
130         return {
131             'id': media_id,
132             'formats': urls,
133             'title': playurl['title'],
134             'thumbnail': playurl['pic'],
135             'description': description,
136             'timestamp': publish_time,
137         }
138
139
140 class LetvTvIE(InfoExtractor):
141     _VALID_URL = r'http://www.letv.com/tv/(?P<id>\d+).html'
142     _TESTS = [{
143         'url': 'http://www.letv.com/tv/46177.html',
144         'info_dict': {
145             'id': '46177',
146             'title': '美人天下',
147             'description': 'md5:395666ff41b44080396e59570dbac01c'
148         },
149         'playlist_count': 35
150     }]
151
152     def _real_extract(self, url):
153         playlist_id = self._match_id(url)
154         page = self._download_webpage(url, playlist_id)
155
156         media_urls = list(set(re.findall(
157             r'http://www.letv.com/ptv/vplay/\d+.html', page)))
158         entries = [self.url_result(media_url, ie='Letv')
159                    for media_url in media_urls]
160
161         title = self._html_search_meta('keywords', page,
162                                        fatal=False).split(',')[0]
163         description = self._html_search_meta('description', page, fatal=False)
164
165         return self.playlist_result(entries, playlist_id, playlist_title=title,
166                                     playlist_description=description)
167
168
169 class LetvPlaylistIE(LetvTvIE):
170     _VALID_URL = r'http://tv.letv.com/[a-z]+/(?P<id>[a-z]+)/index.s?html'
171     _TESTS = [{
172         'url': 'http://tv.letv.com/izt/wuzetian/index.html',
173         'info_dict': {
174             'id': 'wuzetian',
175             'title': '武媚娘传奇',
176             'description': 'md5:e12499475ab3d50219e5bba00b3cb248'
177         },
178         # This playlist contains some extra videos other than the drama itself
179         'playlist_mincount': 96
180     }, {
181         'url': 'http://tv.letv.com/pzt/lswjzzjc/index.shtml',
182         'info_dict': {
183             'id': 'lswjzzjc',
184             # The title should be "劲舞青春", but I can't find a simple way to
185             # determine the playlist title
186             'title': '乐视午间自制剧场',
187             'description': 'md5:b1eef244f45589a7b5b1af9ff25a4489'
188         },
189         'playlist_mincount': 7
190     }]