]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/fc2.py
[npo] Extend _VALID_URL to support ntr.nl (Closes #6248)
[yt-dlp.git] / youtube_dl / extractor / fc2.py
CommitLineData
8e71456a
PH
1#! -*- coding: utf-8 -*-
2from __future__ import unicode_literals
3
8e71456a
PH
4import hashlib
5
6from .common import InfoExtractor
1cc79574 7from ..compat import (
4231235c 8 compat_urllib_parse,
8e71456a
PH
9 compat_urllib_request,
10 compat_urlparse,
11)
1cc79574
PH
12from ..utils import (
13 ExtractorError,
14)
8e71456a
PH
15
16
17class FC2IE(InfoExtractor):
1cc79574 18 _VALID_URL = r'^http://video\.fc2\.com/(?:[^/]+/)?content/(?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)',
38 'skip': 'requires actual password'
39 }
40 }]
4231235c 41
42 def _login(self):
43 (username, password) = self._get_login_info()
8940b860
PH
44 if username is None or password is None:
45 return False
4231235c 46
47 # Log in
48 login_form_strs = {
8940b860 49 'email': username,
4231235c 50 'password': password,
8940b860
PH
51 'done': 'video',
52 'Submit': ' Login ',
4231235c 53 }
54
55 # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
56 # chokes on unicode
57 login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k, v in login_form_strs.items())
58 login_data = compat_urllib_parse.urlencode(login_form).encode('utf-8')
8940b860 59 request = compat_urllib_request.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
68 login_redir = compat_urllib_request.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()
8e71456a
PH
77 webpage = self._download_webpage(url, video_id)
78 self._downloader.cookiejar.clear_session_cookies() # must clear
4231235c 79 self._login()
8e71456a
PH
80
81 title = self._og_search_title(webpage)
82 thumbnail = self._og_search_thumbnail(webpage)
83 refer = url.replace('/content/', '/a/content/')
84
386ba39c 85 mimi = hashlib.md5((video_id + '_gGddgPfeaf_gzyr').encode('utf-8')).hexdigest()
8e71456a
PH
86
87 info_url = (
88 "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&".
5f6a1245 89 format(video_id, mimi, compat_urllib_request.quote(refer, safe='').replace('.', '%2E')))
8e71456a
PH
90
91 info_webpage = self._download_webpage(
92 info_url, video_id, note='Downloading info page')
93 info = compat_urlparse.parse_qs(info_webpage)
94
95 if 'err_code' in info:
4231235c 96 # most of the time we can still download wideo even if err_code is 403 or 602
8940b860
PH
97 self.report_warning(
98 'Error code was: %s... but still trying' % info['err_code'][0])
40b1cbaf 99
4231235c 100 if 'filepath' not in info:
8940b860 101 raise ExtractorError('Cannot download file. Are you logged in?')
8e71456a
PH
102
103 video_url = info['filepath'][0] + '?mid=' + info['mid'][0]
23ae281b
PH
104 title_info = info.get('title')
105 if title_info:
106 title = title_info[0]
8e71456a
PH
107
108 return {
109 'id': video_id,
23ae281b 110 'title': title,
8e71456a
PH
111 'url': video_url,
112 'ext': 'flv',
113 'thumbnail': thumbnail,
114 }