fc2.py 3.8 KB

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