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.

253 lines
8.1 KiB

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