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.

123 lines
4.1 KiB

  1. # coding: utf-8
  2. import json
  3. import math
  4. import random
  5. import re
  6. import time
  7. from .common import InfoExtractor
  8. from ..utils import (
  9. ExtractorError,
  10. )
  11. class YoukuIE(InfoExtractor):
  12. _VALID_URL = r'(?:http://)?(v|player)\.youku\.com/(v_show/id_|player\.php/sid/)(?P<ID>[A-Za-z0-9]+)(\.html|/v.swf)'
  13. _TEST = {
  14. u"url": u"http://v.youku.com/v_show/id_XNDgyMDQ2NTQw.html",
  15. u"file": u"XNDgyMDQ2NTQw_part00.flv",
  16. u"md5": u"ffe3f2e435663dc2d1eea34faeff5b5b",
  17. u"params": { u"test": False },
  18. u"info_dict": {
  19. u"title": u"youtube-dl test video \"'/\\ä↭𝕐"
  20. }
  21. }
  22. def _gen_sid(self):
  23. nowTime = int(time.time() * 1000)
  24. random1 = random.randint(1000,1998)
  25. random2 = random.randint(1000,9999)
  26. return "%d%d%d" %(nowTime,random1,random2)
  27. def _get_file_ID_mix_string(self, seed):
  28. mixed = []
  29. source = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\:._-1234567890")
  30. seed = float(seed)
  31. for i in range(len(source)):
  32. seed = (seed * 211 + 30031 ) % 65536
  33. index = math.floor(seed / 65536 * len(source) )
  34. mixed.append(source[int(index)])
  35. source.remove(source[int(index)])
  36. #return ''.join(mixed)
  37. return mixed
  38. def _get_file_id(self, fileId, seed):
  39. mixed = self._get_file_ID_mix_string(seed)
  40. ids = fileId.split('*')
  41. realId = []
  42. for ch in ids:
  43. if ch:
  44. realId.append(mixed[int(ch)])
  45. return ''.join(realId)
  46. def _real_extract(self, url):
  47. mobj = re.match(self._VALID_URL, url)
  48. if mobj is None:
  49. raise ExtractorError(u'Invalid URL: %s' % url)
  50. video_id = mobj.group('ID')
  51. info_url = 'http://v.youku.com/player/getPlayList/VideoIDS/' + video_id
  52. jsondata = self._download_webpage(info_url, video_id)
  53. self.report_extraction(video_id)
  54. try:
  55. config = json.loads(jsondata)
  56. error_code = config['data'][0].get('error_code')
  57. if error_code:
  58. # -8 means blocked outside China.
  59. error = config['data'][0].get('error') # Chinese and English, separated by newline.
  60. raise ExtractorError(error or u'Server reported error %i' % error_code,
  61. expected=True)
  62. video_title = config['data'][0]['title']
  63. seed = config['data'][0]['seed']
  64. format = self._downloader.params.get('format', None)
  65. supported_format = list(config['data'][0]['streamfileids'].keys())
  66. if format is None or format == 'best':
  67. if 'hd2' in supported_format:
  68. format = 'hd2'
  69. else:
  70. format = 'flv'
  71. ext = u'flv'
  72. elif format == 'worst':
  73. format = 'mp4'
  74. ext = u'mp4'
  75. else:
  76. format = 'flv'
  77. ext = u'flv'
  78. fileid = config['data'][0]['streamfileids'][format]
  79. keys = [s['k'] for s in config['data'][0]['segs'][format]]
  80. # segs is usually a dictionary, but an empty *list* if an error occured.
  81. except (UnicodeDecodeError, ValueError, KeyError):
  82. raise ExtractorError(u'Unable to extract info section')
  83. files_info=[]
  84. sid = self._gen_sid()
  85. fileid = self._get_file_id(fileid, seed)
  86. #column 8,9 of fileid represent the segment number
  87. #fileid[7:9] should be changed
  88. for index, key in enumerate(keys):
  89. temp_fileid = '%s%02X%s' % (fileid[0:8], index, fileid[10:])
  90. download_url = 'http://f.youku.com/player/getFlvPath/sid/%s_%02X/st/flv/fileid/%s?k=%s' % (sid, index, temp_fileid, key)
  91. info = {
  92. 'id': '%s_part%02d' % (video_id, index),
  93. 'url': download_url,
  94. 'uploader': None,
  95. 'upload_date': None,
  96. 'title': video_title,
  97. 'ext': ext,
  98. }
  99. files_info.append(info)
  100. return files_info