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.

334 lines
12 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 ..compat import (
  7. compat_str,
  8. compat_urllib_parse,
  9. compat_urllib_request,
  10. )
  11. from ..utils import (
  12. ExtractorError,
  13. orderedSet,
  14. str_to_int,
  15. unescapeHTML,
  16. unified_strdate,
  17. )
  18. class VKIE(InfoExtractor):
  19. IE_NAME = 'vk'
  20. IE_DESC = 'VK'
  21. _VALID_URL = r'''(?x)
  22. https?://
  23. (?:
  24. (?:m\.)?vk\.com/video_ext\.php\?.*?\boid=(?P<oid>-?\d+).*?\bid=(?P<id>\d+)|
  25. (?:
  26. (?:m\.)?vk\.com/(?:.+?\?.*?z=)?video|
  27. (?:www\.)?biqle\.ru/watch/
  28. )
  29. (?P<videoid>[^s].*?)(?:\?(?:.*\blist=(?P<list_id>[\da-f]+))?|%2F|$)
  30. )
  31. '''
  32. _NETRC_MACHINE = 'vk'
  33. _TESTS = [
  34. {
  35. 'url': 'http://vk.com/videos-77521?z=video-77521_162222515%2Fclub77521',
  36. 'md5': '0deae91935c54e00003c2a00646315f0',
  37. 'info_dict': {
  38. 'id': '162222515',
  39. 'ext': 'flv',
  40. 'title': 'ProtivoGunz - Хуёвая песня',
  41. 'uploader': 're:(?:Noize MC|Alexander Ilyashenko).*',
  42. 'duration': 195,
  43. 'upload_date': '20120212',
  44. 'view_count': int,
  45. },
  46. },
  47. {
  48. 'url': 'http://vk.com/video205387401_165548505',
  49. 'md5': '6c0aeb2e90396ba97035b9cbde548700',
  50. 'info_dict': {
  51. 'id': '165548505',
  52. 'ext': 'mp4',
  53. 'uploader': 'Tom Cruise',
  54. 'title': 'No name',
  55. 'duration': 9,
  56. 'upload_date': '20130721',
  57. 'view_count': int,
  58. }
  59. },
  60. {
  61. 'note': 'Embedded video',
  62. 'url': 'http://vk.com/video_ext.php?oid=32194266&id=162925554&hash=7d8c2e0d5e05aeaa&hd=1',
  63. 'md5': 'c7ce8f1f87bec05b3de07fdeafe21a0a',
  64. 'info_dict': {
  65. 'id': '162925554',
  66. 'ext': 'mp4',
  67. 'uploader': 'Vladimir Gavrin',
  68. 'title': 'Lin Dan',
  69. 'duration': 101,
  70. 'upload_date': '20120730',
  71. 'view_count': int,
  72. }
  73. },
  74. {
  75. # VIDEO NOW REMOVED
  76. # please update if you find a video whose URL follows the same pattern
  77. 'url': 'http://vk.com/video-8871596_164049491',
  78. 'md5': 'a590bcaf3d543576c9bd162812387666',
  79. 'note': 'Only available for registered users',
  80. 'info_dict': {
  81. 'id': '164049491',
  82. 'ext': 'mp4',
  83. 'uploader': 'Триллеры',
  84. 'title': '► Бойцовский клуб / Fight Club 1999 [HD 720]',
  85. 'duration': 8352,
  86. 'upload_date': '20121218',
  87. 'view_count': int,
  88. },
  89. 'skip': 'Requires vk account credentials',
  90. },
  91. {
  92. 'url': 'http://vk.com/hd_kino_mania?z=video-43215063_168067957%2F15c66b9b533119788d',
  93. 'md5': '4d7a5ef8cf114dfa09577e57b2993202',
  94. 'info_dict': {
  95. 'id': '168067957',
  96. 'ext': 'mp4',
  97. 'uploader': 'Киномания - лучшее из мира кино',
  98. 'title': ' ',
  99. 'duration': 7291,
  100. 'upload_date': '20140328',
  101. },
  102. 'skip': 'Requires vk account credentials',
  103. },
  104. {
  105. 'url': 'http://m.vk.com/video-43215063_169084319?list=125c627d1aa1cebb83&from=wall-43215063_2566540',
  106. 'md5': '0c45586baa71b7cb1d0784ee3f4e00a6',
  107. 'note': 'ivi.ru embed',
  108. 'info_dict': {
  109. 'id': '60690',
  110. 'ext': 'mp4',
  111. 'title': 'Книга Илая',
  112. 'duration': 6771,
  113. 'upload_date': '20140626',
  114. 'view_count': int,
  115. },
  116. 'skip': 'Only works from Russia',
  117. },
  118. {
  119. # video (removed?) only available with list id
  120. 'url': 'https://vk.com/video30481095_171201961?list=8764ae2d21f14088d4',
  121. 'md5': '091287af5402239a1051c37ec7b92913',
  122. 'info_dict': {
  123. 'id': '171201961',
  124. 'ext': 'mp4',
  125. 'title': 'ТюменцевВВ_09.07.2015',
  126. 'uploader': 'Anton Ivanov',
  127. 'duration': 109,
  128. 'upload_date': '20150709',
  129. 'view_count': int,
  130. },
  131. },
  132. {
  133. # youtube embed
  134. 'url': 'https://vk.com/video276849682_170681728',
  135. 'info_dict': {
  136. 'id': 'V3K4mi0SYkc',
  137. 'ext': 'mp4',
  138. 'title': "DSWD Awards 'Children's Joy Foundation, Inc.' Certificate of Registration and License to Operate",
  139. 'description': 'md5:bf9c26cfa4acdfb146362682edd3827a',
  140. 'duration': 179,
  141. 'upload_date': '20130116',
  142. 'uploader': "Children's Joy Foundation",
  143. 'uploader_id': 'thecjf',
  144. 'view_count': int,
  145. },
  146. },
  147. {
  148. # removed video, just testing that we match the pattern
  149. 'url': 'http://vk.com/feed?z=video-43215063_166094326%2Fbb50cacd3177146d7a',
  150. 'only_matching': True,
  151. },
  152. {
  153. # age restricted video, requires vk account credentials
  154. 'url': 'https://vk.com/video205387401_164765225',
  155. 'only_matching': True,
  156. },
  157. {
  158. # vk wrapper
  159. 'url': 'http://www.biqle.ru/watch/847655_160197695',
  160. 'only_matching': True,
  161. }
  162. ]
  163. def _login(self):
  164. (username, password) = self._get_login_info()
  165. if username is None:
  166. return
  167. login_page = self._download_webpage(
  168. 'https://vk.com', None, 'Downloading login page')
  169. login_form = self._hidden_inputs(login_page)
  170. login_form.update({
  171. 'email': username.encode('cp1251'),
  172. 'pass': password.encode('cp1251'),
  173. })
  174. request = compat_urllib_request.Request(
  175. 'https://login.vk.com/?act=login',
  176. compat_urllib_parse.urlencode(login_form).encode('utf-8'))
  177. login_page = self._download_webpage(
  178. request, None, note='Logging in as %s' % username)
  179. if re.search(r'onLoginFailed', login_page):
  180. raise ExtractorError(
  181. 'Unable to login, incorrect username and/or password', expected=True)
  182. def _real_initialize(self):
  183. self._login()
  184. def _real_extract(self, url):
  185. mobj = re.match(self._VALID_URL, url)
  186. video_id = mobj.group('videoid')
  187. if not video_id:
  188. video_id = '%s_%s' % (mobj.group('oid'), mobj.group('id'))
  189. info_url = 'https://vk.com/al_video.php?act=show&al=1&module=video&video=%s' % video_id
  190. # Some videos (removed?) can only be downloaded with list id specified
  191. list_id = mobj.group('list_id')
  192. if list_id:
  193. info_url += '&list=%s' % list_id
  194. info_page = self._download_webpage(info_url, video_id)
  195. error_message = self._html_search_regex(
  196. r'(?s)<!><div[^>]+class="video_layer_message"[^>]*>(.+?)</div>',
  197. info_page, 'error message', default=None)
  198. if error_message:
  199. raise ExtractorError(error_message, expected=True)
  200. if re.search(r'<!>/login\.php\?.*\bact=security_check', info_page):
  201. raise ExtractorError(
  202. 'You are trying to log in from an unusual location. You should confirm ownership at vk.com to log in with this IP.',
  203. expected=True)
  204. ERRORS = {
  205. r'>Видеозапись .*? была изъята из публичного доступа в связи с обращением правообладателя.<':
  206. 'Video %s has been removed from public access due to rightholder complaint.',
  207. r'<!>Please log in or <':
  208. 'Video %s is only available for registered users, '
  209. 'use --username and --password options to provide account credentials.',
  210. r'<!>Unknown error':
  211. 'Video %s does not exist.',
  212. r'<!>Видео временно недоступно':
  213. 'Video %s is temporarily unavailable.',
  214. r'<!>Access denied':
  215. 'Access denied to video %s.',
  216. }
  217. for error_re, error_msg in ERRORS.items():
  218. if re.search(error_re, info_page):
  219. raise ExtractorError(error_msg % video_id, expected=True)
  220. youtube_url = self._search_regex(
  221. r'<iframe[^>]+src="((?:https?:)?//www.youtube.com/embed/[^"]+)"',
  222. info_page, 'youtube iframe', default=None)
  223. if youtube_url:
  224. return self.url_result(youtube_url, 'Youtube')
  225. m_rutube = re.search(
  226. r'\ssrc="((?:https?:)?//rutube\.ru\\?/video\\?/embed(?:.*?))\\?"', info_page)
  227. if m_rutube is not None:
  228. self.to_screen('rutube video detected')
  229. rutube_url = self._proto_relative_url(
  230. m_rutube.group(1).replace('\\', ''))
  231. return self.url_result(rutube_url)
  232. m_opts = re.search(r'(?s)var\s+opts\s*=\s*({.+?});', info_page)
  233. if m_opts:
  234. m_opts_url = re.search(r"url\s*:\s*'((?!/\b)[^']+)", m_opts.group(1))
  235. if m_opts_url:
  236. opts_url = m_opts_url.group(1)
  237. if opts_url.startswith('//'):
  238. opts_url = 'http:' + opts_url
  239. return self.url_result(opts_url)
  240. data_json = self._search_regex(r'var\s+vars\s*=\s*({.+?});', info_page, 'vars')
  241. data = json.loads(data_json)
  242. # Extract upload date
  243. upload_date = None
  244. mobj = re.search(r'id="mv_date(?:_views)?_wrap"[^>]*>([a-zA-Z]+ [0-9]+), ([0-9]+) at', info_page)
  245. if mobj is not None:
  246. mobj.group(1) + ' ' + mobj.group(2)
  247. upload_date = unified_strdate(mobj.group(1) + ' ' + mobj.group(2))
  248. view_count = str_to_int(self._search_regex(
  249. r'"mv_views_count_number"[^>]*>([\d,.]+) views<',
  250. info_page, 'view count', fatal=False))
  251. formats = [{
  252. 'format_id': k,
  253. 'url': v,
  254. 'width': int(k[len('url'):]),
  255. } for k, v in data.items()
  256. if k.startswith('url')]
  257. self._sort_formats(formats)
  258. return {
  259. 'id': compat_str(data['vid']),
  260. 'formats': formats,
  261. 'title': unescapeHTML(data['md_title']),
  262. 'thumbnail': data.get('jpg'),
  263. 'uploader': data.get('md_author'),
  264. 'duration': data.get('duration'),
  265. 'upload_date': upload_date,
  266. 'view_count': view_count,
  267. }
  268. class VKUserVideosIE(InfoExtractor):
  269. IE_NAME = 'vk:uservideos'
  270. IE_DESC = "VK - User's Videos"
  271. _VALID_URL = r'https?://vk\.com/videos(?P<id>-?[0-9]+)$'
  272. _TEMPLATE_URL = 'https://vk.com/videos'
  273. _TESTS = [{
  274. 'url': 'http://vk.com/videos205387401',
  275. 'info_dict': {
  276. 'id': '205387401',
  277. 'title': "Tom Cruise's Videos",
  278. },
  279. 'playlist_mincount': 4,
  280. }, {
  281. 'url': 'http://vk.com/videos-77521',
  282. 'only_matching': True,
  283. }]
  284. def _real_extract(self, url):
  285. page_id = self._match_id(url)
  286. webpage = self._download_webpage(url, page_id)
  287. entries = [
  288. self.url_result(
  289. 'http://vk.com/video' + video_id, 'VK', video_id=video_id)
  290. for video_id in orderedSet(re.findall(r'href="/video(-?[0-9_]+)"', webpage))]
  291. title = unescapeHTML(self._search_regex(
  292. r'<title>\s*([^<]+?)\s+\|\s+\d+\s+videos',
  293. webpage, 'title', default=page_id))
  294. return self.playlist_result(entries, page_id, title)