logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

youku.py (11404B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import random
  4. import re
  5. import string
  6. import time
  7. from .common import InfoExtractor
  8. from ..utils import (
  9. ExtractorError,
  10. get_element_by_class,
  11. js_to_json,
  12. str_or_none,
  13. strip_jsonp,
  14. )
  15. class YoukuIE(InfoExtractor):
  16. IE_NAME = 'youku'
  17. IE_DESC = '优酷'
  18. _VALID_URL = r'''(?x)
  19. (?:
  20. https?://(
  21. (?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)|
  22. video\.tudou\.com/v/)|
  23. youku:)
  24. (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|)
  25. '''
  26. _TESTS = [{
  27. # MD5 is unstable
  28. 'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html',
  29. 'info_dict': {
  30. 'id': 'XMTc1ODE5Njcy',
  31. 'title': '★Smile﹗♡ Git Fresh -Booty Music舞蹈.',
  32. 'ext': 'mp4',
  33. 'duration': 74.73,
  34. 'thumbnail': r're:^https?://.*',
  35. 'uploader': '。躲猫猫、',
  36. 'uploader_id': '36017967',
  37. 'uploader_url': 'http://i.youku.com/u/UMTQ0MDcxODY4',
  38. 'tags': list,
  39. }
  40. }, {
  41. 'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf',
  42. 'only_matching': True,
  43. }, {
  44. 'url': 'http://v.youku.com/v_show/id_XODgxNjg1Mzk2_ev_1.html',
  45. 'info_dict': {
  46. 'id': 'XODgxNjg1Mzk2',
  47. 'ext': 'mp4',
  48. 'title': '武媚娘传奇 85',
  49. 'duration': 1999.61,
  50. 'thumbnail': r're:^https?://.*',
  51. 'uploader': '疯狂豆花',
  52. 'uploader_id': '62583473',
  53. 'uploader_url': 'http://i.youku.com/u/UMjUwMzMzODky',
  54. 'tags': list,
  55. },
  56. }, {
  57. 'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html',
  58. 'info_dict': {
  59. 'id': 'XMTI1OTczNDM5Mg',
  60. 'ext': 'mp4',
  61. 'title': '花千骨 04',
  62. 'duration': 2363,
  63. 'thumbnail': r're:^https?://.*',
  64. 'uploader': '放剧场-花千骨',
  65. 'uploader_id': '772849359',
  66. 'uploader_url': 'http://i.youku.com/u/UMzA5MTM5NzQzNg==',
  67. 'tags': list,
  68. },
  69. }, {
  70. 'url': 'http://v.youku.com/v_show/id_XNjA1NzA2Njgw.html',
  71. 'note': 'Video protected with password',
  72. 'info_dict': {
  73. 'id': 'XNjA1NzA2Njgw',
  74. 'ext': 'mp4',
  75. 'title': '邢義田复旦讲座之想象中的胡人—从“左衽孔子”说起',
  76. 'duration': 7264.5,
  77. 'thumbnail': r're:^https?://.*',
  78. 'uploader': 'FoxJin1006',
  79. 'uploader_id': '322014285',
  80. 'uploader_url': 'http://i.youku.com/u/UMTI4ODA1NzE0MA==',
  81. 'tags': list,
  82. },
  83. 'params': {
  84. 'videopassword': '100600',
  85. },
  86. }, {
  87. # /play/get.json contains streams with "channel_type":"tail"
  88. 'url': 'http://v.youku.com/v_show/id_XOTUxMzg4NDMy.html',
  89. 'info_dict': {
  90. 'id': 'XOTUxMzg4NDMy',
  91. 'ext': 'mp4',
  92. 'title': '我的世界☆明月庄主☆车震猎杀☆杀人艺术Minecraft',
  93. 'duration': 702.08,
  94. 'thumbnail': r're:^https?://.*',
  95. 'uploader': '明月庄主moon',
  96. 'uploader_id': '38465621',
  97. 'uploader_url': 'http://i.youku.com/u/UMTUzODYyNDg0',
  98. 'tags': list,
  99. },
  100. }, {
  101. 'url': 'http://video.tudou.com/v/XMjIyNzAzMTQ4NA==.html?f=46177805',
  102. 'info_dict': {
  103. 'id': 'XMjIyNzAzMTQ4NA',
  104. 'ext': 'mp4',
  105. 'title': '卡马乔国足开大脚长传冲吊集锦',
  106. 'duration': 289,
  107. 'thumbnail': r're:^https?://.*',
  108. 'uploader': '阿卜杜拉之星',
  109. 'uploader_id': '2382249',
  110. 'uploader_url': 'http://i.youku.com/u/UOTUyODk5Ng==',
  111. 'tags': list,
  112. },
  113. }, {
  114. 'url': 'http://video.tudou.com/v/XMjE4ODI3OTg2MA==.html',
  115. 'only_matching': True,
  116. }]
  117. @staticmethod
  118. def get_ysuid():
  119. return '%d%s' % (int(time.time()), ''.join([
  120. random.choice(string.ascii_letters) for i in range(3)]))
  121. def get_format_name(self, fm):
  122. _dict = {
  123. '3gp': 'h6',
  124. '3gphd': 'h5',
  125. 'flv': 'h4',
  126. 'flvhd': 'h4',
  127. 'mp4': 'h3',
  128. 'mp4hd': 'h3',
  129. 'mp4hd2': 'h4',
  130. 'mp4hd3': 'h4',
  131. 'hd2': 'h2',
  132. 'hd3': 'h1',
  133. }
  134. return _dict.get(fm)
  135. def _real_extract(self, url):
  136. video_id = self._match_id(url)
  137. self._set_cookie('youku.com', '__ysuid', self.get_ysuid())
  138. self._set_cookie('youku.com', 'xreferrer', 'http://www.youku.com')
  139. _, urlh = self._download_webpage_handle(
  140. 'https://log.mmstat.com/eg.js', video_id, 'Retrieving cna info')
  141. # The etag header is '"foobar"'; let's remove the double quotes
  142. cna = urlh.headers['etag'][1:-1]
  143. # request basic data
  144. basic_data_params = {
  145. 'vid': video_id,
  146. 'ccode': '0532',
  147. 'client_ip': '192.168.1.1',
  148. 'utid': cna,
  149. 'client_ts': time.time() / 1000,
  150. }
  151. video_password = self._downloader.params.get('videopassword')
  152. if video_password:
  153. basic_data_params['password'] = video_password
  154. headers = {
  155. 'Referer': url,
  156. }
  157. headers.update(self.geo_verification_headers())
  158. data = self._download_json(
  159. 'https://ups.youku.com/ups/get.json', video_id,
  160. 'Downloading JSON metadata',
  161. query=basic_data_params, headers=headers)['data']
  162. error = data.get('error')
  163. if error:
  164. error_note = error.get('note')
  165. if error_note is not None and '因版权原因无法观看此视频' in error_note:
  166. raise ExtractorError(
  167. 'Youku said: Sorry, this video is available in China only', expected=True)
  168. elif error_note and '该视频被设为私密' in error_note:
  169. raise ExtractorError(
  170. 'Youku said: Sorry, this video is private', expected=True)
  171. else:
  172. msg = 'Youku server reported error %i' % error.get('code')
  173. if error_note is not None:
  174. msg += ': ' + error_note
  175. raise ExtractorError(msg)
  176. # get video title
  177. video_data = data['video']
  178. title = video_data['title']
  179. formats = [{
  180. 'url': stream['m3u8_url'],
  181. 'format_id': self.get_format_name(stream.get('stream_type')),
  182. 'ext': 'mp4',
  183. 'protocol': 'm3u8_native',
  184. 'filesize': int(stream.get('size')),
  185. 'width': stream.get('width'),
  186. 'height': stream.get('height'),
  187. } for stream in data['stream'] if stream.get('channel_type') != 'tail']
  188. self._sort_formats(formats)
  189. return {
  190. 'id': video_id,
  191. 'title': title,
  192. 'formats': formats,
  193. 'duration': video_data.get('seconds'),
  194. 'thumbnail': video_data.get('logo'),
  195. 'uploader': video_data.get('username'),
  196. 'uploader_id': str_or_none(video_data.get('userid')),
  197. 'uploader_url': data.get('uploader', {}).get('homepage'),
  198. 'tags': video_data.get('tags'),
  199. }
  200. class YoukuShowIE(InfoExtractor):
  201. _VALID_URL = r'https?://list\.youku\.com/show/id_(?P<id>[0-9a-z]+)\.html'
  202. IE_NAME = 'youku:show'
  203. _TESTS = [{
  204. 'url': 'http://list.youku.com/show/id_zc7c670be07ff11e48b3f.html',
  205. 'info_dict': {
  206. 'id': 'zc7c670be07ff11e48b3f',
  207. 'title': '花千骨 DVD版',
  208. 'description': 'md5:a1ae6f5618571bbeb5c9821f9c81b558',
  209. },
  210. 'playlist_count': 50,
  211. }, {
  212. # Episode number not starting from 1
  213. 'url': 'http://list.youku.com/show/id_zefbfbd70efbfbd780bef.html',
  214. 'info_dict': {
  215. 'id': 'zefbfbd70efbfbd780bef',
  216. 'title': '超级飞侠3',
  217. 'description': 'md5:275715156abebe5ccc2a1992e9d56b98',
  218. },
  219. 'playlist_count': 24,
  220. }, {
  221. # Ongoing playlist. The initial page is the last one
  222. 'url': 'http://list.youku.com/show/id_za7c275ecd7b411e1a19e.html',
  223. 'only_matching': True,
  224. }, {
  225. # No data-id value.
  226. 'url': 'http://list.youku.com/show/id_zefbfbd61237fefbfbdef.html',
  227. 'only_matching': True,
  228. }, {
  229. # Wrong number of reload_id.
  230. 'url': 'http://list.youku.com/show/id_z20eb4acaf5c211e3b2ad.html',
  231. 'only_matching': True,
  232. }]
  233. def _extract_entries(self, playlist_data_url, show_id, note, query):
  234. query['callback'] = 'cb'
  235. playlist_data = self._download_json(
  236. playlist_data_url, show_id, query=query, note=note,
  237. transform_source=lambda s: js_to_json(strip_jsonp(s))).get('html')
  238. if playlist_data is None:
  239. return [None, None]
  240. drama_list = (get_element_by_class('p-drama-grid', playlist_data)
  241. or get_element_by_class('p-drama-half-row', playlist_data))
  242. if drama_list is None:
  243. raise ExtractorError('No episodes found')
  244. video_urls = re.findall(r'<a[^>]+href="([^"]+)"', drama_list)
  245. return playlist_data, [
  246. self.url_result(self._proto_relative_url(video_url, 'http:'), YoukuIE.ie_key())
  247. for video_url in video_urls]
  248. def _real_extract(self, url):
  249. show_id = self._match_id(url)
  250. webpage = self._download_webpage(url, show_id)
  251. entries = []
  252. page_config = self._parse_json(self._search_regex(
  253. r'var\s+PageConfig\s*=\s*({.+});', webpage, 'page config'),
  254. show_id, transform_source=js_to_json)
  255. first_page, initial_entries = self._extract_entries(
  256. 'http://list.youku.com/show/module', show_id,
  257. note='Downloading initial playlist data page',
  258. query={
  259. 'id': page_config['showid'],
  260. 'tab': 'showInfo',
  261. })
  262. first_page_reload_id = self._html_search_regex(
  263. r'<div[^>]+id="(reload_\d+)', first_page, 'first page reload id')
  264. # The first reload_id has the same items as first_page
  265. reload_ids = re.findall('<li[^>]+data-id="([^"]+)">', first_page)
  266. entries.extend(initial_entries)
  267. for idx, reload_id in enumerate(reload_ids):
  268. if reload_id == first_page_reload_id:
  269. continue
  270. _, new_entries = self._extract_entries(
  271. 'http://list.youku.com/show/episode', show_id,
  272. note='Downloading playlist data page %d' % (idx + 1),
  273. query={
  274. 'id': page_config['showid'],
  275. 'stage': reload_id,
  276. })
  277. if new_entries is not None:
  278. entries.extend(new_entries)
  279. desc = self._html_search_meta('description', webpage, fatal=False)
  280. playlist_title = desc.split(',')[0] if desc else None
  281. detail_li = get_element_by_class('p-intro', webpage)
  282. playlist_description = get_element_by_class(
  283. 'intro-more', detail_li) if detail_li else None
  284. return self.playlist_result(
  285. entries, show_id, playlist_title, playlist_description)