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.

131 lines
4.4 KiB

10 years ago
10 years ago
  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/(?:video_ext\.php\?.*?\boid=(?P<oid>-?\d+).*?\bid=(?P<id>\d+)|(?:videos.*?\?.*?z=)?video(?P<videoid>.*?)(?:\?|%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': 'Elya Iskhakova',
  36. 'title': 'Dream Theater - Hollow Years Live at Budokan 720*',
  37. 'duration': 558,
  38. }
  39. },
  40. {
  41. 'note': 'Embedded video',
  42. 'url': 'http://vk.com/video_ext.php?oid=32194266&id=162925554&hash=7d8c2e0d5e05aeaa&hd=1',
  43. 'md5': 'c7ce8f1f87bec05b3de07fdeafe21a0a',
  44. 'info_dict': {
  45. 'id': '162925554',
  46. 'ext': 'mp4',
  47. 'uploader': 'Vladimir Gavrin',
  48. 'title': 'Lin Dan',
  49. 'duration': 101,
  50. }
  51. },
  52. {
  53. 'url': 'http://vk.com/video-8871596_164049491',
  54. 'md5': 'a590bcaf3d543576c9bd162812387666',
  55. 'note': 'Only available for registered users',
  56. 'info_dict': {
  57. 'id': '164049491',
  58. 'ext': 'mp4',
  59. 'uploader': 'Триллеры',
  60. 'title': '► Бойцовский клуб / Fight Club 1999 [HD 720]\u00a0',
  61. 'duration': 8352,
  62. },
  63. 'skip': 'Requires vk account credentials',
  64. },
  65. ]
  66. def _login(self):
  67. (username, password) = self._get_login_info()
  68. if username is None:
  69. return
  70. login_form = {
  71. 'act': 'login',
  72. 'role': 'al_frame',
  73. 'expire': '1',
  74. 'email': username,
  75. 'pass': password,
  76. }
  77. request = compat_urllib_request.Request('https://login.vk.com/?act=login',
  78. compat_urllib_parse.urlencode(login_form).encode('utf-8'))
  79. login_page = self._download_webpage(request, None, note='Logging in as %s' % username)
  80. if re.search(r'onLoginFailed', login_page):
  81. raise ExtractorError('Unable to login, incorrect username and/or password', expected=True)
  82. def _real_initialize(self):
  83. self._login()
  84. def _real_extract(self, url):
  85. mobj = re.match(self._VALID_URL, url)
  86. video_id = mobj.group('videoid')
  87. if not video_id:
  88. video_id = '%s_%s' % (mobj.group('oid'), mobj.group('id'))
  89. info_url = 'http://vk.com/al_video.php?act=show&al=1&video=%s' % video_id
  90. info_page = self._download_webpage(info_url, video_id)
  91. if re.search(r'<!>Please log in or <', info_page):
  92. raise ExtractorError('This video is only available for registered users, '
  93. 'use --username and --password options to provide account credentials.', expected=True)
  94. m_yt = re.search(r'src="(http://www.youtube.com/.*?)"', info_page)
  95. if m_yt is not None:
  96. self.to_screen('Youtube video detected')
  97. return self.url_result(m_yt.group(1), 'Youtube')
  98. data_json = self._search_regex(r'var vars = ({.*?});', info_page, 'vars')
  99. data = json.loads(data_json)
  100. formats = [{
  101. 'format_id': k,
  102. 'url': v,
  103. 'width': int(k[len('url'):]),
  104. } for k, v in data.items()
  105. if k.startswith('url')]
  106. self._sort_formats(formats)
  107. return {
  108. 'id': compat_str(data['vid']),
  109. 'formats': formats,
  110. 'title': unescapeHTML(data['md_title']),
  111. 'thumbnail': data.get('jpg'),
  112. 'uploader': data.get('md_author'),
  113. 'duration': data.get('duration')
  114. }