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.

285 lines
11 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
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
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
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. import datetime
  6. from .common import InfoExtractor
  7. from ..compat import (
  8. compat_urllib_parse_urlencode,
  9. compat_urlparse,
  10. )
  11. from ..utils import (
  12. ExtractorError,
  13. int_or_none,
  14. parse_duration,
  15. parse_iso8601,
  16. sanitized_Request,
  17. xpath_text,
  18. determine_ext,
  19. urlencode_postdata,
  20. )
  21. class NiconicoIE(InfoExtractor):
  22. IE_NAME = 'niconico'
  23. IE_DESC = 'ニコニコ動画'
  24. _TESTS = [{
  25. 'url': 'http://www.nicovideo.jp/watch/sm22312215',
  26. 'md5': 'd1a75c0823e2f629128c43e1212760f9',
  27. 'info_dict': {
  28. 'id': 'sm22312215',
  29. 'ext': 'mp4',
  30. 'title': 'Big Buck Bunny',
  31. 'uploader': 'takuya0301',
  32. 'uploader_id': '2698420',
  33. 'upload_date': '20131123',
  34. 'timestamp': 1385182762,
  35. 'description': '(c) copyright 2008, Blender Foundation / www.bigbuckbunny.org',
  36. 'duration': 33,
  37. },
  38. }, {
  39. # File downloaded with and without credentials are different, so omit
  40. # the md5 field
  41. 'url': 'http://www.nicovideo.jp/watch/nm14296458',
  42. 'info_dict': {
  43. 'id': 'nm14296458',
  44. 'ext': 'swf',
  45. 'title': '【鏡音リン】Dance on media【オリジナル】take2!',
  46. 'description': 'md5:689f066d74610b3b22e0f1739add0f58',
  47. 'uploader': 'りょうた',
  48. 'uploader_id': '18822557',
  49. 'upload_date': '20110429',
  50. 'timestamp': 1304065916,
  51. 'duration': 209,
  52. },
  53. }, {
  54. # 'video exists but is marked as "deleted"
  55. # md5 is unstable
  56. 'url': 'http://www.nicovideo.jp/watch/sm10000',
  57. 'info_dict': {
  58. 'id': 'sm10000',
  59. 'ext': 'unknown_video',
  60. 'description': 'deleted',
  61. 'title': 'ドラえもんエターナル第3話「決戦第3新東京市」<前編>',
  62. 'upload_date': '20071224',
  63. 'timestamp': 1198527840, # timestamp field has different value if logged in
  64. 'duration': 304,
  65. },
  66. }, {
  67. 'url': 'http://www.nicovideo.jp/watch/so22543406',
  68. 'info_dict': {
  69. 'id': '1388129933',
  70. 'ext': 'mp4',
  71. 'title': '【第1回】RADIOアニメロミックス ラブライブ!~のぞえりRadio Garden~',
  72. 'description': 'md5:b27d224bb0ff53d3c8269e9f8b561cf1',
  73. 'timestamp': 1388851200,
  74. 'upload_date': '20140104',
  75. 'uploader': 'アニメロチャンネル',
  76. 'uploader_id': '312',
  77. }
  78. }]
  79. _VALID_URL = r'https?://(?:www\.|secure\.)?nicovideo\.jp/watch/(?P<id>(?:[a-z]{2})?[0-9]+)'
  80. _NETRC_MACHINE = 'niconico'
  81. # Determine whether the downloader used authentication to download video
  82. _AUTHENTICATED = False
  83. def _real_initialize(self):
  84. self._login()
  85. def _login(self):
  86. (username, password) = self._get_login_info()
  87. # No authentication to be performed
  88. if not username:
  89. return True
  90. # Log in
  91. login_form_strs = {
  92. 'mail': username,
  93. 'password': password,
  94. }
  95. login_data = urlencode_postdata(login_form_strs)
  96. request = sanitized_Request(
  97. 'https://secure.nicovideo.jp/secure/login', login_data)
  98. login_results = self._download_webpage(
  99. request, None, note='Logging in', errnote='Unable to log in')
  100. if re.search(r'(?i)<h1 class="mb8p4">Log in error</h1>', login_results) is not None:
  101. self._downloader.report_warning('unable to log in: bad username or password')
  102. return False
  103. # Successful login
  104. self._AUTHENTICATED = True
  105. return True
  106. def _real_extract(self, url):
  107. video_id = self._match_id(url)
  108. # Get video webpage. We are not actually interested in it for normal
  109. # cases, but need the cookies in order to be able to download the
  110. # info webpage
  111. webpage, handle = self._download_webpage_handle(
  112. 'http://www.nicovideo.jp/watch/' + video_id, video_id)
  113. if video_id.startswith('so'):
  114. video_id = self._match_id(handle.geturl())
  115. video_info = self._download_xml(
  116. 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id,
  117. note='Downloading video info page')
  118. if self._AUTHENTICATED:
  119. # Get flv info
  120. flv_info_webpage = self._download_webpage(
  121. 'http://flapi.nicovideo.jp/api/getflv/' + video_id + '?as3=1',
  122. video_id, 'Downloading flv info')
  123. else:
  124. # Get external player info
  125. ext_player_info = self._download_webpage(
  126. 'http://ext.nicovideo.jp/thumb_watch/' + video_id, video_id)
  127. thumb_play_key = self._search_regex(
  128. r'\'thumbPlayKey\'\s*:\s*\'(.*?)\'', ext_player_info, 'thumbPlayKey')
  129. # Get flv info
  130. flv_info_data = compat_urllib_parse_urlencode({
  131. 'k': thumb_play_key,
  132. 'v': video_id
  133. })
  134. flv_info_request = sanitized_Request(
  135. 'http://ext.nicovideo.jp/thumb_watch', flv_info_data,
  136. {'Content-Type': 'application/x-www-form-urlencoded'})
  137. flv_info_webpage = self._download_webpage(
  138. flv_info_request, video_id,
  139. note='Downloading flv info', errnote='Unable to download flv info')
  140. flv_info = compat_urlparse.parse_qs(flv_info_webpage)
  141. if 'url' not in flv_info:
  142. if 'deleted' in flv_info:
  143. raise ExtractorError('The video has been deleted.',
  144. expected=True)
  145. else:
  146. raise ExtractorError('Unable to find video URL')
  147. video_real_url = flv_info['url'][0]
  148. # Start extracting information
  149. title = xpath_text(video_info, './/title')
  150. if not title:
  151. title = self._og_search_title(webpage, default=None)
  152. if not title:
  153. title = self._html_search_regex(
  154. r'<span[^>]+class="videoHeaderTitle"[^>]*>([^<]+)</span>',
  155. webpage, 'video title')
  156. watch_api_data_string = self._html_search_regex(
  157. r'<div[^>]+id="watchAPIDataContainer"[^>]+>([^<]+)</div>',
  158. webpage, 'watch api data', default=None)
  159. watch_api_data = self._parse_json(watch_api_data_string, video_id) if watch_api_data_string else {}
  160. video_detail = watch_api_data.get('videoDetail', {})
  161. extension = xpath_text(video_info, './/movie_type')
  162. if not extension:
  163. extension = determine_ext(video_real_url)
  164. thumbnail = (
  165. xpath_text(video_info, './/thumbnail_url') or
  166. self._html_search_meta('image', webpage, 'thumbnail', default=None) or
  167. video_detail.get('thumbnail'))
  168. description = xpath_text(video_info, './/description')
  169. timestamp = parse_iso8601(xpath_text(video_info, './/first_retrieve'))
  170. if not timestamp:
  171. match = self._html_search_meta('datePublished', webpage, 'date published', default=None)
  172. if match:
  173. timestamp = parse_iso8601(match.replace('+', ':00+'))
  174. if not timestamp and video_detail.get('postedAt'):
  175. timestamp = parse_iso8601(
  176. video_detail['postedAt'].replace('/', '-'),
  177. delimiter=' ', timezone=datetime.timedelta(hours=9))
  178. view_count = int_or_none(xpath_text(video_info, './/view_counter'))
  179. if not view_count:
  180. match = self._html_search_regex(
  181. r'>Views: <strong[^>]*>([^<]+)</strong>',
  182. webpage, 'view count', default=None)
  183. if match:
  184. view_count = int_or_none(match.replace(',', ''))
  185. view_count = view_count or video_detail.get('viewCount')
  186. comment_count = int_or_none(xpath_text(video_info, './/comment_num'))
  187. if not comment_count:
  188. match = self._html_search_regex(
  189. r'>Comments: <strong[^>]*>([^<]+)</strong>',
  190. webpage, 'comment count', default=None)
  191. if match:
  192. comment_count = int_or_none(match.replace(',', ''))
  193. comment_count = comment_count or video_detail.get('commentCount')
  194. duration = (parse_duration(
  195. xpath_text(video_info, './/length') or
  196. self._html_search_meta(
  197. 'video:duration', webpage, 'video duration', default=None)) or
  198. video_detail.get('length'))
  199. webpage_url = xpath_text(video_info, './/watch_url') or url
  200. if video_info.find('.//ch_id') is not None:
  201. uploader_id = video_info.find('.//ch_id').text
  202. uploader = video_info.find('.//ch_name').text
  203. elif video_info.find('.//user_id') is not None:
  204. uploader_id = video_info.find('.//user_id').text
  205. uploader = video_info.find('.//user_nickname').text
  206. else:
  207. uploader_id = uploader = None
  208. return {
  209. 'id': video_id,
  210. 'url': video_real_url,
  211. 'title': title,
  212. 'ext': extension,
  213. 'format_id': 'economy' if video_real_url.endswith('low') else 'normal',
  214. 'thumbnail': thumbnail,
  215. 'description': description,
  216. 'uploader': uploader,
  217. 'timestamp': timestamp,
  218. 'uploader_id': uploader_id,
  219. 'view_count': view_count,
  220. 'comment_count': comment_count,
  221. 'duration': duration,
  222. 'webpage_url': webpage_url,
  223. }
  224. class NiconicoPlaylistIE(InfoExtractor):
  225. _VALID_URL = r'https?://www\.nicovideo\.jp/mylist/(?P<id>\d+)'
  226. _TEST = {
  227. 'url': 'http://www.nicovideo.jp/mylist/27411728',
  228. 'info_dict': {
  229. 'id': '27411728',
  230. 'title': 'AKB48のオールナイトニッポン',
  231. },
  232. 'playlist_mincount': 225,
  233. }
  234. def _real_extract(self, url):
  235. list_id = self._match_id(url)
  236. webpage = self._download_webpage(url, list_id)
  237. entries_json = self._search_regex(r'Mylist\.preload\(\d+, (\[.*\])\);',
  238. webpage, 'entries')
  239. entries = json.loads(entries_json)
  240. entries = [{
  241. '_type': 'url',
  242. 'ie_key': NiconicoIE.ie_key(),
  243. 'url': ('http://www.nicovideo.jp/watch/%s' %
  244. entry['item_data']['video_id']),
  245. } for entry in entries]
  246. return {
  247. '_type': 'playlist',
  248. 'title': self._search_regex(r'\s+name: "(.*?)"', webpage, 'title'),
  249. 'id': list_id,
  250. 'entries': entries,
  251. }