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