You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
11 KiB

Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import base64
  4. import itertools
  5. import random
  6. import re
  7. import string
  8. import time
  9. from .common import InfoExtractor
  10. from ..compat import (
  11. compat_urllib_parse_urlencode,
  12. compat_ord,
  13. )
  14. from ..utils import (
  15. ExtractorError,
  16. get_element_by_attribute,
  17. sanitized_Request,
  18. )
  19. class YoukuIE(InfoExtractor):
  20. IE_NAME = 'youku'
  21. IE_DESC = '优酷'
  22. _VALID_URL = r'''(?x)
  23. (?:
  24. http://(?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)|
  25. youku:)
  26. (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|)
  27. '''
  28. _TESTS = [{
  29. # MD5 is unstable
  30. 'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html',
  31. 'info_dict': {
  32. 'id': 'XMTc1ODE5Njcy_part1',
  33. 'title': '★Smile﹗♡ Git Fresh -Booty Music舞蹈.',
  34. 'ext': 'flv'
  35. }
  36. }, {
  37. 'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf',
  38. 'only_matching': True,
  39. }, {
  40. 'url': 'http://v.youku.com/v_show/id_XODgxNjg1Mzk2_ev_1.html',
  41. 'info_dict': {
  42. 'id': 'XODgxNjg1Mzk2',
  43. 'title': '武媚娘传奇 85',
  44. },
  45. 'playlist_count': 11,
  46. 'skip': 'Available in China only',
  47. }, {
  48. 'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html',
  49. 'info_dict': {
  50. 'id': 'XMTI1OTczNDM5Mg',
  51. 'title': '花千骨 04',
  52. },
  53. 'playlist_count': 13,
  54. }, {
  55. 'url': 'http://v.youku.com/v_show/id_XNjA1NzA2Njgw.html',
  56. 'note': 'Video protected with password',
  57. 'info_dict': {
  58. 'id': 'XNjA1NzA2Njgw',
  59. 'title': '邢義田复旦讲座之想象中的胡人—从“左衽孔子”说起',
  60. },
  61. 'playlist_count': 19,
  62. 'params': {
  63. 'videopassword': '100600',
  64. },
  65. }, {
  66. # /play/get.json contains streams with "channel_type":"tail"
  67. 'url': 'http://v.youku.com/v_show/id_XOTUxMzg4NDMy.html',
  68. 'info_dict': {
  69. 'id': 'XOTUxMzg4NDMy',
  70. 'title': '我的世界☆明月庄主☆车震猎杀☆杀人艺术Minecraft',
  71. },
  72. 'playlist_count': 6,
  73. }]
  74. def construct_video_urls(self, data):
  75. # get sid, token
  76. def yk_t(s1, s2):
  77. ls = list(range(256))
  78. t = 0
  79. for i in range(256):
  80. t = (t + ls[i] + compat_ord(s1[i % len(s1)])) % 256
  81. ls[i], ls[t] = ls[t], ls[i]
  82. s = bytearray()
  83. x, y = 0, 0
  84. for i in range(len(s2)):
  85. y = (y + 1) % 256
  86. x = (x + ls[y]) % 256
  87. ls[x], ls[y] = ls[y], ls[x]
  88. s.append(compat_ord(s2[i]) ^ ls[(ls[x] + ls[y]) % 256])
  89. return bytes(s)
  90. sid, token = yk_t(
  91. b'becaf9be', base64.b64decode(data['security']['encrypt_string'].encode('ascii'))
  92. ).decode('ascii').split('_')
  93. # get oip
  94. oip = data['security']['ip']
  95. fileid_dict = {}
  96. for stream in data['stream']:
  97. if stream.get('channel_type') == 'tail':
  98. continue
  99. format = stream.get('stream_type')
  100. fileid = stream['stream_fileid']
  101. fileid_dict[format] = fileid
  102. def get_fileid(format, n):
  103. number = hex(int(str(n), 10))[2:].upper()
  104. if len(number) == 1:
  105. number = '0' + number
  106. streamfileids = fileid_dict[format]
  107. fileid = streamfileids[0:8] + number + streamfileids[10:]
  108. return fileid
  109. # get ep
  110. def generate_ep(format, n):
  111. fileid = get_fileid(format, n)
  112. ep_t = yk_t(
  113. b'bf7e5f01',
  114. ('%s_%s_%s' % (sid, fileid, token)).encode('ascii')
  115. )
  116. ep = base64.b64encode(ep_t).decode('ascii')
  117. return ep
  118. # generate video_urls
  119. video_urls_dict = {}
  120. for stream in data['stream']:
  121. if stream.get('channel_type') == 'tail':
  122. continue
  123. format = stream.get('stream_type')
  124. video_urls = []
  125. for dt in stream['segs']:
  126. n = str(stream['segs'].index(dt))
  127. param = {
  128. 'K': dt['key'],
  129. 'hd': self.get_hd(format),
  130. 'myp': 0,
  131. 'ypp': 0,
  132. 'ctype': 12,
  133. 'ev': 1,
  134. 'token': token,
  135. 'oip': oip,
  136. 'ep': generate_ep(format, n)
  137. }
  138. video_url = \
  139. 'http://k.youku.com/player/getFlvPath/' + \
  140. 'sid/' + sid + \
  141. '_00' + \
  142. '/st/' + self.parse_ext_l(format) + \
  143. '/fileid/' + get_fileid(format, n) + '?' + \
  144. compat_urllib_parse_urlencode(param)
  145. video_urls.append(video_url)
  146. video_urls_dict[format] = video_urls
  147. return video_urls_dict
  148. @staticmethod
  149. def get_ysuid():
  150. return '%d%s' % (int(time.time()), ''.join([
  151. random.choice(string.ascii_letters) for i in range(3)]))
  152. def get_hd(self, fm):
  153. hd_id_dict = {
  154. '3gp': '0',
  155. '3gphd': '1',
  156. 'flv': '0',
  157. 'flvhd': '0',
  158. 'mp4': '1',
  159. 'mp4hd': '1',
  160. 'mp4hd2': '1',
  161. 'mp4hd3': '1',
  162. 'hd2': '2',
  163. 'hd3': '3',
  164. }
  165. return hd_id_dict[fm]
  166. def parse_ext_l(self, fm):
  167. ext_dict = {
  168. '3gp': 'flv',
  169. '3gphd': 'mp4',
  170. 'flv': 'flv',
  171. 'flvhd': 'flv',
  172. 'mp4': 'mp4',
  173. 'mp4hd': 'mp4',
  174. 'mp4hd2': 'flv',
  175. 'mp4hd3': 'flv',
  176. 'hd2': 'flv',
  177. 'hd3': 'flv',
  178. }
  179. return ext_dict[fm]
  180. def get_format_name(self, fm):
  181. _dict = {
  182. '3gp': 'h6',
  183. '3gphd': 'h5',
  184. 'flv': 'h4',
  185. 'flvhd': 'h4',
  186. 'mp4': 'h3',
  187. 'mp4hd': 'h3',
  188. 'mp4hd2': 'h4',
  189. 'mp4hd3': 'h4',
  190. 'hd2': 'h2',
  191. 'hd3': 'h1',
  192. }
  193. return _dict[fm]
  194. def _real_extract(self, url):
  195. video_id = self._match_id(url)
  196. self._set_cookie('youku.com', '__ysuid', self.get_ysuid())
  197. def retrieve_data(req_url, note):
  198. headers = {
  199. 'Referer': req_url,
  200. }
  201. self._set_cookie('youku.com', 'xreferrer', 'http://www.youku.com')
  202. req = sanitized_Request(req_url, headers=headers)
  203. cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
  204. if cn_verification_proxy:
  205. req.add_header('Ytdl-request-proxy', cn_verification_proxy)
  206. raw_data = self._download_json(req, video_id, note=note)
  207. return raw_data['data']
  208. video_password = self._downloader.params.get('videopassword')
  209. # request basic data
  210. basic_data_url = 'http://play.youku.com/play/get.json?vid=%s&ct=12' % video_id
  211. if video_password:
  212. basic_data_url += '&pwd=%s' % video_password
  213. data = retrieve_data(basic_data_url, 'Downloading JSON metadata')
  214. error = data.get('error')
  215. if error:
  216. error_note = error.get('note')
  217. if error_note is not None and '因版权原因无法观看此视频' in error_note:
  218. raise ExtractorError(
  219. 'Youku said: Sorry, this video is available in China only', expected=True)
  220. elif error_note and '该视频被设为私密' in error_note:
  221. raise ExtractorError(
  222. 'Youku said: Sorry, this video is private', expected=True)
  223. else:
  224. msg = 'Youku server reported error %i' % error.get('code')
  225. if error_note is not None:
  226. msg += ': ' + error_note
  227. raise ExtractorError(msg)
  228. # get video title
  229. title = data['video']['title']
  230. # generate video_urls_dict
  231. video_urls_dict = self.construct_video_urls(data)
  232. # construct info
  233. entries = [{
  234. 'id': '%s_part%d' % (video_id, i + 1),
  235. 'title': title,
  236. 'formats': [],
  237. # some formats are not available for all parts, we have to detect
  238. # which one has all
  239. } for i in range(max(len(v.get('segs')) for v in data['stream']))]
  240. for stream in data['stream']:
  241. if stream.get('channel_type') == 'tail':
  242. continue
  243. fm = stream.get('stream_type')
  244. video_urls = video_urls_dict[fm]
  245. for video_url, seg, entry in zip(video_urls, stream['segs'], entries):
  246. entry['formats'].append({
  247. 'url': video_url,
  248. 'format_id': self.get_format_name(fm),
  249. 'ext': self.parse_ext_l(fm),
  250. 'filesize': int(seg['size']),
  251. 'width': stream.get('width'),
  252. 'height': stream.get('height'),
  253. })
  254. return {
  255. '_type': 'multi_video',
  256. 'id': video_id,
  257. 'title': title,
  258. 'entries': entries,
  259. }
  260. class YoukuShowIE(InfoExtractor):
  261. _VALID_URL = r'https?://(?:www\.)?youku\.com/show_page/id_(?P<id>[0-9a-z]+)\.html'
  262. IE_NAME = 'youku:show'
  263. _TEST = {
  264. 'url': 'http://www.youku.com/show_page/id_zc7c670be07ff11e48b3f.html',
  265. 'info_dict': {
  266. 'id': 'zc7c670be07ff11e48b3f',
  267. 'title': '花千骨 未删减版',
  268. 'description': 'md5:578d4f2145ae3f9128d9d4d863312910',
  269. },
  270. 'playlist_count': 50,
  271. }
  272. _PAGE_SIZE = 40
  273. def _find_videos_in_page(self, webpage):
  274. videos = re.findall(
  275. r'<li><a[^>]+href="(?P<url>https?://v\.youku\.com/[^"]+)"[^>]+title="(?P<title>[^"]+)"', webpage)
  276. return [
  277. self.url_result(video_url, YoukuIE.ie_key(), title)
  278. for video_url, title in videos]
  279. def _real_extract(self, url):
  280. show_id = self._match_id(url)
  281. webpage = self._download_webpage(url, show_id)
  282. entries = self._find_videos_in_page(webpage)
  283. playlist_title = self._html_search_regex(
  284. r'<span[^>]+class="name">([^<]+)</span>', webpage, 'playlist title', fatal=False)
  285. detail_div = get_element_by_attribute('class', 'detail', webpage) or ''
  286. playlist_description = self._html_search_regex(
  287. r'<span[^>]+style="display:none"[^>]*>([^<]+)</span>',
  288. detail_div, 'playlist description', fatal=False)
  289. for idx in itertools.count(1):
  290. episodes_page = self._download_webpage(
  291. 'http://www.youku.com/show_episode/id_%s.html' % show_id,
  292. show_id, query={'divid': 'reload_%d' % (idx * self._PAGE_SIZE + 1)},
  293. note='Downloading episodes page %d' % idx)
  294. new_entries = self._find_videos_in_page(episodes_page)
  295. entries.extend(new_entries)
  296. if len(new_entries) < self._PAGE_SIZE:
  297. break
  298. return self.playlist_result(entries, show_id, playlist_title, playlist_description)