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.

124 lines
3.8 KiB

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