You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.9 KiB

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