]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/fc2.py
[cookies] Update MacOS12 `Cookies.binarycookies` location (#2742)
[yt-dlp.git] / yt_dlp / extractor / fc2.py
CommitLineData
cf0efe96 1# coding: utf-8
8e71456a
PH
2from __future__ import unicode_literals
3
8e71456a 4from .common import InfoExtractor
1cc79574 5from ..compat import (
cf0efe96 6 compat_parse_qs,
8e71456a 7)
1cc79574
PH
8from ..utils import (
9 ExtractorError,
5c2266df 10 sanitized_Request,
d6bc443b 11 traverse_obj,
6e6bc8da 12 urlencode_postdata,
d6bc443b 13 urljoin,
1cc79574 14)
8e71456a
PH
15
16
17class FC2IE(InfoExtractor):
cf0efe96 18 _VALID_URL = r'^(?:https?://video\.fc2\.com/(?:[^/]+/)*content/|fc2:)(?P<id>[^/]+)'
8e71456a 19 IE_NAME = 'fc2'
4231235c 20 _NETRC_MACHINE = 'fc2'
8940b860 21 _TESTS = [{
8e71456a
PH
22 'url': 'http://video.fc2.com/en/content/20121103kUan1KHs',
23 'md5': 'a6ebe8ebe0396518689d963774a54eb7',
24 'info_dict': {
25 'id': '20121103kUan1KHs',
26 'ext': 'flv',
27 'title': 'Boxing again with Puff',
28 },
8940b860
PH
29 }, {
30 'url': 'http://video.fc2.com/en/content/20150125cEva0hDn/',
31 'info_dict': {
32 'id': '20150125cEva0hDn',
33 'ext': 'mp4',
34 },
35 'params': {
36 'username': 'ytdl@yt-dl.org',
37 'password': '(snip)',
38d05d17
YCH
38 },
39 'skip': 'requires actual password',
52dfb7ff
S
40 }, {
41 'url': 'http://video.fc2.com/en/a/content/20130926eZpARwsF',
42 'only_matching': True,
8940b860 43 }]
4231235c 44
45 def _login(self):
68217024 46 username, password = self._get_login_info()
8940b860
PH
47 if username is None or password is None:
48 return False
4231235c 49
50 # Log in
51 login_form_strs = {
8940b860 52 'email': username,
4231235c 53 'password': password,
8940b860
PH
54 'done': 'video',
55 'Submit': ' Login ',
4231235c 56 }
57
6e6bc8da 58 login_data = urlencode_postdata(login_form_strs)
5c2266df 59 request = sanitized_Request(
4231235c 60 'https://secure.id.fc2.com/index.php?mode=login&switch_language=en', login_data)
61
62 login_results = self._download_webpage(request, None, note='Logging in', errnote='Unable to log in')
63 if 'mode=redirect&login=done' not in login_results:
8940b860 64 self.report_warning('unable to log in: bad username or password')
4231235c 65 return False
8940b860 66
4231235c 67 # this is also needed
5c2266df 68 login_redir = sanitized_Request('http://id.fc2.com/?mode=redirect&login=done')
8940b860
PH
69 self._download_webpage(
70 login_redir, None, note='Login redirect', errnote='Login redirect failed')
4231235c 71
72 return True
8e71456a
PH
73
74 def _real_extract(self, url):
1cc79574 75 video_id = self._match_id(url)
8940b860 76 self._login()
cf0efe96
YCH
77 webpage = None
78 if not url.startswith('fc2:'):
79 webpage = self._download_webpage(url, video_id)
80 self._downloader.cookiejar.clear_session_cookies() # must clear
81 self._login()
82
d6bc443b 83 title, thumbnail, description = None, None, None
cf0efe96 84 if webpage is not None:
d6bc443b
LNO
85 title = self._html_search_regex(
86 (r'<h2\s+class="videoCnt_title">([^<]+?)</h2>',
87 r'\s+href="[^"]+"\s*title="([^"]+?)"\s*rel="nofollow">\s*<img',
88 # there's two matches in the webpage
89 r'\s+href="[^"]+"\s*title="([^"]+?)"\s*rel="nofollow">\s*\1'),
90 webpage,
91 'title', fatal=False)
cf0efe96 92 thumbnail = self._og_search_thumbnail(webpage)
d6bc443b 93 description = self._og_search_description(webpage)
8e71456a 94
d6bc443b
LNO
95 vidplaylist = self._download_json(
96 'https://video.fc2.com/api/v3/videoplaylist/%s?sh=1&fs=0' % video_id, video_id,
97 note='Downloading info page')
98 vid_url = traverse_obj(vidplaylist, ('playlist', 'nq'))
99 if not vid_url:
100 raise ExtractorError('Unable to extract video URL')
101 vid_url = urljoin('https://video.fc2.com/', vid_url)
8e71456a
PH
102
103 return {
104 'id': video_id,
23ae281b 105 'title': title,
d6bc443b
LNO
106 'url': vid_url,
107 'ext': 'mp4',
108 'description': description,
8e71456a
PH
109 'thumbnail': thumbnail,
110 }
cf0efe96
YCH
111
112
113class FC2EmbedIE(InfoExtractor):
114 _VALID_URL = r'https?://video\.fc2\.com/flv2\.swf\?(?P<query>.+)'
115 IE_NAME = 'fc2:embed'
116
117 _TEST = {
118 '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【吹替】',
119 'md5': 'b8aae5334cb691bdb1193a88a6ab5d5a',
120 'info_dict': {
121 'id': '201403223kCqB3Ez',
122 'ext': 'flv',
123 'title': 'プリズン・ブレイク S1-01 マイケル 【吹替】',
ec85ded8 124 'thumbnail': r're:^https?://.*\.jpg$',
cf0efe96
YCH
125 },
126 }
127
128 def _real_extract(self, url):
5ad28e7f 129 mobj = self._match_valid_url(url)
cf0efe96
YCH
130 query = compat_parse_qs(mobj.group('query'))
131
132 video_id = query['i'][-1]
133 title = query.get('tl', ['FC2 video %s' % video_id])[0]
134
135 sj = query.get('sj', [None])[0]
136 thumbnail = None
137 if sj:
138 # See thumbnailImagePath() in ServerConst.as of flv2.swf
139 thumbnail = 'http://video%s-thumbnail.fc2.com/up/pic/%s.jpg' % (
140 sj, '/'.join((video_id[:6], video_id[6:8], video_id[-2], video_id[-1], video_id)))
141
142 return {
143 '_type': 'url_transparent',
ed2bfe93 144 'ie_key': FC2IE.ie_key(),
cf0efe96
YCH
145 'url': 'fc2:%s' % video_id,
146 'title': title,
147 'thumbnail': thumbnail,
148 }