]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/youku.py
Imported Upstream version 2013.06.26
[youtubedl] / youtube_dl / extractor / youku.py
1 import json
2 import math
3 import random
4 import re
5 import time
6
7 from .common import InfoExtractor
8 from ..utils import (
9 ExtractorError,
10 )
11
12
13 class YoukuIE(InfoExtractor):
14 _VALID_URL = r'(?:http://)?v\.youku\.com/v_show/id_(?P<ID>[A-Za-z0-9]+)\.html'
15
16 def _gen_sid(self):
17 nowTime = int(time.time() * 1000)
18 random1 = random.randint(1000,1998)
19 random2 = random.randint(1000,9999)
20
21 return "%d%d%d" %(nowTime,random1,random2)
22
23 def _get_file_ID_mix_string(self, seed):
24 mixed = []
25 source = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\:._-1234567890")
26 seed = float(seed)
27 for i in range(len(source)):
28 seed = (seed * 211 + 30031 ) % 65536
29 index = math.floor(seed / 65536 * len(source) )
30 mixed.append(source[int(index)])
31 source.remove(source[int(index)])
32 #return ''.join(mixed)
33 return mixed
34
35 def _get_file_id(self, fileId, seed):
36 mixed = self._get_file_ID_mix_string(seed)
37 ids = fileId.split('*')
38 realId = []
39 for ch in ids:
40 if ch:
41 realId.append(mixed[int(ch)])
42 return ''.join(realId)
43
44 def _real_extract(self, url):
45 mobj = re.match(self._VALID_URL, url)
46 if mobj is None:
47 raise ExtractorError(u'Invalid URL: %s' % url)
48 video_id = mobj.group('ID')
49
50 info_url = 'http://v.youku.com/player/getPlayList/VideoIDS/' + video_id
51
52 jsondata = self._download_webpage(info_url, video_id)
53
54 self.report_extraction(video_id)
55 try:
56 config = json.loads(jsondata)
57
58 video_title = config['data'][0]['title']
59 seed = config['data'][0]['seed']
60
61 format = self._downloader.params.get('format', None)
62 supported_format = list(config['data'][0]['streamfileids'].keys())
63
64 if format is None or format == 'best':
65 if 'hd2' in supported_format:
66 format = 'hd2'
67 else:
68 format = 'flv'
69 ext = u'flv'
70 elif format == 'worst':
71 format = 'mp4'
72 ext = u'mp4'
73 else:
74 format = 'flv'
75 ext = u'flv'
76
77
78 fileid = config['data'][0]['streamfileids'][format]
79 keys = [s['k'] for s in config['data'][0]['segs'][format]]
80 except (UnicodeDecodeError, ValueError, KeyError):
81 raise ExtractorError(u'Unable to extract info section')
82
83 files_info=[]
84 sid = self._gen_sid()
85 fileid = self._get_file_id(fileid, seed)
86
87 #column 8,9 of fileid represent the segment number
88 #fileid[7:9] should be changed
89 for index, key in enumerate(keys):
90
91 temp_fileid = '%s%02X%s' % (fileid[0:8], index, fileid[10:])
92 download_url = 'http://f.youku.com/player/getFlvPath/sid/%s_%02X/st/flv/fileid/%s?k=%s' % (sid, index, temp_fileid, key)
93
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
104 return files_info