fc2.py 3.8 KB

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