]>
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, | |
11 | compat_str, | |
12 | ||
13 | ExtractorError, | |
14 | unified_strdate, | |
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', | |
52ad14ae | 33 | }, |
214c22c7 JMF |
34 | 'params': { |
35 | 'username': 'ydl.niconico@gmail.com', | |
36 | 'password': 'youtube-dl', | |
52ad14ae TT |
37 | }, |
38 | } | |
39 | ||
13ebea79 | 40 | _VALID_URL = r'^https?://(?:www\.|secure\.)?nicovideo\.jp/watch/([a-z][a-z][0-9]+)(?:.*)$' |
52ad14ae | 41 | _NETRC_MACHINE = 'niconico' |
52ad14ae TT |
42 | |
43 | def _real_initialize(self): | |
44 | self._login() | |
45 | ||
46 | def _login(self): | |
47 | (username, password) = self._get_login_info() | |
52ad14ae | 48 | if username is None: |
214c22c7 JMF |
49 | # Login is required |
50 | raise ExtractorError('No login info available, needed for using %s.' % self.IE_NAME, expected=True) | |
52ad14ae TT |
51 | |
52 | # Log in | |
53 | login_form_strs = { | |
214c22c7 JMF |
54 | 'mail': username, |
55 | 'password': password, | |
52ad14ae TT |
56 | } |
57 | # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode | |
58 | # chokes on unicode | |
214c22c7 | 59 | login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k, v in login_form_strs.items()) |
13ebea79 PH |
60 | login_data = compat_urllib_parse.urlencode(login_form).encode('utf-8') |
61 | request = compat_urllib_request.Request( | |
214c22c7 | 62 | 'https://secure.nicovideo.jp/secure/login', login_data) |
13ebea79 | 63 | login_results = self._download_webpage( |
214c22c7 | 64 | request, None, note='Logging in', errnote='Unable to log in') |
13ebea79 | 65 | if re.search(r'(?i)<h1 class="mb8p4">Log in error</h1>', login_results) is not None: |
214c22c7 | 66 | self._downloader.report_warning('unable to log in: bad username or password') |
52ad14ae TT |
67 | return False |
68 | return True | |
69 | ||
70 | def _real_extract(self, url): | |
13ebea79 PH |
71 | mobj = re.match(self._VALID_URL, url) |
72 | video_id = mobj.group(1) | |
52ad14ae | 73 | |
bd49928f PH |
74 | # Get video webpage. We are not actually interested in it, but need |
75 | # the cookies in order to be able to download the info webpage | |
76 | self._download_webpage('http://www.nicovideo.jp/watch/' + video_id, video_id) | |
52ad14ae | 77 | |
e26f8712 | 78 | video_info = self._download_xml( |
13ebea79 | 79 | 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id, |
214c22c7 | 80 | note='Downloading video info page') |
52ad14ae TT |
81 | |
82 | # Get flv info | |
13ebea79 | 83 | flv_info_webpage = self._download_webpage( |
214c22c7 JMF |
84 | 'http://flapi.nicovideo.jp/api/getflv?v=' + video_id, |
85 | video_id, 'Downloading flv info') | |
13ebea79 | 86 | video_real_url = compat_urlparse.parse_qs(flv_info_webpage)['url'][0] |
52ad14ae TT |
87 | |
88 | # Start extracting information | |
52ad14ae | 89 | video_title = video_info.find('.//title').text |
52ad14ae | 90 | video_extension = video_info.find('.//movie_type').text |
52ad14ae | 91 | video_format = video_extension.upper() |
52ad14ae | 92 | video_thumbnail = video_info.find('.//thumbnail_url').text |
52ad14ae | 93 | video_description = video_info.find('.//description').text |
52ad14ae | 94 | video_uploader_id = video_info.find('.//user_id').text |
13ebea79 PH |
95 | video_upload_date = unified_strdate(video_info.find('.//first_retrieve').text.split('+')[0]) |
96 | video_view_count = video_info.find('.//view_counter').text | |
97 | video_webpage_url = video_info.find('.//watch_url').text | |
52ad14ae TT |
98 | |
99 | # uploader | |
13ebea79 | 100 | video_uploader = video_uploader_id |
52ad14ae | 101 | url = 'http://seiga.nicovideo.jp/api/user/info?id=' + video_uploader_id |
52ad14ae | 102 | try: |
e26f8712 | 103 | user_info = self._download_xml( |
214c22c7 | 104 | url, video_id, note='Downloading user information') |
e26f8712 | 105 | video_uploader = user_info.find('.//nickname').text |
214c22c7 JMF |
106 | except ExtractorError as err: |
107 | self._downloader.report_warning('Unable to download user info webpage: %s' % compat_str(err)) | |
52ad14ae TT |
108 | |
109 | return { | |
214c22c7 JMF |
110 | 'id': video_id, |
111 | 'url': video_real_url, | |
112 | 'title': video_title, | |
113 | 'ext': video_extension, | |
114 | 'format': video_format, | |
115 | 'thumbnail': video_thumbnail, | |
52ad14ae | 116 | 'description': video_description, |
214c22c7 | 117 | 'uploader': video_uploader, |
52ad14ae TT |
118 | 'upload_date': video_upload_date, |
119 | 'uploader_id': video_uploader_id, | |
214c22c7 | 120 | 'view_count': video_view_count, |
52ad14ae TT |
121 | 'webpage_url': video_webpage_url, |
122 | } |