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.

177 lines
6.2 KiB

10 years ago
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?://(?:m\.)?vk\.com/(?:video_ext\.php\?.*?\boid=(?P<oid>-?\d+).*?\bid=(?P<id>\d+)|(?:.+?\?.*?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': 're: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]',
  61. 'duration': 8352,
  62. },
  63. 'skip': 'Requires vk account credentials',
  64. },
  65. {
  66. 'url': 'http://vk.com/feed?z=video-43215063_166094326%2Fbb50cacd3177146d7a',
  67. 'md5': 'd82c22e449f036282d1d3f7f4d276869',
  68. 'info_dict': {
  69. 'id': '166094326',
  70. 'ext': 'mp4',
  71. 'uploader': 'Киномания - лучшее из мира кино',
  72. 'title': 'Запах женщины (1992)',
  73. 'duration': 9392,
  74. },
  75. 'skip': 'Requires vk account credentials',
  76. },
  77. {
  78. 'url': 'http://vk.com/hd_kino_mania?z=video-43215063_168067957%2F15c66b9b533119788d',
  79. 'md5': '4d7a5ef8cf114dfa09577e57b2993202',
  80. 'info_dict': {
  81. 'id': '168067957',
  82. 'ext': 'mp4',
  83. 'uploader': 'Киномания - лучшее из мира кино',
  84. 'title': ' ',
  85. 'duration': 7291,
  86. },
  87. 'skip': 'Requires vk account credentials',
  88. },
  89. {
  90. 'url': 'http://m.vk.com/video-43215063_169084319?list=125c627d1aa1cebb83&from=wall-43215063_2566540',
  91. 'md5': '0c45586baa71b7cb1d0784ee3f4e00a6',
  92. 'note': 'ivi.ru embed',
  93. 'info_dict': {
  94. 'id': '60690',
  95. 'ext': 'mp4',
  96. 'title': 'Книга Илая',
  97. 'duration': 6771,
  98. },
  99. 'skip': 'Only works from Russia',
  100. },
  101. ]
  102. def _login(self):
  103. (username, password) = self._get_login_info()
  104. if username is None:
  105. return
  106. login_form = {
  107. 'act': 'login',
  108. 'role': 'al_frame',
  109. 'expire': '1',
  110. 'email': username,
  111. 'pass': password,
  112. }
  113. request = compat_urllib_request.Request('https://login.vk.com/?act=login',
  114. compat_urllib_parse.urlencode(login_form).encode('utf-8'))
  115. login_page = self._download_webpage(request, None, note='Logging in as %s' % username)
  116. if re.search(r'onLoginFailed', login_page):
  117. raise ExtractorError('Unable to login, incorrect username and/or password', expected=True)
  118. def _real_initialize(self):
  119. self._login()
  120. def _real_extract(self, url):
  121. mobj = re.match(self._VALID_URL, url)
  122. video_id = mobj.group('videoid')
  123. if not video_id:
  124. video_id = '%s_%s' % (mobj.group('oid'), mobj.group('id'))
  125. info_url = 'http://vk.com/al_video.php?act=show&al=1&video=%s' % video_id
  126. info_page = self._download_webpage(info_url, video_id)
  127. if re.search(r'<!>Please log in or <', info_page):
  128. raise ExtractorError('This video is only available for registered users, '
  129. 'use --username and --password options to provide account credentials.', expected=True)
  130. m_yt = re.search(r'src="(http://www.youtube.com/.*?)"', info_page)
  131. if m_yt is not None:
  132. self.to_screen('Youtube video detected')
  133. return self.url_result(m_yt.group(1), 'Youtube')
  134. m_opts = re.search(r'(?s)var\s+opts\s*=\s*({.*?});', info_page)
  135. if m_opts:
  136. m_opts_url = re.search(r"url\s*:\s*'([^']+)", m_opts.group(1))
  137. if m_opts_url:
  138. opts_url = m_opts_url.group(1)
  139. if opts_url.startswith('//'):
  140. opts_url = 'http:' + opts_url
  141. return self.url_result(opts_url)
  142. data_json = self._search_regex(r'var vars = ({.*?});', info_page, 'vars')
  143. data = json.loads(data_json)
  144. formats = [{
  145. 'format_id': k,
  146. 'url': v,
  147. 'width': int(k[len('url'):]),
  148. } for k, v in data.items()
  149. if k.startswith('url')]
  150. self._sort_formats(formats)
  151. return {
  152. 'id': compat_str(data['vid']),
  153. 'formats': formats,
  154. 'title': unescapeHTML(data['md_title']),
  155. 'thumbnail': data.get('jpg'),
  156. 'uploader': data.get('md_author'),
  157. 'duration': data.get('duration')
  158. }