]> git.bitcoin.ninja Git - youtube-dl/blob - youku.py
996d384784cb827ed4baa3304b61782542ea0767
[youtube-dl] / youku.py
1 # coding: utf-8
2
3 import json
4 import math
5 import random
6 import re
7 import time
8
9 from .common import InfoExtractor
10 from ..utils import (
11     ExtractorError,
12 )
13
14
15 class YoukuIE(InfoExtractor):
16     _VALID_URL =  r'(?:http://)?(v|player)\.youku\.com/(v_show/id_|player\.php/sid/)(?P<ID>[A-Za-z0-9]+)(\.html|/v.swf)'
17     _TEST =   {
18         u"url": u"http://v.youku.com/v_show/id_XNDgyMDQ2NTQw.html",
19         u"file": u"XNDgyMDQ2NTQw_part00.flv",
20         u"md5": u"ffe3f2e435663dc2d1eea34faeff5b5b",
21         u"params": { u"test": False },
22         u"info_dict": {
23             u"title": u"youtube-dl test video \"'/\\ä↭𝕐"
24         }
25     }
26
27
28     def _gen_sid(self):
29         nowTime = int(time.time() * 1000)
30         random1 = random.randint(1000,1998)
31         random2 = random.randint(1000,9999)
32
33         return "%d%d%d" %(nowTime,random1,random2)
34
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
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
56     def _real_extract(self, url):
57         mobj = re.match(self._VALID_URL, url)
58         if mobj is None:
59             raise ExtractorError(u'Invalid URL: %s' % url)
60         video_id = mobj.group('ID')
61
62         info_url = 'http://v.youku.com/player/getPlayList/VideoIDS/' + video_id
63
64         jsondata = self._download_webpage(info_url, video_id)
65
66         self.report_extraction(video_id)
67         try:
68             config = json.loads(jsondata)
69
70             video_title =  config['data'][0]['title']
71             seed = config['data'][0]['seed']
72
73             format = self._downloader.params.get('format', None)
74             supported_format = list(config['data'][0]['streamfileids'].keys())
75
76             if format is None or format == 'best':
77                 if 'hd2' in supported_format:
78                     format = 'hd2'
79                 else:
80                     format = 'flv'
81                 ext = u'flv'
82             elif format == 'worst':
83                 format = 'mp4'
84                 ext = u'mp4'
85             else:
86                 format = 'flv'
87                 ext = u'flv'
88
89
90             fileid = config['data'][0]['streamfileids'][format]
91             keys = [s['k'] for s in config['data'][0]['segs'][format]]
92         except (UnicodeDecodeError, ValueError, KeyError):
93             raise ExtractorError(u'Unable to extract info section')
94
95         files_info=[]
96         sid = self._gen_sid()
97         fileid = self._get_file_id(fileid, seed)
98
99         #column 8,9 of fileid represent the segment number
100         #fileid[7:9] should be changed
101         for index, key in enumerate(keys):
102
103             temp_fileid = '%s%02X%s' % (fileid[0:8], index, fileid[10:])
104             download_url = 'http://f.youku.com/player/getFlvPath/sid/%s_%02X/st/flv/fileid/%s?k=%s' % (sid, index, temp_fileid, key)
105
106             info = {
107                 'id': '%s_part%02d' % (video_id, index),
108                 'url': download_url,
109                 'uploader': None,
110                 'upload_date': None,
111                 'title': video_title,
112                 'ext': ext,
113             }
114             files_info.append(info)
115
116         return files_info