]>
Commit | Line | Data |
---|---|---|
52ad14ae | 1 | # encoding: utf-8 |
214c22c7 | 2 | from __future__ import unicode_literals |
52ad14ae TT |
3 | |
4 | import re | |
52ad14ae TT |
5 | |
6 | from .common import InfoExtractor | |
7 | from ..utils import ( | |
52ad14ae TT |
8 | compat_urllib_parse, |
9 | compat_urllib_request, | |
10 | compat_urlparse, | |
52ad14ae | 11 | unified_strdate, |
15ce1338 S |
12 | parse_duration, |
13 | int_or_none, | |
52ad14ae TT |
14 | ) |
15 | ||
13ebea79 | 16 | |
52ad14ae | 17 | class NiconicoIE(InfoExtractor): |
214c22c7 JMF |
18 | IE_NAME = 'niconico' |
19 | IE_DESC = 'ニコニコ動画' | |
52ad14ae TT |
20 | |
21 | _TEST = { | |
214c22c7 JMF |
22 | 'url': 'http://www.nicovideo.jp/watch/sm22312215', |
23 | 'md5': 'd1a75c0823e2f629128c43e1212760f9', | |
24 | 'info_dict': { | |
25 | 'id': 'sm22312215', | |
26 | 'ext': 'mp4', | |
27 | 'title': 'Big Buck Bunny', | |
28 | 'uploader': 'takuya0301', | |
29 | 'uploader_id': '2698420', | |
30 | 'upload_date': '20131123', | |
31 | 'description': '(c) copyright 2008, Blender Foundation / www.bigbuckbunny.org', | |
15ce1338 | 32 | 'duration': 33, |
52ad14ae | 33 | }, |
214c22c7 JMF |
34 | 'params': { |
35 | 'username': 'ydl.niconico@gmail.com', | |
36 | 'password': 'youtube-dl', | |
52ad14ae TT |
37 | }, |
38 | } | |
39 | ||
15ce1338 | 40 | _VALID_URL = r'https?://(?:www\.|secure\.)?nicovideo\.jp/watch/((?:[a-z]{2})?[0-9]+)' |
52ad14ae | 41 | _NETRC_MACHINE = 'niconico' |
2fd466fc | 42 | # Determine whether the downloader uses authentication to download video |
43 | _AUTHENTICATE = False | |
52ad14ae TT |
44 | |
45 | def _real_initialize(self): | |
2fd466fc | 46 | if self._downloader.params.get('username', None) is not None: |
47 | self._AUTHENTICATE = True | |
48 | ||
49 | if self._AUTHENTICATE: | |
50 | self._login() | |
52ad14ae TT |
51 | |
52 | def _login(self): | |
53 | (username, password) = self._get_login_info() | |
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 TT |
70 | return False |
71 | return True | |
72 | ||
73 | def _real_extract(self, url): | |
13ebea79 PH |
74 | mobj = re.match(self._VALID_URL, url) |
75 | video_id = mobj.group(1) | |
52ad14ae | 76 | |
bd49928f PH |
77 | # Get video webpage. We are not actually interested in it, but need |
78 | # the cookies in order to be able to download the info webpage | |
79 | self._download_webpage('http://www.nicovideo.jp/watch/' + video_id, video_id) | |
52ad14ae | 80 | |
e26f8712 | 81 | video_info = self._download_xml( |
13ebea79 | 82 | 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id, |
214c22c7 | 83 | note='Downloading video info page') |
52ad14ae | 84 | |
2fd466fc | 85 | if self._AUTHENTICATE: |
86 | # Get flv info | |
87 | flv_info_webpage = self._download_webpage( | |
88 | 'http://flapi.nicovideo.jp/api/getflv?v=' + video_id, | |
89 | video_id, 'Downloading flv info') | |
90 | else: | |
91 | # Get external player info | |
92 | ext_player_info = self._download_webpage( | |
93 | 'http://ext.nicovideo.jp/thumb_watch/' + video_id, video_id) | |
94 | thumb_play_key = self._search_regex( | |
95 | r'\'thumbPlayKey\'\s*:\s*\'(.*?)\'', ext_player_info, 'thumbPlayKey') | |
96 | ||
97 | # Get flv info | |
98 | flv_info_data = compat_urllib_parse.urlencode({ | |
99 | 'k': thumb_play_key, | |
100 | 'v': video_id | |
101 | }) | |
102 | flv_info_request = compat_urllib_request.Request( | |
103 | 'http://ext.nicovideo.jp/thumb_watch', flv_info_data, | |
104 | {'Content-Type': 'application/x-www-form-urlencoded'}) | |
105 | flv_info_webpage = self._download_webpage( | |
106 | flv_info_request, video_id, | |
107 | note='Downloading flv info', errnote='Unable to download flv info') | |
108 | ||
13ebea79 | 109 | video_real_url = compat_urlparse.parse_qs(flv_info_webpage)['url'][0] |
52ad14ae TT |
110 | |
111 | # Start extracting information | |
15ce1338 S |
112 | title = video_info.find('.//title').text |
113 | extension = video_info.find('.//movie_type').text | |
114 | video_format = extension.upper() | |
115 | thumbnail = video_info.find('.//thumbnail_url').text | |
116 | description = video_info.find('.//description').text | |
117 | upload_date = unified_strdate(video_info.find('.//first_retrieve').text.split('+')[0]) | |
118 | view_count = int_or_none(video_info.find('.//view_counter').text) | |
119 | comment_count = int_or_none(video_info.find('.//comment_num').text) | |
120 | duration = parse_duration(video_info.find('.//length').text) | |
121 | webpage_url = video_info.find('.//watch_url').text | |
122 | ||
64ed7a38 | 123 | if video_info.find('.//ch_id') is not None: |
15ce1338 S |
124 | uploader_id = video_info.find('.//ch_id').text |
125 | uploader = video_info.find('.//ch_name').text | |
64ed7a38 | 126 | elif video_info.find('.//user_id') is not None: |
15ce1338 S |
127 | uploader_id = video_info.find('.//user_id').text |
128 | uploader = video_info.find('.//user_nickname').text | |
129 | else: | |
130 | uploader_id = uploader = None | |
52ad14ae TT |
131 | |
132 | return { | |
214c22c7 JMF |
133 | 'id': video_id, |
134 | 'url': video_real_url, | |
15ce1338 S |
135 | 'title': title, |
136 | 'ext': extension, | |
214c22c7 | 137 | 'format': video_format, |
15ce1338 S |
138 | 'thumbnail': thumbnail, |
139 | 'description': description, | |
140 | 'uploader': uploader, | |
141 | 'upload_date': upload_date, | |
142 | 'uploader_id': uploader_id, | |
143 | 'view_count': view_count, | |
144 | 'comment_count': comment_count, | |
145 | 'duration': duration, | |
146 | 'webpage_url': webpage_url, | |
52ad14ae | 147 | } |