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.

258 lines
8.0 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
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. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_urllib_parse,
  7. compat_ord,
  8. )
  9. from ..utils import (
  10. ExtractorError,
  11. sanitized_Request,
  12. )
  13. class YoukuIE(InfoExtractor):
  14. IE_NAME = 'youku'
  15. IE_DESC = '优酷'
  16. _VALID_URL = r'''(?x)
  17. (?:
  18. http://(?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)|
  19. youku:)
  20. (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|)
  21. '''
  22. _TESTS = [{
  23. # MD5 is unstable
  24. 'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html',
  25. 'info_dict': {
  26. 'id': 'XMTc1ODE5Njcy_part1',
  27. 'title': '★Smile﹗♡ Git Fresh -Booty Music舞蹈.',
  28. 'ext': 'flv'
  29. }
  30. }, {
  31. 'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf',
  32. 'only_matching': True,
  33. }, {
  34. 'url': 'http://v.youku.com/v_show/id_XODgxNjg1Mzk2_ev_1.html',
  35. 'info_dict': {
  36. 'id': 'XODgxNjg1Mzk2',
  37. 'title': '武媚娘传奇 85',
  38. },
  39. 'playlist_count': 11,
  40. 'skip': 'Available in China only',
  41. }, {
  42. 'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html',
  43. 'info_dict': {
  44. 'id': 'XMTI1OTczNDM5Mg',
  45. 'title': '花千骨 04',
  46. },
  47. 'playlist_count': 13,
  48. }, {
  49. 'url': 'http://v.youku.com/v_show/id_XNjA1NzA2Njgw.html',
  50. 'note': 'Video protected with password',
  51. 'info_dict': {
  52. 'id': 'XNjA1NzA2Njgw',
  53. 'title': '邢義田复旦讲座之想象中的胡人—从“左衽孔子”说起',
  54. },
  55. 'playlist_count': 19,
  56. 'params': {
  57. 'videopassword': '100600',
  58. },
  59. }]
  60. def construct_video_urls(self, data):
  61. # get sid, token
  62. def yk_t(s1, s2):
  63. ls = list(range(256))
  64. t = 0
  65. for i in range(256):
  66. t = (t + ls[i] + compat_ord(s1[i % len(s1)])) % 256
  67. ls[i], ls[t] = ls[t], ls[i]
  68. s = bytearray()
  69. x, y = 0, 0
  70. for i in range(len(s2)):
  71. y = (y + 1) % 256
  72. x = (x + ls[y]) % 256
  73. ls[x], ls[y] = ls[y], ls[x]
  74. s.append(compat_ord(s2[i]) ^ ls[(ls[x] + ls[y]) % 256])
  75. return bytes(s)
  76. sid, token = yk_t(
  77. b'becaf9be', base64.b64decode(data['security']['encrypt_string'].encode('ascii'))
  78. ).decode('ascii').split('_')
  79. # get oip
  80. oip = data['security']['ip']
  81. fileid_dict = {}
  82. for stream in data['stream']:
  83. format = stream.get('stream_type')
  84. fileid = stream['stream_fileid']
  85. fileid_dict[format] = fileid
  86. def get_fileid(format, n):
  87. number = hex(int(str(n), 10))[2:].upper()
  88. if len(number) == 1:
  89. number = '0' + number
  90. streamfileids = fileid_dict[format]
  91. fileid = streamfileids[0:8] + number + streamfileids[10:]
  92. return fileid
  93. # get ep
  94. def generate_ep(format, n):
  95. fileid = get_fileid(format, n)
  96. ep_t = yk_t(
  97. b'bf7e5f01',
  98. ('%s_%s_%s' % (sid, fileid, token)).encode('ascii')
  99. )
  100. ep = base64.b64encode(ep_t).decode('ascii')
  101. return ep
  102. # generate video_urls
  103. video_urls_dict = {}
  104. for stream in data['stream']:
  105. format = stream.get('stream_type')
  106. video_urls = []
  107. for dt in stream['segs']:
  108. n = str(stream['segs'].index(dt))
  109. param = {
  110. 'K': dt['key'],
  111. 'hd': self.get_hd(format),
  112. 'myp': 0,
  113. 'ypp': 0,
  114. 'ctype': 12,
  115. 'ev': 1,
  116. 'token': token,
  117. 'oip': oip,
  118. 'ep': generate_ep(format, n)
  119. }
  120. video_url = \
  121. 'http://k.youku.com/player/getFlvPath/' + \
  122. 'sid/' + sid + \
  123. '_00' + \
  124. '/st/' + self.parse_ext_l(format) + \
  125. '/fileid/' + get_fileid(format, n) + '?' + \
  126. compat_urllib_parse.urlencode(param)
  127. video_urls.append(video_url)
  128. video_urls_dict[format] = video_urls
  129. return video_urls_dict
  130. def get_hd(self, fm):
  131. hd_id_dict = {
  132. '3gp': '0',
  133. '3gphd': '1',
  134. 'flv': '0',
  135. 'flvhd': '0',
  136. 'mp4': '1',
  137. 'mp4hd': '1',
  138. 'mp4hd2': '1',
  139. 'mp4hd3': '1',
  140. 'hd2': '2',
  141. 'hd3': '3',
  142. }
  143. return hd_id_dict[fm]
  144. def parse_ext_l(self, fm):
  145. ext_dict = {
  146. '3gp': 'flv',
  147. '3gphd': 'mp4',
  148. 'flv': 'flv',
  149. 'flvhd': 'flv',
  150. 'mp4': 'mp4',
  151. 'mp4hd': 'mp4',
  152. 'mp4hd2': 'flv',
  153. 'mp4hd3': 'flv',
  154. 'hd2': 'flv',
  155. 'hd3': 'flv',
  156. }
  157. return ext_dict[fm]
  158. def get_format_name(self, fm):
  159. _dict = {
  160. '3gp': 'h6',
  161. '3gphd': 'h5',
  162. 'flv': 'h4',
  163. 'flvhd': 'h4',
  164. 'mp4': 'h3',
  165. 'mp4hd': 'h3',
  166. 'mp4hd2': 'h4',
  167. 'mp4hd3': 'h4',
  168. 'hd2': 'h2',
  169. 'hd3': 'h1',
  170. }
  171. return _dict[fm]
  172. def _real_extract(self, url):
  173. video_id = self._match_id(url)
  174. def retrieve_data(req_url, note):
  175. headers = {
  176. 'Referer': req_url,
  177. }
  178. self._set_cookie('youku.com', 'xreferrer', 'http://www.youku.com')
  179. req = sanitized_Request(req_url, headers=headers)
  180. cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
  181. if cn_verification_proxy:
  182. req.add_header('Ytdl-request-proxy', cn_verification_proxy)
  183. raw_data = self._download_json(req, video_id, note=note)
  184. return raw_data['data']
  185. video_password = self._downloader.params.get('videopassword', None)
  186. # request basic data
  187. basic_data_url = "http://play.youku.com/play/get.json?vid=%s&ct=12" % video_id
  188. if video_password:
  189. basic_data_url += '&pwd=%s' % video_password
  190. data = retrieve_data(basic_data_url, 'Downloading JSON metadata')
  191. error = data.get('error')
  192. if error:
  193. error_note = error.get('note')
  194. if error_note is not None and '因版权原因无法观看此视频' in error_note:
  195. raise ExtractorError(
  196. 'Youku said: Sorry, this video is available in China only', expected=True)
  197. else:
  198. msg = 'Youku server reported error %i' % error.get('code')
  199. if error_note is not None:
  200. msg += ': ' + error_note
  201. raise ExtractorError(msg)
  202. # get video title
  203. title = data['video']['title']
  204. # generate video_urls_dict
  205. video_urls_dict = self.construct_video_urls(data)
  206. # construct info
  207. entries = [{
  208. 'id': '%s_part%d' % (video_id, i + 1),
  209. 'title': title,
  210. 'formats': [],
  211. # some formats are not available for all parts, we have to detect
  212. # which one has all
  213. } for i in range(max(len(v.get('segs')) for v in data['stream']))]
  214. for stream in data['stream']:
  215. fm = stream.get('stream_type')
  216. video_urls = video_urls_dict[fm]
  217. for video_url, seg, entry in zip(video_urls, stream['segs'], entries):
  218. entry['formats'].append({
  219. 'url': video_url,
  220. 'format_id': self.get_format_name(fm),
  221. 'ext': self.parse_ext_l(fm),
  222. 'filesize': int(seg['size']),
  223. })
  224. return {
  225. '_type': 'multi_video',
  226. 'id': video_id,
  227. 'title': title,
  228. 'entries': entries,
  229. }