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
3.8 KiB

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