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.

277 lines
9.6 KiB

Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import itertools
  4. import random
  5. import re
  6. import string
  7. import time
  8. from .common import InfoExtractor
  9. from ..utils import (
  10. ExtractorError,
  11. get_element_by_class,
  12. js_to_json,
  13. str_or_none,
  14. strip_jsonp,
  15. urljoin,
  16. )
  17. class YoukuIE(InfoExtractor):
  18. IE_NAME = 'youku'
  19. IE_DESC = '优酷'
  20. _VALID_URL = r'''(?x)
  21. (?:
  22. https?://(
  23. (?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)|
  24. video\.tudou\.com/v/)|
  25. youku:)
  26. (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|)
  27. '''
  28. _TESTS = [{
  29. # MD5 is unstable
  30. 'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html',
  31. 'info_dict': {
  32. 'id': 'XMTc1ODE5Njcy',
  33. 'title': '★Smile﹗♡ Git Fresh -Booty Music舞蹈.',
  34. 'ext': 'mp4',
  35. 'duration': 74.73,
  36. 'thumbnail': r're:^https?://.*',
  37. 'uploader': '。躲猫猫、',
  38. 'uploader_id': '36017967',
  39. 'uploader_url': 'http://i.youku.com/u/UMTQ0MDcxODY4',
  40. 'tags': list,
  41. }
  42. }, {
  43. 'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf',
  44. 'only_matching': True,
  45. }, {
  46. 'url': 'http://v.youku.com/v_show/id_XODgxNjg1Mzk2_ev_1.html',
  47. 'info_dict': {
  48. 'id': 'XODgxNjg1Mzk2',
  49. 'ext': 'mp4',
  50. 'title': '武媚娘传奇 85',
  51. 'duration': 1999.61,
  52. 'thumbnail': r're:^https?://.*',
  53. 'uploader': '疯狂豆花',
  54. 'uploader_id': '62583473',
  55. 'uploader_url': 'http://i.youku.com/u/UMjUwMzMzODky',
  56. 'tags': list,
  57. },
  58. }, {
  59. 'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html',
  60. 'info_dict': {
  61. 'id': 'XMTI1OTczNDM5Mg',
  62. 'ext': 'mp4',
  63. 'title': '花千骨 04',
  64. 'duration': 2363,
  65. 'thumbnail': r're:^https?://.*',
  66. 'uploader': '放剧场-花千骨',
  67. 'uploader_id': '772849359',
  68. 'uploader_url': 'http://i.youku.com/u/UMzA5MTM5NzQzNg==',
  69. 'tags': list,
  70. },
  71. }, {
  72. 'url': 'http://v.youku.com/v_show/id_XNjA1NzA2Njgw.html',
  73. 'note': 'Video protected with password',
  74. 'info_dict': {
  75. 'id': 'XNjA1NzA2Njgw',
  76. 'ext': 'mp4',
  77. 'title': '邢義田复旦讲座之想象中的胡人—从“左衽孔子”说起',
  78. 'duration': 7264.5,
  79. 'thumbnail': r're:^https?://.*',
  80. 'uploader': 'FoxJin1006',
  81. 'uploader_id': '322014285',
  82. 'uploader_url': 'http://i.youku.com/u/UMTI4ODA1NzE0MA==',
  83. 'tags': list,
  84. },
  85. 'params': {
  86. 'videopassword': '100600',
  87. },
  88. }, {
  89. # /play/get.json contains streams with "channel_type":"tail"
  90. 'url': 'http://v.youku.com/v_show/id_XOTUxMzg4NDMy.html',
  91. 'info_dict': {
  92. 'id': 'XOTUxMzg4NDMy',
  93. 'ext': 'mp4',
  94. 'title': '我的世界☆明月庄主☆车震猎杀☆杀人艺术Minecraft',
  95. 'duration': 702.08,
  96. 'thumbnail': r're:^https?://.*',
  97. 'uploader': '明月庄主moon',
  98. 'uploader_id': '38465621',
  99. 'uploader_url': 'http://i.youku.com/u/UMTUzODYyNDg0',
  100. 'tags': list,
  101. },
  102. }, {
  103. 'url': 'http://video.tudou.com/v/XMjIyNzAzMTQ4NA==.html?f=46177805',
  104. 'info_dict': {
  105. 'id': 'XMjIyNzAzMTQ4NA',
  106. 'ext': 'mp4',
  107. 'title': '卡马乔国足开大脚长传冲吊集锦',
  108. 'duration': 289,
  109. 'thumbnail': r're:^https?://.*',
  110. 'uploader': '阿卜杜拉之星',
  111. 'uploader_id': '2382249',
  112. 'uploader_url': 'http://i.youku.com/u/UOTUyODk5Ng==',
  113. 'tags': list,
  114. },
  115. }, {
  116. 'url': 'http://video.tudou.com/v/XMjE4ODI3OTg2MA==.html',
  117. 'only_matching': True,
  118. }]
  119. @staticmethod
  120. def get_ysuid():
  121. return '%d%s' % (int(time.time()), ''.join([
  122. random.choice(string.ascii_letters) for i in range(3)]))
  123. def get_format_name(self, fm):
  124. _dict = {
  125. '3gp': 'h6',
  126. '3gphd': 'h5',
  127. 'flv': 'h4',
  128. 'flvhd': 'h4',
  129. 'mp4': 'h3',
  130. 'mp4hd': 'h3',
  131. 'mp4hd2': 'h4',
  132. 'mp4hd3': 'h4',
  133. 'hd2': 'h2',
  134. 'hd3': 'h1',
  135. }
  136. return _dict.get(fm)
  137. def _real_extract(self, url):
  138. video_id = self._match_id(url)
  139. self._set_cookie('youku.com', '__ysuid', self.get_ysuid())
  140. self._set_cookie('youku.com', 'xreferrer', 'http://www.youku.com')
  141. _, urlh = self._download_webpage_handle(
  142. 'https://log.mmstat.com/eg.js', video_id, 'Retrieving cna info')
  143. # The etag header is '"foobar"'; let's remove the double quotes
  144. cna = urlh.headers['etag'][1:-1]
  145. # request basic data
  146. basic_data_params = {
  147. 'vid': video_id,
  148. 'ccode': '0402' if 'tudou.com' in url else '0401',
  149. 'client_ip': '192.168.1.1',
  150. 'utid': cna,
  151. 'client_ts': time.time() / 1000,
  152. }
  153. video_password = self._downloader.params.get('videopassword')
  154. if video_password:
  155. basic_data_params['password'] = video_password
  156. headers = {
  157. 'Referer': url,
  158. }
  159. headers.update(self.geo_verification_headers())
  160. data = self._download_json(
  161. 'https://ups.youku.com/ups/get.json', video_id,
  162. 'Downloading JSON metadata',
  163. query=basic_data_params, headers=headers)['data']
  164. error = data.get('error')
  165. if error:
  166. error_note = error.get('note')
  167. if error_note is not None and '因版权原因无法观看此视频' in error_note:
  168. raise ExtractorError(
  169. 'Youku said: Sorry, this video is available in China only', expected=True)
  170. elif error_note and '该视频被设为私密' in error_note:
  171. raise ExtractorError(
  172. 'Youku said: Sorry, this video is private', expected=True)
  173. else:
  174. msg = 'Youku server reported error %i' % error.get('code')
  175. if error_note is not None:
  176. msg += ': ' + error_note
  177. raise ExtractorError(msg)
  178. # get video title
  179. video_data = data['video']
  180. title = video_data['title']
  181. formats = [{
  182. 'url': stream['m3u8_url'],
  183. 'format_id': self.get_format_name(stream.get('stream_type')),
  184. 'ext': 'mp4',
  185. 'protocol': 'm3u8_native',
  186. 'filesize': int(stream.get('size')),
  187. 'width': stream.get('width'),
  188. 'height': stream.get('height'),
  189. } for stream in data['stream'] if stream.get('channel_type') != 'tail']
  190. self._sort_formats(formats)
  191. return {
  192. 'id': video_id,
  193. 'title': title,
  194. 'formats': formats,
  195. 'duration': video_data.get('seconds'),
  196. 'thumbnail': video_data.get('logo'),
  197. 'uploader': video_data.get('username'),
  198. 'uploader_id': str_or_none(video_data.get('userid')),
  199. 'uploader_url': data.get('uploader', {}).get('homepage'),
  200. 'tags': video_data.get('tags'),
  201. }
  202. class YoukuShowIE(InfoExtractor):
  203. _VALID_URL = r'https?://list\.youku\.com/show/id_(?P<id>[0-9a-z]+)\.html'
  204. IE_NAME = 'youku:show'
  205. _TEST = {
  206. 'url': 'http://list.youku.com/show/id_zc7c670be07ff11e48b3f.html',
  207. 'info_dict': {
  208. 'id': 'zc7c670be07ff11e48b3f',
  209. 'title': '花千骨 未删减版',
  210. 'description': 'md5:a1ae6f5618571bbeb5c9821f9c81b558',
  211. },
  212. 'playlist_count': 50,
  213. }
  214. _PAGE_SIZE = 40
  215. def _real_extract(self, url):
  216. show_id = self._match_id(url)
  217. webpage = self._download_webpage(url, show_id)
  218. entries = []
  219. page_config = self._parse_json(self._search_regex(
  220. r'var\s+PageConfig\s*=\s*({.+});', webpage, 'page config'),
  221. show_id, transform_source=js_to_json)
  222. for idx in itertools.count(0):
  223. if idx == 0:
  224. playlist_data_url = 'http://list.youku.com/show/module'
  225. query = {'id': page_config['showid'], 'tab': 'point'}
  226. else:
  227. playlist_data_url = 'http://list.youku.com/show/point'
  228. query = {
  229. 'id': page_config['showid'],
  230. 'stage': 'reload_%d' % (self._PAGE_SIZE * idx + 1),
  231. }
  232. query['callback'] = 'cb'
  233. playlist_data = self._download_json(
  234. playlist_data_url, show_id, query=query,
  235. note='Downloading playlist data page %d' % (idx + 1),
  236. transform_source=lambda s: js_to_json(strip_jsonp(s)))['html']
  237. video_urls = re.findall(
  238. r'<div[^>]+class="p-thumb"[^<]+<a[^>]+href="([^"]+)"',
  239. playlist_data)
  240. new_entries = [
  241. self.url_result(urljoin(url, video_url), YoukuIE.ie_key())
  242. for video_url in video_urls]
  243. entries.extend(new_entries)
  244. if len(new_entries) < self._PAGE_SIZE:
  245. break
  246. desc = self._html_search_meta('description', webpage, fatal=False)
  247. playlist_title = desc.split(',')[0] if desc else None
  248. detail_li = get_element_by_class('p-intro', webpage)
  249. playlist_description = get_element_by_class(
  250. 'intro-more', detail_li) if detail_li else None
  251. return self.playlist_result(
  252. entries, show_id, playlist_title, playlist_description)