]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/fc2.py
[eitb] Improve hds extraction
[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 12from ..utils import (
cf83f532 13 encode_dict,
1cc79574
PH
14 ExtractorError,
15)
8e71456a
PH
16
17
18class FC2IE(InfoExtractor):
93462856 19 _VALID_URL = r'^http://video\.fc2\.com/(?:[^/]+/)*content/(?P<id>[^/]+)'
8e71456a 20 IE_NAME = 'fc2'
4231235c 21 _NETRC_MACHINE = 'fc2'
8940b860 22 _TESTS = [{
8e71456a
PH
23 'url': 'http://video.fc2.com/en/content/20121103kUan1KHs',
24 'md5': 'a6ebe8ebe0396518689d963774a54eb7',
25 'info_dict': {
26 'id': '20121103kUan1KHs',
27 'ext': 'flv',
28 'title': 'Boxing again with Puff',
29 },
8940b860
PH
30 }, {
31 'url': 'http://video.fc2.com/en/content/20150125cEva0hDn/',
32 'info_dict': {
33 'id': '20150125cEva0hDn',
34 'ext': 'mp4',
35 },
36 'params': {
37 'username': 'ytdl@yt-dl.org',
38 'password': '(snip)',
39 'skip': 'requires actual password'
40 }
52dfb7ff
S
41 }, {
42 'url': 'http://video.fc2.com/en/a/content/20130926eZpARwsF',
43 'only_matching': True,
8940b860 44 }]
4231235c 45
46 def _login(self):
47 (username, password) = self._get_login_info()
8940b860
PH
48 if username is None or password is None:
49 return False
4231235c 50
51 # Log in
52 login_form_strs = {
8940b860 53 'email': username,
4231235c 54 'password': password,
8940b860
PH
55 'done': 'video',
56 'Submit': ' Login ',
4231235c 57 }
58
cf83f532 59 login_data = compat_urllib_parse.urlencode(encode_dict(login_form_strs)).encode('utf-8')
8940b860 60 request = compat_urllib_request.Request(
4231235c 61 'https://secure.id.fc2.com/index.php?mode=login&switch_language=en', login_data)
62
63 login_results = self._download_webpage(request, None, note='Logging in', errnote='Unable to log in')
64 if 'mode=redirect&login=done' not in login_results:
8940b860 65 self.report_warning('unable to log in: bad username or password')
4231235c 66 return False
8940b860 67
4231235c 68 # this is also needed
69 login_redir = compat_urllib_request.Request('http://id.fc2.com/?mode=redirect&login=done')
8940b860
PH
70 self._download_webpage(
71 login_redir, None, note='Login redirect', errnote='Login redirect failed')
4231235c 72
73 return True
8e71456a
PH
74
75 def _real_extract(self, url):
1cc79574 76 video_id = self._match_id(url)
8940b860 77 self._login()
8e71456a
PH
78 webpage = self._download_webpage(url, video_id)
79 self._downloader.cookiejar.clear_session_cookies() # must clear
4231235c 80 self._login()
8e71456a
PH
81
82 title = self._og_search_title(webpage)
83 thumbnail = self._og_search_thumbnail(webpage)
39955b04 84 refer = url.replace('/content/', '/a/content/') if '/a/content/' not in url else url
8e71456a 85
386ba39c 86 mimi = hashlib.md5((video_id + '_gGddgPfeaf_gzyr').encode('utf-8')).hexdigest()
8e71456a
PH
87
88 info_url = (
89 "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 90 format(video_id, mimi, compat_urllib_request.quote(refer, safe=b'').replace('.', '%2E')))
8e71456a
PH
91
92 info_webpage = self._download_webpage(
93 info_url, video_id, note='Downloading info page')
94 info = compat_urlparse.parse_qs(info_webpage)
95
96 if 'err_code' in info:
4231235c 97 # most of the time we can still download wideo even if err_code is 403 or 602
8940b860
PH
98 self.report_warning(
99 'Error code was: %s... but still trying' % info['err_code'][0])
40b1cbaf 100
4231235c 101 if 'filepath' not in info:
8940b860 102 raise ExtractorError('Cannot download file. Are you logged in?')
8e71456a
PH
103
104 video_url = info['filepath'][0] + '?mid=' + info['mid'][0]
23ae281b
PH
105 title_info = info.get('title')
106 if title_info:
107 title = title_info[0]
8e71456a
PH
108
109 return {
110 'id': video_id,
23ae281b 111 'title': title,
8e71456a
PH
112 'url': video_url,
113 'ext': 'flv',
114 'thumbnail': thumbnail,
115 }