]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/niconico.py
[streamcz] Update extractor
[yt-dlp.git] / youtube_dl / extractor / niconico.py
CommitLineData
52ad14ae 1# encoding: utf-8
214c22c7 2from __future__ import unicode_literals
52ad14ae
TT
3
4import re
a9bad429 5import json
52ad14ae
TT
6
7from .common import InfoExtractor
8from ..utils import (
52ad14ae
TT
9 compat_urllib_parse,
10 compat_urllib_request,
11 compat_urlparse,
52ad14ae 12 unified_strdate,
15ce1338
S
13 parse_duration,
14 int_or_none,
6110bbbf 15 ExtractorError,
52ad14ae
TT
16)
17
13ebea79 18
52ad14ae 19class NiconicoIE(InfoExtractor):
214c22c7
JMF
20 IE_NAME = 'niconico'
21 IE_DESC = 'ニコニコ動画'
52ad14ae
TT
22
23 _TEST = {
214c22c7
JMF
24 'url': 'http://www.nicovideo.jp/watch/sm22312215',
25 'md5': 'd1a75c0823e2f629128c43e1212760f9',
26 'info_dict': {
27 'id': 'sm22312215',
28 'ext': 'mp4',
29 'title': 'Big Buck Bunny',
30 'uploader': 'takuya0301',
31 'uploader_id': '2698420',
32 'upload_date': '20131123',
33 'description': '(c) copyright 2008, Blender Foundation / www.bigbuckbunny.org',
15ce1338 34 'duration': 33,
52ad14ae 35 },
214c22c7
JMF
36 'params': {
37 'username': 'ydl.niconico@gmail.com',
38 'password': 'youtube-dl',
52ad14ae
TT
39 },
40 }
41
15ce1338 42 _VALID_URL = r'https?://(?:www\.|secure\.)?nicovideo\.jp/watch/((?:[a-z]{2})?[0-9]+)'
52ad14ae 43 _NETRC_MACHINE = 'niconico'
23d83ad4
NJ
44 # Determine whether the downloader used authentication to download video
45 _AUTHENTICATED = False
52ad14ae
TT
46
47 def _real_initialize(self):
23d83ad4 48 self._login()
52ad14ae
TT
49
50 def _login(self):
51 (username, password) = self._get_login_info()
23d83ad4
NJ
52 # No authentication to be performed
53 if not username:
54 return True
52ad14ae
TT
55
56 # Log in
57 login_form_strs = {
214c22c7
JMF
58 'mail': username,
59 'password': password,
52ad14ae
TT
60 }
61 # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
62 # chokes on unicode
214c22c7 63 login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k, v in login_form_strs.items())
13ebea79
PH
64 login_data = compat_urllib_parse.urlencode(login_form).encode('utf-8')
65 request = compat_urllib_request.Request(
214c22c7 66 'https://secure.nicovideo.jp/secure/login', login_data)
13ebea79 67 login_results = self._download_webpage(
214c22c7 68 request, None, note='Logging in', errnote='Unable to log in')
13ebea79 69 if re.search(r'(?i)<h1 class="mb8p4">Log in error</h1>', login_results) is not None:
214c22c7 70 self._downloader.report_warning('unable to log in: bad username or password')
52ad14ae 71 return False
23d83ad4
NJ
72 # Successful login
73 self._AUTHENTICATED = True
52ad14ae
TT
74 return True
75
76 def _real_extract(self, url):
13ebea79
PH
77 mobj = re.match(self._VALID_URL, url)
78 video_id = mobj.group(1)
52ad14ae 79
bd49928f
PH
80 # Get video webpage. We are not actually interested in it, but need
81 # the cookies in order to be able to download the info webpage
82 self._download_webpage('http://www.nicovideo.jp/watch/' + video_id, video_id)
52ad14ae 83
e26f8712 84 video_info = self._download_xml(
13ebea79 85 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id,
214c22c7 86 note='Downloading video info page')
52ad14ae 87
23d83ad4 88 if self._AUTHENTICATED:
2fd466fc 89 # Get flv info
90 flv_info_webpage = self._download_webpage(
91 'http://flapi.nicovideo.jp/api/getflv?v=' + video_id,
92 video_id, 'Downloading flv info')
93 else:
94 # Get external player info
95 ext_player_info = self._download_webpage(
96 'http://ext.nicovideo.jp/thumb_watch/' + video_id, video_id)
97 thumb_play_key = self._search_regex(
98 r'\'thumbPlayKey\'\s*:\s*\'(.*?)\'', ext_player_info, 'thumbPlayKey')
99
100 # Get flv info
101 flv_info_data = compat_urllib_parse.urlencode({
102 'k': thumb_play_key,
103 'v': video_id
104 })
105 flv_info_request = compat_urllib_request.Request(
106 'http://ext.nicovideo.jp/thumb_watch', flv_info_data,
107 {'Content-Type': 'application/x-www-form-urlencoded'})
108 flv_info_webpage = self._download_webpage(
109 flv_info_request, video_id,
110 note='Downloading flv info', errnote='Unable to download flv info')
111
6110bbbf
JMF
112 if 'deleted=' in flv_info_webpage:
113 raise ExtractorError('The video has been deleted.',
9e1a5b84 114 expected=True)
13ebea79 115 video_real_url = compat_urlparse.parse_qs(flv_info_webpage)['url'][0]
52ad14ae
TT
116
117 # Start extracting information
15ce1338
S
118 title = video_info.find('.//title').text
119 extension = video_info.find('.//movie_type').text
120 video_format = extension.upper()
121 thumbnail = video_info.find('.//thumbnail_url').text
122 description = video_info.find('.//description').text
123 upload_date = unified_strdate(video_info.find('.//first_retrieve').text.split('+')[0])
124 view_count = int_or_none(video_info.find('.//view_counter').text)
125 comment_count = int_or_none(video_info.find('.//comment_num').text)
126 duration = parse_duration(video_info.find('.//length').text)
127 webpage_url = video_info.find('.//watch_url').text
128
64ed7a38 129 if video_info.find('.//ch_id') is not None:
15ce1338
S
130 uploader_id = video_info.find('.//ch_id').text
131 uploader = video_info.find('.//ch_name').text
64ed7a38 132 elif video_info.find('.//user_id') is not None:
15ce1338
S
133 uploader_id = video_info.find('.//user_id').text
134 uploader = video_info.find('.//user_nickname').text
135 else:
136 uploader_id = uploader = None
52ad14ae
TT
137
138 return {
214c22c7
JMF
139 'id': video_id,
140 'url': video_real_url,
15ce1338
S
141 'title': title,
142 'ext': extension,
214c22c7 143 'format': video_format,
15ce1338
S
144 'thumbnail': thumbnail,
145 'description': description,
146 'uploader': uploader,
147 'upload_date': upload_date,
148 'uploader_id': uploader_id,
149 'view_count': view_count,
150 'comment_count': comment_count,
151 'duration': duration,
152 'webpage_url': webpage_url,
52ad14ae 153 }
a9bad429
JMF
154
155
156class NiconicoPlaylistIE(InfoExtractor):
157 _VALID_URL = r'https?://www\.nicovideo\.jp/mylist/(?P<id>\d+)'
158
159 _TEST = {
160 'url': 'http://www.nicovideo.jp/mylist/27411728',
161 'info_dict': {
162 'id': '27411728',
163 'title': 'AKB48のオールナイトニッポン',
164 },
165 'playlist_mincount': 225,
166 }
167
168 def _real_extract(self, url):
169 list_id = self._match_id(url)
170 webpage = self._download_webpage(url, list_id)
171
172 entries_json = self._search_regex(r'Mylist\.preload\(\d+, (\[.*\])\);',
9e1a5b84 173 webpage, 'entries')
a9bad429
JMF
174 entries = json.loads(entries_json)
175 entries = [{
176 '_type': 'url',
177 'ie_key': NiconicoIE.ie_key(),
2fdbf27a 178 'url': ('http://www.nicovideo.jp/watch/%s' %
9e1a5b84 179 entry['item_data']['video_id']),
a9bad429
JMF
180 } for entry in entries]
181
182 return {
183 '_type': 'playlist',
184 'title': self._search_regex(r'\s+name: "(.*?)"', webpage, 'title'),
185 'id': list_id,
186 'entries': entries,
187 }