]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/fc2.py
[test/cookies] Improve logging
[yt-dlp.git] / yt_dlp / extractor / fc2.py
CommitLineData
cf0efe96 1# coding: utf-8
8e71456a
PH
2from __future__ import unicode_literals
3
8e71456a
PH
4import hashlib
5
6from .common import InfoExtractor
1cc79574 7from ..compat import (
cf0efe96 8 compat_parse_qs,
8e71456a
PH
9 compat_urllib_request,
10 compat_urlparse,
11)
1cc79574
PH
12from ..utils import (
13 ExtractorError,
5c2266df 14 sanitized_Request,
6e6bc8da 15 urlencode_postdata,
1cc79574 16)
8e71456a
PH
17
18
19class FC2IE(InfoExtractor):
cf0efe96 20 _VALID_URL = r'^(?:https?://video\.fc2\.com/(?:[^/]+/)*content/|fc2:)(?P<id>[^/]+)'
8e71456a 21 IE_NAME = 'fc2'
4231235c 22 _NETRC_MACHINE = 'fc2'
8940b860 23 _TESTS = [{
8e71456a
PH
24 'url': 'http://video.fc2.com/en/content/20121103kUan1KHs',
25 'md5': 'a6ebe8ebe0396518689d963774a54eb7',
26 'info_dict': {
27 'id': '20121103kUan1KHs',
28 'ext': 'flv',
29 'title': 'Boxing again with Puff',
30 },
8940b860
PH
31 }, {
32 'url': 'http://video.fc2.com/en/content/20150125cEva0hDn/',
33 'info_dict': {
34 'id': '20150125cEva0hDn',
35 'ext': 'mp4',
36 },
37 'params': {
38 'username': 'ytdl@yt-dl.org',
39 'password': '(snip)',
38d05d17
YCH
40 },
41 'skip': 'requires actual password',
52dfb7ff
S
42 }, {
43 'url': 'http://video.fc2.com/en/a/content/20130926eZpARwsF',
44 'only_matching': True,
8940b860 45 }]
4231235c 46
47 def _login(self):
68217024 48 username, password = self._get_login_info()
8940b860
PH
49 if username is None or password is None:
50 return False
4231235c 51
52 # Log in
53 login_form_strs = {
8940b860 54 'email': username,
4231235c 55 'password': password,
8940b860
PH
56 'done': 'video',
57 'Submit': ' Login ',
4231235c 58 }
59
6e6bc8da 60 login_data = urlencode_postdata(login_form_strs)
5c2266df 61 request = sanitized_Request(
4231235c 62 'https://secure.id.fc2.com/index.php?mode=login&switch_language=en', login_data)
63
64 login_results = self._download_webpage(request, None, note='Logging in', errnote='Unable to log in')
65 if 'mode=redirect&login=done' not in login_results:
8940b860 66 self.report_warning('unable to log in: bad username or password')
4231235c 67 return False
8940b860 68
4231235c 69 # this is also needed
5c2266df 70 login_redir = sanitized_Request('http://id.fc2.com/?mode=redirect&login=done')
8940b860
PH
71 self._download_webpage(
72 login_redir, None, note='Login redirect', errnote='Login redirect failed')
4231235c 73
74 return True
8e71456a
PH
75
76 def _real_extract(self, url):
1cc79574 77 video_id = self._match_id(url)
8940b860 78 self._login()
cf0efe96
YCH
79 webpage = None
80 if not url.startswith('fc2:'):
81 webpage = self._download_webpage(url, video_id)
82 self._downloader.cookiejar.clear_session_cookies() # must clear
83 self._login()
84
85 title = 'FC2 video %s' % video_id
86 thumbnail = None
87 if webpage is not None:
88 title = self._og_search_title(webpage)
89 thumbnail = self._og_search_thumbnail(webpage)
39955b04 90 refer = url.replace('/content/', '/a/content/') if '/a/content/' not in url else url
8e71456a 91
386ba39c 92 mimi = hashlib.md5((video_id + '_gGddgPfeaf_gzyr').encode('utf-8')).hexdigest()
8e71456a
PH
93
94 info_url = (
611c1dd9 95 '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&'.
c5864a8c 96 format(video_id, mimi, compat_urllib_request.quote(refer, safe=b'').replace('.', '%2E')))
8e71456a
PH
97
98 info_webpage = self._download_webpage(
99 info_url, video_id, note='Downloading info page')
100 info = compat_urlparse.parse_qs(info_webpage)
101
102 if 'err_code' in info:
4231235c 103 # most of the time we can still download wideo even if err_code is 403 or 602
8940b860
PH
104 self.report_warning(
105 'Error code was: %s... but still trying' % info['err_code'][0])
40b1cbaf 106
4231235c 107 if 'filepath' not in info:
8940b860 108 raise ExtractorError('Cannot download file. Are you logged in?')
8e71456a
PH
109
110 video_url = info['filepath'][0] + '?mid=' + info['mid'][0]
23ae281b
PH
111 title_info = info.get('title')
112 if title_info:
113 title = title_info[0]
8e71456a
PH
114
115 return {
116 'id': video_id,
23ae281b 117 'title': title,
8e71456a
PH
118 'url': video_url,
119 'ext': 'flv',
120 'thumbnail': thumbnail,
121 }
cf0efe96
YCH
122
123
124class FC2EmbedIE(InfoExtractor):
125 _VALID_URL = r'https?://video\.fc2\.com/flv2\.swf\?(?P<query>.+)'
126 IE_NAME = 'fc2:embed'
127
128 _TEST = {
129 'url': 'http://video.fc2.com/flv2.swf?t=201404182936758512407645&i=20130316kwishtfitaknmcgd76kjd864hso93htfjcnaogz629mcgfs6rbfk0hsycma7shkf85937cbchfygd74&i=201403223kCqB3Ez&d=2625&sj=11&lang=ja&rel=1&from=11&cmt=1&tk=TlRBM09EQTNNekU9&tl=プリズン・ブレイク%20S1-01%20マイケル%20【吹替】',
130 'md5': 'b8aae5334cb691bdb1193a88a6ab5d5a',
131 'info_dict': {
132 'id': '201403223kCqB3Ez',
133 'ext': 'flv',
134 'title': 'プリズン・ブレイク S1-01 マイケル 【吹替】',
ec85ded8 135 'thumbnail': r're:^https?://.*\.jpg$',
cf0efe96
YCH
136 },
137 }
138
139 def _real_extract(self, url):
5ad28e7f 140 mobj = self._match_valid_url(url)
cf0efe96
YCH
141 query = compat_parse_qs(mobj.group('query'))
142
143 video_id = query['i'][-1]
144 title = query.get('tl', ['FC2 video %s' % video_id])[0]
145
146 sj = query.get('sj', [None])[0]
147 thumbnail = None
148 if sj:
149 # See thumbnailImagePath() in ServerConst.as of flv2.swf
150 thumbnail = 'http://video%s-thumbnail.fc2.com/up/pic/%s.jpg' % (
151 sj, '/'.join((video_id[:6], video_id[6:8], video_id[-2], video_id[-1], video_id)))
152
153 return {
154 '_type': 'url_transparent',
ed2bfe93 155 'ie_key': FC2IE.ie_key(),
cf0efe96
YCH
156 'url': 'fc2:%s' % video_id,
157 'title': title,
158 'thumbnail': thumbnail,
159 }