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