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.

117 lines
4.0 KiB

9 years ago
11 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. 'url': 'http://video.fc2.com/en/a/content/20130926eZpARwsF',
  38. 'only_matching': True,
  39. }]
  40. def _login(self):
  41. (username, password) = self._get_login_info()
  42. if username is None or password is None:
  43. return False
  44. # Log in
  45. login_form_strs = {
  46. 'email': username,
  47. 'password': password,
  48. 'done': 'video',
  49. 'Submit': ' Login ',
  50. }
  51. # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
  52. # chokes on unicode
  53. login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k, v in login_form_strs.items())
  54. login_data = compat_urllib_parse.urlencode(login_form).encode('utf-8')
  55. request = compat_urllib_request.Request(
  56. 'https://secure.id.fc2.com/index.php?mode=login&switch_language=en', login_data)
  57. login_results = self._download_webpage(request, None, note='Logging in', errnote='Unable to log in')
  58. if 'mode=redirect&login=done' not in login_results:
  59. self.report_warning('unable to log in: bad username or password')
  60. return False
  61. # this is also needed
  62. login_redir = compat_urllib_request.Request('http://id.fc2.com/?mode=redirect&login=done')
  63. self._download_webpage(
  64. login_redir, None, note='Login redirect', errnote='Login redirect failed')
  65. return True
  66. def _real_extract(self, url):
  67. video_id = self._match_id(url)
  68. self._login()
  69. webpage = self._download_webpage(url, video_id)
  70. self._downloader.cookiejar.clear_session_cookies() # must clear
  71. self._login()
  72. title = self._og_search_title(webpage)
  73. thumbnail = self._og_search_thumbnail(webpage)
  74. refer = url.replace('/content/', '/a/content/') if '/a/content/' not in url else url
  75. mimi = hashlib.md5((video_id + '_gGddgPfeaf_gzyr').encode('utf-8')).hexdigest()
  76. info_url = (
  77. "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&".
  78. format(video_id, mimi, compat_urllib_request.quote(refer, safe=b'').replace('.', '%2E')))
  79. info_webpage = self._download_webpage(
  80. info_url, video_id, note='Downloading info page')
  81. info = compat_urlparse.parse_qs(info_webpage)
  82. if 'err_code' in info:
  83. # most of the time we can still download wideo even if err_code is 403 or 602
  84. self.report_warning(
  85. 'Error code was: %s... but still trying' % info['err_code'][0])
  86. if 'filepath' not in info:
  87. raise ExtractorError('Cannot download file. Are you logged in?')
  88. video_url = info['filepath'][0] + '?mid=' + info['mid'][0]
  89. title_info = info.get('title')
  90. if title_info:
  91. title = title_info[0]
  92. return {
  93. 'id': video_id,
  94. 'title': title,
  95. 'url': video_url,
  96. 'ext': 'flv',
  97. 'thumbnail': thumbnail,
  98. }