]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/niconico.py
Update changelog.
[youtubedl] / youtube_dl / extractor / niconico.py
1 # encoding: utf-8
2
3 import re
4 import socket
5
6 from .common import InfoExtractor
7 from ..utils import (
8 compat_http_client,
9 compat_urllib_error,
10 compat_urllib_parse,
11 compat_urllib_request,
12 compat_urlparse,
13 compat_str,
14
15 ExtractorError,
16 unified_strdate,
17 )
18
19
20 class NiconicoIE(InfoExtractor):
21 IE_NAME = u'niconico'
22 IE_DESC = u'ニコニコ動画'
23
24 _TEST = {
25 u'url': u'http://www.nicovideo.jp/watch/sm22312215',
26 u'file': u'sm22312215.mp4',
27 u'md5': u'd1a75c0823e2f629128c43e1212760f9',
28 u'info_dict': {
29 u'title': u'Big Buck Bunny',
30 u'uploader': u'takuya0301',
31 u'uploader_id': u'2698420',
32 u'upload_date': u'20131123',
33 u'description': u'(c) copyright 2008, Blender Foundation / www.bigbuckbunny.org',
34 },
35 u'params': {
36 u'username': u'ydl.niconico@gmail.com',
37 u'password': u'youtube-dl',
38 },
39 }
40
41 _VALID_URL = r'^https?://(?:www\.|secure\.)?nicovideo\.jp/watch/([a-z][a-z][0-9]+)(?:.*)$'
42 _NETRC_MACHINE = 'niconico'
43 # If True it will raise an error if no login info is provided
44 _LOGIN_REQUIRED = True
45
46 def _real_initialize(self):
47 self._login()
48
49 def _login(self):
50 (username, password) = self._get_login_info()
51 # No authentication to be performed
52 if username is None:
53 if self._LOGIN_REQUIRED:
54 raise ExtractorError(u'No login info available, needed for using %s.' % self.IE_NAME, expected=True)
55 return False
56
57 # Log in
58 login_form_strs = {
59 u'mail': username,
60 u'password': password,
61 }
62 # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
63 # chokes on unicode
64 login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k,v in login_form_strs.items())
65 login_data = compat_urllib_parse.urlencode(login_form).encode('utf-8')
66 request = compat_urllib_request.Request(
67 u'https://secure.nicovideo.jp/secure/login', login_data)
68 login_results = self._download_webpage(
69 request, u'', note=u'Logging in', errnote=u'Unable to log in')
70 if re.search(r'(?i)<h1 class="mb8p4">Log in error</h1>', login_results) is not None:
71 self._downloader.report_warning(u'unable to log in: bad username or password')
72 return False
73 return True
74
75 def _real_extract(self, url):
76 mobj = re.match(self._VALID_URL, url)
77 video_id = mobj.group(1)
78
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)
82
83 video_info = self._download_xml(
84 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id,
85 note=u'Downloading video info page')
86
87 # Get flv info
88 flv_info_webpage = self._download_webpage(
89 u'http://flapi.nicovideo.jp/api/getflv?v=' + video_id,
90 video_id, u'Downloading flv info')
91 video_real_url = compat_urlparse.parse_qs(flv_info_webpage)['url'][0]
92
93 # Start extracting information
94 video_title = video_info.find('.//title').text
95 video_extension = video_info.find('.//movie_type').text
96 video_format = video_extension.upper()
97 video_thumbnail = video_info.find('.//thumbnail_url').text
98 video_description = video_info.find('.//description').text
99 video_uploader_id = video_info.find('.//user_id').text
100 video_upload_date = unified_strdate(video_info.find('.//first_retrieve').text.split('+')[0])
101 video_view_count = video_info.find('.//view_counter').text
102 video_webpage_url = video_info.find('.//watch_url').text
103
104 # uploader
105 video_uploader = video_uploader_id
106 url = 'http://seiga.nicovideo.jp/api/user/info?id=' + video_uploader_id
107 try:
108 user_info = self._download_xml(
109 url, video_id, note=u'Downloading user information')
110 video_uploader = user_info.find('.//nickname').text
111 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
112 self._downloader.report_warning(u'Unable to download user info webpage: %s' % compat_str(err))
113
114 return {
115 'id': video_id,
116 'url': video_real_url,
117 'title': video_title,
118 'ext': video_extension,
119 'format': video_format,
120 'thumbnail': video_thumbnail,
121 'description': video_description,
122 'uploader': video_uploader,
123 'upload_date': video_upload_date,
124 'uploader_id': video_uploader_id,
125 'view_count': video_view_count,
126 'webpage_url': video_webpage_url,
127 }