2 from __future__ import unicode_literals
7 from .common import InfoExtractor
10 compat_urllib_request,
21 class NiconicoIE(InfoExtractor):
26 'url': 'http://www.nicovideo.jp/watch/sm22312215',
27 'md5': 'd1a75c0823e2f629128c43e1212760f9',
31 'title': 'Big Buck Bunny',
32 'uploader': 'takuya0301',
33 'uploader_id': '2698420',
34 'upload_date': '20131123',
35 'description': '(c) copyright 2008, Blender Foundation / www.bigbuckbunny.org',
39 'username': 'ydl.niconico@gmail.com',
40 'password': 'youtube-dl',
43 'url': 'http://www.nicovideo.jp/watch/nm14296458',
44 'md5': '8db08e0158457cf852a31519fceea5bc',
48 'title': '【鏡音リン】Dance on media【オリジナル】take2!',
49 'description': 'md5:',
51 'uploader_id': '18822557',
52 'upload_date': '20110429',
56 'username': 'ydl.niconico@gmail.com',
57 'password': 'youtube-dl',
61 _VALID_URL = r'https?://(?:www\.|secure\.)?nicovideo\.jp/watch/(?P<id>(?:[a-z]{2})?[0-9]+)'
62 _NETRC_MACHINE = 'niconico'
63 # Determine whether the downloader used authentication to download video
64 _AUTHENTICATED = False
66 def _real_initialize(self):
70 (username, password) = self._get_login_info()
71 # No authentication to be performed
80 # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
82 login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k, v in login_form_strs.items())
83 login_data = compat_urllib_parse.urlencode(login_form).encode('utf-8')
84 request = compat_urllib_request.Request(
85 'https://secure.nicovideo.jp/secure/login', login_data)
86 login_results = self._download_webpage(
87 request, None, note='Logging in', errnote='Unable to log in')
88 if re.search(r'(?i)<h1 class="mb8p4">Log in error</h1>', login_results) is not None:
89 self._downloader.report_warning('unable to log in: bad username or password')
92 self._AUTHENTICATED = True
95 def _real_extract(self, url):
96 video_id = self._match_id(url)
98 # Get video webpage. We are not actually interested in it, but need
99 # the cookies in order to be able to download the info webpage
100 self._download_webpage('http://www.nicovideo.jp/watch/' + video_id, video_id)
102 video_info = self._download_xml(
103 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id,
104 note='Downloading video info page')
106 if self._AUTHENTICATED:
108 flv_info_webpage = self._download_webpage(
109 'http://flapi.nicovideo.jp/api/getflv/' + video_id + '?as3=1',
110 video_id, 'Downloading flv info')
112 # Get external player info
113 ext_player_info = self._download_webpage(
114 'http://ext.nicovideo.jp/thumb_watch/' + video_id, video_id)
115 thumb_play_key = self._search_regex(
116 r'\'thumbPlayKey\'\s*:\s*\'(.*?)\'', ext_player_info, 'thumbPlayKey')
119 flv_info_data = compat_urllib_parse.urlencode({
123 flv_info_request = compat_urllib_request.Request(
124 'http://ext.nicovideo.jp/thumb_watch', flv_info_data,
125 {'Content-Type': 'application/x-www-form-urlencoded'})
126 flv_info_webpage = self._download_webpage(
127 flv_info_request, video_id,
128 note='Downloading flv info', errnote='Unable to download flv info')
130 if 'deleted=' in flv_info_webpage:
131 raise ExtractorError('The video has been deleted.',
133 video_real_url = compat_urlparse.parse_qs(flv_info_webpage)['url'][0]
135 # Start extracting information
136 title = video_info.find('.//title').text
137 extension = video_info.find('.//movie_type').text
138 video_format = extension.upper()
139 thumbnail = video_info.find('.//thumbnail_url').text
140 description = video_info.find('.//description').text
141 upload_date = unified_strdate(video_info.find('.//first_retrieve').text.split('+')[0])
142 view_count = int_or_none(video_info.find('.//view_counter').text)
143 comment_count = int_or_none(video_info.find('.//comment_num').text)
144 duration = parse_duration(video_info.find('.//length').text)
145 webpage_url = video_info.find('.//watch_url').text
147 if video_info.find('.//ch_id') is not None:
148 uploader_id = video_info.find('.//ch_id').text
149 uploader = video_info.find('.//ch_name').text
150 elif video_info.find('.//user_id') is not None:
151 uploader_id = video_info.find('.//user_id').text
152 uploader = video_info.find('.//user_nickname').text
154 uploader_id = uploader = None
158 'url': video_real_url,
161 'format': video_format,
162 'thumbnail': thumbnail,
163 'description': description,
164 'uploader': uploader,
165 'upload_date': upload_date,
166 'uploader_id': uploader_id,
167 'view_count': view_count,
168 'comment_count': comment_count,
169 'duration': duration,
170 'webpage_url': webpage_url,
174 class NiconicoPlaylistIE(InfoExtractor):
175 _VALID_URL = r'https?://www\.nicovideo\.jp/mylist/(?P<id>\d+)'
178 'url': 'http://www.nicovideo.jp/mylist/27411728',
181 'title': 'AKB48のオールナイトニッポン',
183 'playlist_mincount': 225,
186 def _real_extract(self, url):
187 list_id = self._match_id(url)
188 webpage = self._download_webpage(url, list_id)
190 entries_json = self._search_regex(r'Mylist\.preload\(\d+, (\[.*\])\);',
192 entries = json.loads(entries_json)
195 'ie_key': NiconicoIE.ie_key(),
196 'url': ('http://www.nicovideo.jp/watch/%s' %
197 entry['item_data']['video_id']),
198 } for entry in entries]
202 'title': self._search_regex(r'\s+name: "(.*?)"', webpage, 'title'),