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.

221 lines
7.9 KiB

10 years ago
10 years ago
10 years ago
10 years ago
11 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. unified_strdate,
  13. orderedSet)
  14. class VKIE(InfoExtractor):
  15. IE_NAME = 'vk.com'
  16. _VALID_URL = r'https?://(?:m\.)?vk\.com/(?:video_ext\.php\?.*?\boid=(?P<oid>-?\d+).*?\bid=(?P<id>\d+)|(?:.+?\?.*?z=)?video(?P<videoid>[^s].*?)(?:\?|%2F|$))'
  17. _NETRC_MACHINE = 'vk'
  18. _TESTS = [
  19. {
  20. 'url': 'http://vk.com/videos-77521?z=video-77521_162222515%2Fclub77521',
  21. 'md5': '0deae91935c54e00003c2a00646315f0',
  22. 'info_dict': {
  23. 'id': '162222515',
  24. 'ext': 'flv',
  25. 'title': 'ProtivoGunz - Хуёвая песня',
  26. 'uploader': 're:Noize MC.*',
  27. 'duration': 195,
  28. 'upload_date': '20120212',
  29. },
  30. },
  31. {
  32. 'url': 'http://vk.com/video205387401_165548505',
  33. 'md5': '6c0aeb2e90396ba97035b9cbde548700',
  34. 'info_dict': {
  35. 'id': '165548505',
  36. 'ext': 'mp4',
  37. 'uploader': 'Tom Cruise',
  38. 'title': 'No name',
  39. 'duration': 9,
  40. 'upload_date': '20130721'
  41. }
  42. },
  43. {
  44. 'note': 'Embedded video',
  45. 'url': 'http://vk.com/video_ext.php?oid=32194266&id=162925554&hash=7d8c2e0d5e05aeaa&hd=1',
  46. 'md5': 'c7ce8f1f87bec05b3de07fdeafe21a0a',
  47. 'info_dict': {
  48. 'id': '162925554',
  49. 'ext': 'mp4',
  50. 'uploader': 'Vladimir Gavrin',
  51. 'title': 'Lin Dan',
  52. 'duration': 101,
  53. 'upload_date': '20120730',
  54. }
  55. },
  56. {
  57. # VIDEO NOW REMOVED
  58. # please update if you find a video whose URL follows the same pattern
  59. 'url': 'http://vk.com/video-8871596_164049491',
  60. 'md5': 'a590bcaf3d543576c9bd162812387666',
  61. 'note': 'Only available for registered users',
  62. 'info_dict': {
  63. 'id': '164049491',
  64. 'ext': 'mp4',
  65. 'uploader': 'Триллеры',
  66. 'title': '► Бойцовский клуб / Fight Club 1999 [HD 720]',
  67. 'duration': 8352,
  68. 'upload_date': '20121218'
  69. },
  70. 'skip': 'Requires vk account credentials',
  71. },
  72. {
  73. 'url': 'http://vk.com/hd_kino_mania?z=video-43215063_168067957%2F15c66b9b533119788d',
  74. 'md5': '4d7a5ef8cf114dfa09577e57b2993202',
  75. 'info_dict': {
  76. 'id': '168067957',
  77. 'ext': 'mp4',
  78. 'uploader': 'Киномания - лучшее из мира кино',
  79. 'title': ' ',
  80. 'duration': 7291,
  81. 'upload_date': '20140328',
  82. },
  83. 'skip': 'Requires vk account credentials',
  84. },
  85. {
  86. 'url': 'http://m.vk.com/video-43215063_169084319?list=125c627d1aa1cebb83&from=wall-43215063_2566540',
  87. 'md5': '0c45586baa71b7cb1d0784ee3f4e00a6',
  88. 'note': 'ivi.ru embed',
  89. 'info_dict': {
  90. 'id': '60690',
  91. 'ext': 'mp4',
  92. 'title': 'Книга Илая',
  93. 'duration': 6771,
  94. 'upload_date': '20140626',
  95. },
  96. 'skip': 'Only works from Russia',
  97. },
  98. {
  99. # removed video, just testing that we match the pattern
  100. 'url': 'http://vk.com/feed?z=video-43215063_166094326%2Fbb50cacd3177146d7a',
  101. 'only_matching': True,
  102. },
  103. ]
  104. def _login(self):
  105. (username, password) = self._get_login_info()
  106. if username is None:
  107. return
  108. login_form = {
  109. 'act': 'login',
  110. 'role': 'al_frame',
  111. 'expire': '1',
  112. 'email': username,
  113. 'pass': password,
  114. }
  115. request = compat_urllib_request.Request('https://login.vk.com/?act=login',
  116. compat_urllib_parse.urlencode(login_form).encode('utf-8'))
  117. login_page = self._download_webpage(request, None, note='Logging in as %s' % username)
  118. if re.search(r'onLoginFailed', login_page):
  119. raise ExtractorError('Unable to login, incorrect username and/or password', expected=True)
  120. def _real_initialize(self):
  121. self._login()
  122. def _real_extract(self, url):
  123. mobj = re.match(self._VALID_URL, url)
  124. video_id = mobj.group('videoid')
  125. if not video_id:
  126. video_id = '%s_%s' % (mobj.group('oid'), mobj.group('id'))
  127. info_url = 'http://vk.com/al_video.php?act=show&al=1&video=%s' % video_id
  128. info_page = self._download_webpage(info_url, video_id)
  129. ERRORS = {
  130. r'>Видеозапись .*? была изъята из публичного доступа в связи с обращением правообладателя.<':
  131. 'Video %s has been removed from public access due to rightholder complaint.',
  132. r'<!>Please log in or <':
  133. 'Video %s is only available for registered users, '
  134. 'use --username and --password options to provide account credentials.',
  135. r'<!>Unknown error':
  136. 'Video %s does not exist.'
  137. }
  138. for error_re, error_msg in ERRORS.items():
  139. if re.search(error_re, info_page):
  140. raise ExtractorError(error_msg % video_id, expected=True)
  141. m_yt = re.search(r'src="(http://www.youtube.com/.*?)"', info_page)
  142. if m_yt is not None:
  143. self.to_screen('Youtube video detected')
  144. return self.url_result(m_yt.group(1), 'Youtube')
  145. m_opts = re.search(r'(?s)var\s+opts\s*=\s*({.*?});', info_page)
  146. if m_opts:
  147. m_opts_url = re.search(r"url\s*:\s*'([^']+)", m_opts.group(1))
  148. if m_opts_url:
  149. opts_url = m_opts_url.group(1)
  150. if opts_url.startswith('//'):
  151. opts_url = 'http:' + opts_url
  152. return self.url_result(opts_url)
  153. data_json = self._search_regex(r'var vars = ({.*?});', info_page, 'vars')
  154. data = json.loads(data_json)
  155. # Extract upload date
  156. upload_date = None
  157. mobj = re.search(r'id="mv_date_wrap".*?Added ([a-zA-Z]+ [0-9]+), ([0-9]+) at', info_page)
  158. if mobj is not None:
  159. mobj.group(1) + ' ' + mobj.group(2)
  160. upload_date = unified_strdate(mobj.group(1) + ' ' + mobj.group(2))
  161. formats = [{
  162. 'format_id': k,
  163. 'url': v,
  164. 'width': int(k[len('url'):]),
  165. } for k, v in data.items()
  166. if k.startswith('url')]
  167. self._sort_formats(formats)
  168. return {
  169. 'id': compat_str(data['vid']),
  170. 'formats': formats,
  171. 'title': unescapeHTML(data['md_title']),
  172. 'thumbnail': data.get('jpg'),
  173. 'uploader': data.get('md_author'),
  174. 'duration': data.get('duration'),
  175. 'upload_date': upload_date,
  176. }
  177. class VKUserVideosIE(InfoExtractor):
  178. IE_NAME = 'vk.com:user-videos'
  179. IE_DESC = 'vk.com:All of a user\'s videos'
  180. _VALID_URL = r'https?://vk\.com/videos(?P<id>[0-9]+)(?:m\?.*)?'
  181. _TEMPLATE_URL = 'https://vk.com/videos'
  182. _TEST = {
  183. 'url': 'http://vk.com/videos205387401',
  184. 'playlist_mincount': 4,
  185. }
  186. def _real_extract(self, url):
  187. page_id = self._match_id(url)
  188. page = self._download_webpage(url, page_id)
  189. video_ids = orderedSet(
  190. m.group(1) for m in re.finditer(r'href="/video([0-9_]+)"', page))
  191. url_entries = [
  192. self.url_result(
  193. 'http://vk.com/video' + video_id, 'VK', video_id=video_id)
  194. for video_id in video_ids]
  195. return self.playlist_result(url_entries, page_id)