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.

116 lines
3.6 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. video_title = config['data'][0]['title']
  57. seed = config['data'][0]['seed']
  58. format = self._downloader.params.get('format', None)
  59. supported_format = list(config['data'][0]['streamfileids'].keys())
  60. if format is None or format == 'best':
  61. if 'hd2' in supported_format:
  62. format = 'hd2'
  63. else:
  64. format = 'flv'
  65. ext = u'flv'
  66. elif format == 'worst':
  67. format = 'mp4'
  68. ext = u'mp4'
  69. else:
  70. format = 'flv'
  71. ext = u'flv'
  72. fileid = config['data'][0]['streamfileids'][format]
  73. keys = [s['k'] for s in config['data'][0]['segs'][format]]
  74. except (UnicodeDecodeError, ValueError, KeyError):
  75. raise ExtractorError(u'Unable to extract info section')
  76. files_info=[]
  77. sid = self._gen_sid()
  78. fileid = self._get_file_id(fileid, seed)
  79. #column 8,9 of fileid represent the segment number
  80. #fileid[7:9] should be changed
  81. for index, key in enumerate(keys):
  82. temp_fileid = '%s%02X%s' % (fileid[0:8], index, fileid[10:])
  83. download_url = 'http://f.youku.com/player/getFlvPath/sid/%s_%02X/st/flv/fileid/%s?k=%s' % (sid, index, temp_fileid, key)
  84. info = {
  85. 'id': '%s_part%02d' % (video_id, index),
  86. 'url': download_url,
  87. 'uploader': None,
  88. 'upload_date': None,
  89. 'title': video_title,
  90. 'ext': ext,
  91. }
  92. files_info.append(info)
  93. return files_info