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.

63 lines
2.0 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. compat_str,
  8. unescapeHTML,
  9. )
  10. class VKIE(InfoExtractor):
  11. IE_NAME = 'vk.com'
  12. _VALID_URL = r'https?://vk\.com/(?:videos.*?\?.*?z=)?video(?P<id>.*?)(?:\?|%2F|$)'
  13. _TESTS = [{
  14. 'url': 'http://vk.com/videos-77521?z=video-77521_162222515%2Fclub77521',
  15. 'file': '162222515.flv',
  16. 'md5': '0deae91935c54e00003c2a00646315f0',
  17. 'info_dict': {
  18. 'title': 'ProtivoGunz - Хуёвая песня',
  19. 'uploader': 'Noize MC',
  20. },
  21. },
  22. {
  23. 'url': 'http://vk.com/video4643923_163339118',
  24. 'file': '163339118.mp4',
  25. 'md5': 'f79bccb5cd182b1f43502ca5685b2b36',
  26. 'info_dict': {
  27. 'uploader': 'Elvira Dzhonik',
  28. 'title': 'Dream Theater - Hollow Years Live at Budokan 720*',
  29. }
  30. }]
  31. def _real_extract(self, url):
  32. mobj = re.match(self._VALID_URL, url)
  33. video_id = mobj.group('id')
  34. info_url = 'http://vk.com/al_video.php?act=show&al=1&video=%s' % video_id
  35. info_page = self._download_webpage(info_url, video_id)
  36. m_yt = re.search(r'src="(http://www.youtube.com/.*?)"', info_page)
  37. if m_yt is not None:
  38. self.to_screen(u'Youtube video detected')
  39. return self.url_result(m_yt.group(1), 'Youtube')
  40. data_json = self._search_regex(r'var vars = ({.*?});', info_page, 'vars')
  41. data = json.loads(data_json)
  42. formats = [{
  43. 'format_id': k,
  44. 'url': v,
  45. 'width': int(k[len('url'):]),
  46. } for k, v in data.items()
  47. if k.startswith('url')]
  48. self._sort_formats(formats)
  49. return {
  50. 'id': compat_str(data['vid']),
  51. 'formats': formats,
  52. 'title': unescapeHTML(data['md_title']),
  53. 'thumbnail': data.get('jpg'),
  54. 'uploader': data.get('md_author'),
  55. }