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.

116 lines
3.8 KiB

  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. ExtractorError,
  8. compat_urllib_request,
  9. compat_urllib_parse,
  10. compat_str,
  11. unescapeHTML,
  12. )
  13. class VKIE(InfoExtractor):
  14. IE_NAME = 'vk.com'
  15. _VALID_URL = r'https?://vk\.com/(?:videos.*?\?.*?z=)?video(?P<id>.*?)(?:\?|%2F|$)'
  16. _NETRC_MACHINE = 'vk'
  17. _TESTS = [
  18. {
  19. 'url': 'http://vk.com/videos-77521?z=video-77521_162222515%2Fclub77521',
  20. 'md5': '0deae91935c54e00003c2a00646315f0',
  21. 'info_dict': {
  22. 'id': '162222515',
  23. 'ext': 'flv',
  24. 'title': 'ProtivoGunz - Хуёвая песня',
  25. 'uploader': 'Noize MC',
  26. 'duration': 195,
  27. },
  28. },
  29. {
  30. 'url': 'http://vk.com/video4643923_163339118',
  31. 'md5': 'f79bccb5cd182b1f43502ca5685b2b36',
  32. 'info_dict': {
  33. 'id': '163339118',
  34. 'ext': 'mp4',
  35. 'uploader': 'Elvira Dzhonik',
  36. 'title': 'Dream Theater - Hollow Years Live at Budokan 720*',
  37. 'duration': 558,
  38. }
  39. },
  40. {
  41. 'url': 'http://vk.com/video-8871596_164049491',
  42. 'md5': 'a590bcaf3d543576c9bd162812387666',
  43. 'note': 'Only available for registered users',
  44. 'info_dict': {
  45. 'id': '164049491',
  46. 'ext': 'mp4',
  47. 'uploader': 'Триллеры',
  48. 'title': '► Бойцовский клуб / Fight Club 1999 [HD 720]\u00a0',
  49. 'duration': 8352,
  50. },
  51. 'skip': 'Requires vk account credentials',
  52. }
  53. ]
  54. def _login(self):
  55. (username, password) = self._get_login_info()
  56. if username is None:
  57. return
  58. login_form = {
  59. 'act': 'login',
  60. 'role': 'al_frame',
  61. 'expire': '1',
  62. 'email': username,
  63. 'pass': password,
  64. }
  65. request = compat_urllib_request.Request('https://login.vk.com/?act=login',
  66. compat_urllib_parse.urlencode(login_form).encode('utf-8'))
  67. login_page = self._download_webpage(request, None, note='Logging in as %s' % username)
  68. if re.search(r'onLoginFailed', login_page):
  69. raise ExtractorError('Unable to login, incorrect username and/or password', expected=True)
  70. def _real_initialize(self):
  71. self._login()
  72. def _real_extract(self, url):
  73. mobj = re.match(self._VALID_URL, url)
  74. video_id = mobj.group('id')
  75. info_url = 'http://vk.com/al_video.php?act=show&al=1&video=%s' % video_id
  76. info_page = self._download_webpage(info_url, video_id)
  77. if re.search(r'<!>Please log in or <', info_page):
  78. raise ExtractorError('This video is only available for registered users, '
  79. 'use --username and --password options to provide account credentials.', expected=True)
  80. m_yt = re.search(r'src="(http://www.youtube.com/.*?)"', info_page)
  81. if m_yt is not None:
  82. self.to_screen(u'Youtube video detected')
  83. return self.url_result(m_yt.group(1), 'Youtube')
  84. data_json = self._search_regex(r'var vars = ({.*?});', info_page, 'vars')
  85. data = json.loads(data_json)
  86. formats = [{
  87. 'format_id': k,
  88. 'url': v,
  89. 'width': int(k[len('url'):]),
  90. } for k, v in data.items()
  91. if k.startswith('url')]
  92. self._sort_formats(formats)
  93. return {
  94. 'id': compat_str(data['vid']),
  95. 'formats': formats,
  96. 'title': unescapeHTML(data['md_title']),
  97. 'thumbnail': data.get('jpg'),
  98. 'uploader': data.get('md_author'),
  99. 'duration': data.get('duration')
  100. }