]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/fc2.py
Fix issues with fc2
[yt-dlp.git] / youtube_dl / extractor / fc2.py
CommitLineData
8e71456a
PH
1#! -*- coding: utf-8 -*-
2from __future__ import unicode_literals
3
4import re
5import hashlib
6
7from .common import InfoExtractor
8from ..utils import (
9 ExtractorError,
4231235c 10 compat_urllib_parse,
8e71456a
PH
11 compat_urllib_request,
12 compat_urlparse,
13)
14
15
16class FC2IE(InfoExtractor):
4231235c 17 _VALID_URL = r'^http://video\.fc2\.com/((?P<lang>[^/]+)/)?(a/)?content/(?P<id>[^/]+)'
8e71456a 18 IE_NAME = 'fc2'
4231235c 19 _NETRC_MACHINE = 'fc2'
8e71456a
PH
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
4231235c 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._downloader.report_warning('unable to log in: will be downloading in non authorized mode')
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._downloader.report_warning('unable to log in: bad username or password')
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
8e71456a
PH
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
4231235c 71 self._login()
8e71456a
PH
72
73 title = self._og_search_title(webpage)
74 thumbnail = self._og_search_thumbnail(webpage)
8e71456a 75
4231235c 76 refer = (url if '/a/content/' in url else url.replace('/content/', '/a/content/'));
386ba39c 77 mimi = hashlib.md5((video_id + '_gGddgPfeaf_gzyr').encode('utf-8')).hexdigest()
8e71456a
PH
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:
4231235c 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 print 'Error code was: %s... but still trying' % info['err_code'][0]
91
92 if 'filepath' not in info:
93 raise ExtractorError('No file path for download. Maybe not logged?')
8e71456a
PH
94
95 video_url = info['filepath'][0] + '?mid=' + info['mid'][0]
23ae281b
PH
96 title_info = info.get('title')
97 if title_info:
98 title = title_info[0]
8e71456a
PH
99
100 return {
101 'id': video_id,
23ae281b 102 'title': title,
8e71456a
PH
103 'url': video_url,
104 'ext': 'flv',
105 'thumbnail': thumbnail,
106 }