Don't use report_warning for reporting warnings
[youtube-dl] / youtube_dl / extractor / fc2.py
1 #! -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3
4 import re
5 import hashlib
6
7 from .common import InfoExtractor
8 from ..utils import (
9     ExtractorError,
10     compat_urllib_parse,
11     compat_urllib_request,
12     compat_urlparse,
13 )
14
15
16 class FC2IE(InfoExtractor):
17     _VALID_URL = r'^http://video\.fc2\.com/((?P<lang>[^/]+)/)?(a/)?content/(?P<id>[^/]+)'
18     IE_NAME = 'fc2'
19     _NETRC_MACHINE = 'fc2'
20     _TEST = {
21         'url': 'http://video.fc2.com/en/content/20121103kUan1KHs',
22         'md5': 'a6ebe8ebe0396518689d963774a54eb7',
23         'info_dict': {
24             'id': '20121103kUan1KHs',
25             'ext': 'flv',
26             'title': 'Boxing again with Puff',
27         },
28     }
29
30     #def _real_initialize(self):
31     #    self._login()
32
33     def _login(self):
34         (username, password) = self._get_login_info()
35         if (username is None) or (password is None):
36            self.to_screen('unable to log in: will be downloading in non authorized mode') # report_warning
37            return False
38
39         # Log in
40         login_form_strs = {
41             'email':    username,
42             'password': password,
43             'done':     'video',
44             'Submit':   ' Login ',
45         }
46
47         # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
48         # chokes on unicode
49         login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k, v in login_form_strs.items())
50         login_data = compat_urllib_parse.urlencode(login_form).encode('utf-8')
51         request    = compat_urllib_request.Request(
52             'https://secure.id.fc2.com/index.php?mode=login&switch_language=en', login_data)
53
54         login_results = self._download_webpage(request, None, note='Logging in', errnote='Unable to log in')
55         if 'mode=redirect&login=done' not in login_results:
56             self.to_screen('unable to log in: bad username or password') # report_warning
57             return False
58         
59         # this is also needed
60         login_redir = compat_urllib_request.Request('http://id.fc2.com/?mode=redirect&login=done')
61         redir_res   = self._download_webpage(login_redir, None, note='Login redirect', errnote='Something is not right')
62
63         return True
64
65     def _real_extract(self, url):
66         mobj = re.match(self._VALID_URL, url)
67         video_id = mobj.group('id')
68
69         webpage = self._download_webpage(url, video_id)
70         self._downloader.cookiejar.clear_session_cookies()  # must clear
71         self._login()
72
73         title = self._og_search_title(webpage)
74         thumbnail = self._og_search_thumbnail(webpage)
75
76         refer = (url if '/a/content/' in url else url.replace('/content/', '/a/content/'));
77         mimi = hashlib.md5((video_id + '_gGddgPfeaf_gzyr').encode('utf-8')).hexdigest()
78
79         info_url = (
80             "http://video.fc2.com/ginfo.php?mimi={1:s}&href={2:s}&v={0:s}&fversion=WIN%2011%2C6%2C602%2C180&from=2&otag=0&upid={0:s}&tk=null&".
81             format(video_id, mimi, compat_urllib_request.quote(refer, safe='').replace('.','%2E')))
82
83         info_webpage = self._download_webpage(
84             info_url, video_id, note='Downloading info page')
85         info = compat_urlparse.parse_qs(info_webpage)
86
87         if 'err_code' in info:
88             #raise ExtractorError('Error code: %s' % info['err_code'][0])
89             # most of the time we can still download wideo even if err_code is 403 or 602
90             self.to_screen('Error code was: %s... but still trying' % info['err_code'][0]) # report_warning
91
92         if 'filepath' not in info:
93             raise ExtractorError('Cannot download file. Are you logged?')
94
95         video_url = info['filepath'][0] + '?mid=' + info['mid'][0]
96         title_info = info.get('title')
97         if title_info:
98             title = title_info[0]
99
100         return {
101             'id': video_id,
102             'title': title,
103             'url': video_url,
104             'ext': 'flv',
105             'thumbnail': thumbnail,
106         }