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.

678 lines
25 KiB

10 years ago
10 years ago
10 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import collections
  4. import functools
  5. import re
  6. from .common import InfoExtractor
  7. from ..compat import compat_urlparse
  8. from ..utils import (
  9. clean_html,
  10. ExtractorError,
  11. get_element_by_class,
  12. int_or_none,
  13. OnDemandPagedList,
  14. orderedSet,
  15. str_or_none,
  16. str_to_int,
  17. unescapeHTML,
  18. unified_timestamp,
  19. url_or_none,
  20. urlencode_postdata,
  21. )
  22. from .dailymotion import DailymotionIE
  23. from .odnoklassniki import OdnoklassnikiIE
  24. from .pladform import PladformIE
  25. from .vimeo import VimeoIE
  26. from .youtube import YoutubeIE
  27. class VKBaseIE(InfoExtractor):
  28. _NETRC_MACHINE = 'vk'
  29. def _login(self):
  30. username, password = self._get_login_info()
  31. if username is None:
  32. return
  33. login_page, url_handle = self._download_webpage_handle(
  34. 'https://vk.com', None, 'Downloading login page')
  35. login_form = self._hidden_inputs(login_page)
  36. login_form.update({
  37. 'email': username.encode('cp1251'),
  38. 'pass': password.encode('cp1251'),
  39. })
  40. # vk serves two same remixlhk cookies in Set-Cookie header and expects
  41. # first one to be actually set
  42. self._apply_first_set_cookie_header(url_handle, 'remixlhk')
  43. login_page = self._download_webpage(
  44. 'https://login.vk.com/?act=login', None,
  45. note='Logging in',
  46. data=urlencode_postdata(login_form))
  47. if re.search(r'onLoginFailed', login_page):
  48. raise ExtractorError(
  49. 'Unable to login, incorrect username and/or password', expected=True)
  50. def _real_initialize(self):
  51. self._login()
  52. def _download_payload(self, path, video_id, data, fatal=True):
  53. data['al'] = 1
  54. code, payload = self._download_json(
  55. 'https://vk.com/%s.php' % path, video_id,
  56. data=urlencode_postdata(data), fatal=fatal,
  57. headers={'X-Requested-With': 'XMLHttpRequest'})['payload']
  58. if code == '3':
  59. self.raise_login_required()
  60. elif code == '8':
  61. raise ExtractorError(clean_html(payload[0][1:-1]), expected=True)
  62. return payload
  63. class VKIE(VKBaseIE):
  64. IE_NAME = 'vk'
  65. IE_DESC = 'VK'
  66. _VALID_URL = r'''(?x)
  67. https?://
  68. (?:
  69. (?:
  70. (?:(?:m|new)\.)?vk\.com/video_|
  71. (?:www\.)?daxab.com/
  72. )
  73. ext\.php\?(?P<embed_query>.*?\boid=(?P<oid>-?\d+).*?\bid=(?P<id>\d+).*)|
  74. (?:
  75. (?:(?:m|new)\.)?vk\.com/(?:.+?\?.*?z=)?video|
  76. (?:www\.)?daxab.com/embed/
  77. )
  78. (?P<videoid>-?\d+_\d+)(?:.*\blist=(?P<list_id>[\da-f]+))?
  79. )
  80. '''
  81. _TESTS = [
  82. {
  83. 'url': 'http://vk.com/videos-77521?z=video-77521_162222515%2Fclub77521',
  84. 'md5': '7babad3b85ea2e91948005b1b8b0cb84',
  85. 'info_dict': {
  86. 'id': '-77521_162222515',
  87. 'ext': 'mp4',
  88. 'title': 'ProtivoGunz - Хуёвая песня',
  89. 'uploader': 're:(?:Noize MC|Alexander Ilyashenko).*',
  90. 'uploader_id': '-77521',
  91. 'duration': 195,
  92. 'timestamp': 1329049880,
  93. 'upload_date': '20120212',
  94. },
  95. },
  96. {
  97. 'url': 'http://vk.com/video205387401_165548505',
  98. 'info_dict': {
  99. 'id': '205387401_165548505',
  100. 'ext': 'mp4',
  101. 'title': 'No name',
  102. 'uploader': 'Tom Cruise',
  103. 'uploader_id': '205387401',
  104. 'duration': 9,
  105. 'timestamp': 1374364108,
  106. 'upload_date': '20130720',
  107. }
  108. },
  109. {
  110. 'note': 'Embedded video',
  111. 'url': 'https://vk.com/video_ext.php?oid=-77521&id=162222515&hash=87b046504ccd8bfa',
  112. 'md5': '7babad3b85ea2e91948005b1b8b0cb84',
  113. 'info_dict': {
  114. 'id': '-77521_162222515',
  115. 'ext': 'mp4',
  116. 'uploader': 're:(?:Noize MC|Alexander Ilyashenko).*',
  117. 'title': 'ProtivoGunz - Хуёвая песня',
  118. 'duration': 195,
  119. 'upload_date': '20120212',
  120. 'timestamp': 1329049880,
  121. 'uploader_id': '-77521',
  122. },
  123. },
  124. {
  125. # VIDEO NOW REMOVED
  126. # please update if you find a video whose URL follows the same pattern
  127. 'url': 'http://vk.com/video-8871596_164049491',
  128. 'md5': 'a590bcaf3d543576c9bd162812387666',
  129. 'note': 'Only available for registered users',
  130. 'info_dict': {
  131. 'id': '-8871596_164049491',
  132. 'ext': 'mp4',
  133. 'uploader': 'Триллеры',
  134. 'title': '► Бойцовский клуб / Fight Club 1999 [HD 720]',
  135. 'duration': 8352,
  136. 'upload_date': '20121218',
  137. 'view_count': int,
  138. },
  139. 'skip': 'Removed',
  140. },
  141. {
  142. 'url': 'http://vk.com/hd_kino_mania?z=video-43215063_168067957%2F15c66b9b533119788d',
  143. 'info_dict': {
  144. 'id': '-43215063_168067957',
  145. 'ext': 'mp4',
  146. 'uploader': 'Bro Mazter',
  147. 'title': ' ',
  148. 'duration': 7291,
  149. 'upload_date': '20140328',
  150. 'uploader_id': '223413403',
  151. 'timestamp': 1396018030,
  152. },
  153. 'skip': 'Requires vk account credentials',
  154. },
  155. {
  156. 'url': 'http://m.vk.com/video-43215063_169084319?list=125c627d1aa1cebb83&from=wall-43215063_2566540',
  157. 'md5': '0c45586baa71b7cb1d0784ee3f4e00a6',
  158. 'note': 'ivi.ru embed',
  159. 'info_dict': {
  160. 'id': '-43215063_169084319',
  161. 'ext': 'mp4',
  162. 'title': 'Книга Илая',
  163. 'duration': 6771,
  164. 'upload_date': '20140626',
  165. 'view_count': int,
  166. },
  167. 'skip': 'Removed',
  168. },
  169. {
  170. # video (removed?) only available with list id
  171. 'url': 'https://vk.com/video30481095_171201961?list=8764ae2d21f14088d4',
  172. 'md5': '091287af5402239a1051c37ec7b92913',
  173. 'info_dict': {
  174. 'id': '30481095_171201961',
  175. 'ext': 'mp4',
  176. 'title': 'ТюменцевВВ_09.07.2015',
  177. 'uploader': 'Anton Ivanov',
  178. 'duration': 109,
  179. 'upload_date': '20150709',
  180. 'view_count': int,
  181. },
  182. 'skip': 'Removed',
  183. },
  184. {
  185. # youtube embed
  186. 'url': 'https://vk.com/video276849682_170681728',
  187. 'info_dict': {
  188. 'id': 'V3K4mi0SYkc',
  189. 'ext': 'mp4',
  190. 'title': "DSWD Awards 'Children's Joy Foundation, Inc.' Certificate of Registration and License to Operate",
  191. 'description': 'md5:bf9c26cfa4acdfb146362682edd3827a',
  192. 'duration': 178,
  193. 'upload_date': '20130116',
  194. 'uploader': "Children's Joy Foundation Inc.",
  195. 'uploader_id': 'thecjf',
  196. 'view_count': int,
  197. },
  198. },
  199. {
  200. # dailymotion embed
  201. 'url': 'https://vk.com/video-37468416_456239855',
  202. 'info_dict': {
  203. 'id': 'k3lz2cmXyRuJQSjGHUv',
  204. 'ext': 'mp4',
  205. 'title': 'md5:d52606645c20b0ddbb21655adaa4f56f',
  206. 'description': 'md5:424b8e88cc873217f520e582ba28bb36',
  207. 'uploader': 'AniLibria.Tv',
  208. 'upload_date': '20160914',
  209. 'uploader_id': 'x1p5vl5',
  210. 'timestamp': 1473877246,
  211. },
  212. 'params': {
  213. 'skip_download': True,
  214. },
  215. },
  216. {
  217. # video key is extra_data not url\d+
  218. 'url': 'http://vk.com/video-110305615_171782105',
  219. 'md5': 'e13fcda136f99764872e739d13fac1d1',
  220. 'info_dict': {
  221. 'id': '-110305615_171782105',
  222. 'ext': 'mp4',
  223. 'title': 'S-Dance, репетиции к The way show',
  224. 'uploader': 'THE WAY SHOW | 17 апреля',
  225. 'uploader_id': '-110305615',
  226. 'timestamp': 1454859345,
  227. 'upload_date': '20160207',
  228. },
  229. 'params': {
  230. 'skip_download': True,
  231. },
  232. },
  233. {
  234. # finished live stream, postlive_mp4
  235. 'url': 'https://vk.com/videos-387766?z=video-387766_456242764%2Fpl_-387766_-2',
  236. 'info_dict': {
  237. 'id': '-387766_456242764',
  238. 'ext': 'mp4',
  239. 'title': 'ИгроМир 2016 День 1 — Игромания Утром',
  240. 'uploader': 'Игромания',
  241. 'duration': 5239,
  242. # TODO: use act=show to extract view_count
  243. # 'view_count': int,
  244. 'upload_date': '20160929',
  245. 'uploader_id': '-387766',
  246. 'timestamp': 1475137527,
  247. },
  248. 'params': {
  249. 'skip_download': True,
  250. },
  251. },
  252. {
  253. # live stream, hls and rtmp links, most likely already finished live
  254. # stream by the time you are reading this comment
  255. 'url': 'https://vk.com/video-140332_456239111',
  256. 'only_matching': True,
  257. },
  258. {
  259. # removed video, just testing that we match the pattern
  260. 'url': 'http://vk.com/feed?z=video-43215063_166094326%2Fbb50cacd3177146d7a',
  261. 'only_matching': True,
  262. },
  263. {
  264. # age restricted video, requires vk account credentials
  265. 'url': 'https://vk.com/video205387401_164765225',
  266. 'only_matching': True,
  267. },
  268. {
  269. # pladform embed
  270. 'url': 'https://vk.com/video-76116461_171554880',
  271. 'only_matching': True,
  272. },
  273. {
  274. 'url': 'http://new.vk.com/video205387401_165548505',
  275. 'only_matching': True,
  276. },
  277. {
  278. # This video is no longer available, because its author has been blocked.
  279. 'url': 'https://vk.com/video-10639516_456240611',
  280. 'only_matching': True,
  281. },
  282. {
  283. # The video is not available in your region.
  284. 'url': 'https://vk.com/video-51812607_171445436',
  285. 'only_matching': True,
  286. }]
  287. def _real_extract(self, url):
  288. mobj = re.match(self._VALID_URL, url)
  289. video_id = mobj.group('videoid')
  290. mv_data = {}
  291. if video_id:
  292. data = {
  293. 'act': 'show_inline',
  294. 'video': video_id,
  295. }
  296. # Some videos (removed?) can only be downloaded with list id specified
  297. list_id = mobj.group('list_id')
  298. if list_id:
  299. data['list'] = list_id
  300. payload = self._download_payload('al_video', video_id, data)
  301. info_page = payload[1]
  302. opts = payload[-1]
  303. mv_data = opts.get('mvData') or {}
  304. player = opts.get('player') or {}
  305. else:
  306. video_id = '%s_%s' % (mobj.group('oid'), mobj.group('id'))
  307. info_page = self._download_webpage(
  308. 'http://vk.com/video_ext.php?' + mobj.group('embed_query'), video_id)
  309. error_message = self._html_search_regex(
  310. [r'(?s)<!><div[^>]+class="video_layer_message"[^>]*>(.+?)</div>',
  311. r'(?s)<div[^>]+id="video_ext_msg"[^>]*>(.+?)</div>'],
  312. info_page, 'error message', default=None)
  313. if error_message:
  314. raise ExtractorError(error_message, expected=True)
  315. if re.search(r'<!>/login\.php\?.*\bact=security_check', info_page):
  316. raise ExtractorError(
  317. 'You are trying to log in from an unusual location. You should confirm ownership at vk.com to log in with this IP.',
  318. expected=True)
  319. ERROR_COPYRIGHT = 'Video %s has been removed from public access due to rightholder complaint.'
  320. ERRORS = {
  321. r'>Видеозапись .*? была изъята из публичного доступа в связи с обращением правообладателя.<':
  322. ERROR_COPYRIGHT,
  323. r'>The video .*? was removed from public access by request of the copyright holder.<':
  324. ERROR_COPYRIGHT,
  325. r'<!>Please log in or <':
  326. 'Video %s is only available for registered users, '
  327. 'use --username and --password options to provide account credentials.',
  328. r'<!>Unknown error':
  329. 'Video %s does not exist.',
  330. r'<!>Видео временно недоступно':
  331. 'Video %s is temporarily unavailable.',
  332. r'<!>Access denied':
  333. 'Access denied to video %s.',
  334. r'<!>Видеозапись недоступна, так как её автор был заблокирован.':
  335. 'Video %s is no longer available, because its author has been blocked.',
  336. r'<!>This video is no longer available, because its author has been blocked.':
  337. 'Video %s is no longer available, because its author has been blocked.',
  338. r'<!>This video is no longer available, because it has been deleted.':
  339. 'Video %s is no longer available, because it has been deleted.',
  340. r'<!>The video .+? is not available in your region.':
  341. 'Video %s is not available in your region.',
  342. }
  343. for error_re, error_msg in ERRORS.items():
  344. if re.search(error_re, info_page):
  345. raise ExtractorError(error_msg % video_id, expected=True)
  346. player = self._parse_json(self._search_regex(
  347. r'var\s+playerParams\s*=\s*({.+?})\s*;\s*\n',
  348. info_page, 'player params'), video_id)
  349. youtube_url = YoutubeIE._extract_url(info_page)
  350. if youtube_url:
  351. return self.url_result(youtube_url, YoutubeIE.ie_key())
  352. vimeo_url = VimeoIE._extract_url(url, info_page)
  353. if vimeo_url is not None:
  354. return self.url_result(vimeo_url, VimeoIE.ie_key())
  355. pladform_url = PladformIE._extract_url(info_page)
  356. if pladform_url:
  357. return self.url_result(pladform_url, PladformIE.ie_key())
  358. m_rutube = re.search(
  359. r'\ssrc="((?:https?:)?//rutube\.ru\\?/(?:video|play)\\?/embed(?:.*?))\\?"', info_page)
  360. if m_rutube is not None:
  361. rutube_url = self._proto_relative_url(
  362. m_rutube.group(1).replace('\\', ''))
  363. return self.url_result(rutube_url)
  364. dailymotion_urls = DailymotionIE._extract_urls(info_page)
  365. if dailymotion_urls:
  366. return self.url_result(dailymotion_urls[0], DailymotionIE.ie_key())
  367. odnoklassniki_url = OdnoklassnikiIE._extract_url(info_page)
  368. if odnoklassniki_url:
  369. return self.url_result(odnoklassniki_url, OdnoklassnikiIE.ie_key())
  370. m_opts = re.search(r'(?s)var\s+opts\s*=\s*({.+?});', info_page)
  371. if m_opts:
  372. m_opts_url = re.search(r"url\s*:\s*'((?!/\b)[^']+)", m_opts.group(1))
  373. if m_opts_url:
  374. opts_url = m_opts_url.group(1)
  375. if opts_url.startswith('//'):
  376. opts_url = 'http:' + opts_url
  377. return self.url_result(opts_url)
  378. data = player['params'][0]
  379. title = unescapeHTML(data['md_title'])
  380. # 2 = live
  381. # 3 = post live (finished live)
  382. is_live = data.get('live') == 2
  383. if is_live:
  384. title = self._live_title(title)
  385. timestamp = unified_timestamp(self._html_search_regex(
  386. r'class=["\']mv_info_date[^>]+>([^<]+)(?:<|from)', info_page,
  387. 'upload date', default=None)) or int_or_none(data.get('date'))
  388. view_count = str_to_int(self._search_regex(
  389. r'class=["\']mv_views_count[^>]+>\s*([\d,.]+)',
  390. info_page, 'view count', default=None))
  391. formats = []
  392. for format_id, format_url in data.items():
  393. format_url = url_or_none(format_url)
  394. if not format_url or not format_url.startswith(('http', '//', 'rtmp')):
  395. continue
  396. if (format_id.startswith(('url', 'cache'))
  397. or format_id in ('extra_data', 'live_mp4', 'postlive_mp4')):
  398. height = int_or_none(self._search_regex(
  399. r'^(?:url|cache)(\d+)', format_id, 'height', default=None))
  400. formats.append({
  401. 'format_id': format_id,
  402. 'url': format_url,
  403. 'height': height,
  404. })
  405. elif format_id == 'hls':
  406. formats.extend(self._extract_m3u8_formats(
  407. format_url, video_id, 'mp4', 'm3u8_native',
  408. m3u8_id=format_id, fatal=False, live=is_live))
  409. elif format_id == 'rtmp':
  410. formats.append({
  411. 'format_id': format_id,
  412. 'url': format_url,
  413. 'ext': 'flv',
  414. })
  415. self._sort_formats(formats)
  416. return {
  417. 'id': video_id,
  418. 'formats': formats,
  419. 'title': title,
  420. 'thumbnail': data.get('jpg'),
  421. 'uploader': data.get('md_author'),
  422. 'uploader_id': str_or_none(data.get('author_id') or mv_data.get('authorId')),
  423. 'duration': int_or_none(data.get('duration') or mv_data.get('duration')),
  424. 'timestamp': timestamp,
  425. 'view_count': view_count,
  426. 'like_count': int_or_none(mv_data.get('likes')),
  427. 'comment_count': int_or_none(mv_data.get('commcount')),
  428. 'is_live': is_live,
  429. }
  430. class VKUserVideosIE(VKBaseIE):
  431. IE_NAME = 'vk:uservideos'
  432. IE_DESC = "VK - User's Videos"
  433. _VALID_URL = r'https?://(?:(?:m|new)\.)?vk\.com/videos(?P<id>-?[0-9]+)(?!\?.*\bz=video)(?:[/?#&](?:.*?\bsection=(?P<section>\w+))?|$)'
  434. _TEMPLATE_URL = 'https://vk.com/videos'
  435. _TESTS = [{
  436. 'url': 'https://vk.com/videos-767561',
  437. 'info_dict': {
  438. 'id': '-767561_all',
  439. },
  440. 'playlist_mincount': 1150,
  441. }, {
  442. 'url': 'https://vk.com/videos-767561?section=uploaded',
  443. 'info_dict': {
  444. 'id': '-767561_uploaded',
  445. },
  446. 'playlist_mincount': 425,
  447. }, {
  448. 'url': 'http://vk.com/videos205387401',
  449. 'only_matching': True,
  450. }, {
  451. 'url': 'http://vk.com/videos-77521',
  452. 'only_matching': True,
  453. }, {
  454. 'url': 'http://vk.com/videos-97664626?section=all',
  455. 'only_matching': True,
  456. }, {
  457. 'url': 'http://m.vk.com/videos205387401',
  458. 'only_matching': True,
  459. }, {
  460. 'url': 'http://new.vk.com/videos205387401',
  461. 'only_matching': True,
  462. }]
  463. _PAGE_SIZE = 1000
  464. _VIDEO = collections.namedtuple('Video', ['owner_id', 'id'])
  465. def _fetch_page(self, page_id, section, page):
  466. l = self._download_payload('al_video', page_id, {
  467. 'act': 'load_videos_silent',
  468. 'offset': page * self._PAGE_SIZE,
  469. 'oid': page_id,
  470. 'section': section,
  471. })[0][section]['list']
  472. for video in l:
  473. v = self._VIDEO._make(video[:2])
  474. video_id = '%d_%d' % (v.owner_id, v.id)
  475. yield self.url_result(
  476. 'http://vk.com/video' + video_id, VKIE.ie_key(), video_id)
  477. def _real_extract(self, url):
  478. page_id, section = re.match(self._VALID_URL, url).groups()
  479. if not section:
  480. section = 'all'
  481. entries = OnDemandPagedList(
  482. functools.partial(self._fetch_page, page_id, section),
  483. self._PAGE_SIZE)
  484. return self.playlist_result(entries, '%s_%s' % (page_id, section))
  485. class VKWallPostIE(VKBaseIE):
  486. IE_NAME = 'vk:wallpost'
  487. _VALID_URL = r'https?://(?:(?:(?:(?:m|new)\.)?vk\.com/(?:[^?]+\?.*\bw=)?wall(?P<id>-?\d+_\d+)))'
  488. _TESTS = [{
  489. # public page URL, audio playlist
  490. 'url': 'https://vk.com/bs.official?w=wall-23538238_35',
  491. 'info_dict': {
  492. 'id': '-23538238_35',
  493. 'title': 'Black Shadow - Wall post -23538238_35',
  494. 'description': 'md5:3f84b9c4f9ef499731cf1ced9998cc0c',
  495. },
  496. 'playlist': [{
  497. 'md5': '5ba93864ec5b85f7ce19a9af4af080f6',
  498. 'info_dict': {
  499. 'id': '135220665_111806521',
  500. 'ext': 'mp4',
  501. 'title': 'Black Shadow - Слепое Верование',
  502. 'duration': 370,
  503. 'uploader': 'Black Shadow',
  504. 'artist': 'Black Shadow',
  505. 'track': 'Слепое Верование',
  506. },
  507. }, {
  508. 'md5': '4cc7e804579122b17ea95af7834c9233',
  509. 'info_dict': {
  510. 'id': '135220665_111802303',
  511. 'ext': 'mp4',
  512. 'title': 'Black Shadow - Война - Негасимое Бездны Пламя!',
  513. 'duration': 423,
  514. 'uploader': 'Black Shadow',
  515. 'artist': 'Black Shadow',
  516. 'track': 'Война - Негасимое Бездны Пламя!',
  517. },
  518. }],
  519. 'params': {
  520. 'skip_download': True,
  521. 'usenetrc': True,
  522. },
  523. 'skip': 'Requires vk account credentials',
  524. }, {
  525. # single YouTube embed, no leading -
  526. 'url': 'https://vk.com/wall85155021_6319',
  527. 'info_dict': {
  528. 'id': '85155021_6319',
  529. 'title': 'Сергей Горбунов - Wall post 85155021_6319',
  530. },
  531. 'playlist_count': 1,
  532. 'params': {
  533. 'usenetrc': True,
  534. },
  535. 'skip': 'Requires vk account credentials',
  536. }, {
  537. # wall page URL
  538. 'url': 'https://vk.com/wall-23538238_35',
  539. 'only_matching': True,
  540. }, {
  541. # mobile wall page URL
  542. 'url': 'https://m.vk.com/wall-23538238_35',
  543. 'only_matching': True,
  544. }]
  545. _BASE64_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN0PQRSTUVWXYZO123456789+/='
  546. _AUDIO = collections.namedtuple('Audio', ['id', 'owner_id', 'url', 'title', 'performer', 'duration', 'album_id', 'unk', 'author_link', 'lyrics', 'flags', 'context', 'extra', 'hashes', 'cover_url', 'ads'])
  547. def _decode(self, enc):
  548. dec = ''
  549. e = n = 0
  550. for c in enc:
  551. r = self._BASE64_CHARS.index(c)
  552. cond = n % 4
  553. e = 64 * e + r if cond else r
  554. n += 1
  555. if cond:
  556. dec += chr(255 & e >> (-2 * n & 6))
  557. return dec
  558. def _unmask_url(self, mask_url, vk_id):
  559. if 'audio_api_unavailable' in mask_url:
  560. extra = mask_url.split('?extra=')[1].split('#')
  561. func, base = self._decode(extra[1]).split(chr(11))
  562. mask_url = list(self._decode(extra[0]))
  563. url_len = len(mask_url)
  564. indexes = [None] * url_len
  565. index = int(base) ^ vk_id
  566. for n in range(url_len - 1, -1, -1):
  567. index = (url_len * (n + 1) ^ index + n) % url_len
  568. indexes[n] = index
  569. for n in range(1, url_len):
  570. c = mask_url[n]
  571. index = indexes[url_len - 1 - n]
  572. mask_url[n] = mask_url[index]
  573. mask_url[index] = c
  574. mask_url = ''.join(mask_url)
  575. return mask_url
  576. def _real_extract(self, url):
  577. post_id = self._match_id(url)
  578. webpage = self._download_payload('wkview', post_id, {
  579. 'act': 'show',
  580. 'w': 'wall' + post_id,
  581. })[1]
  582. description = clean_html(get_element_by_class('wall_post_text', webpage))
  583. uploader = clean_html(get_element_by_class('author', webpage))
  584. entries = []
  585. for audio in re.findall(r'data-audio="([^"]+)', webpage):
  586. audio = self._parse_json(unescapeHTML(audio), post_id)
  587. a = self._AUDIO._make(audio[:16])
  588. if not a.url:
  589. continue
  590. title = unescapeHTML(a.title)
  591. performer = unescapeHTML(a.performer)
  592. entries.append({
  593. 'id': '%s_%s' % (a.owner_id, a.id),
  594. 'url': self._unmask_url(a.url, a.ads['vk_id']),
  595. 'title': '%s - %s' % (performer, title) if performer else title,
  596. 'thumbnails': [{'url': c_url} for c_url in a.cover_url.split(',')] if a.cover_url else None,
  597. 'duration': int_or_none(a.duration),
  598. 'uploader': uploader,
  599. 'artist': performer,
  600. 'track': title,
  601. 'ext': 'mp4',
  602. 'protocol': 'm3u8',
  603. })
  604. for video in re.finditer(
  605. r'<a[^>]+href=(["\'])(?P<url>/video(?:-?[\d_]+).*?)\1', webpage):
  606. entries.append(self.url_result(
  607. compat_urlparse.urljoin(url, video.group('url')), VKIE.ie_key()))
  608. title = 'Wall post %s' % post_id
  609. return self.playlist_result(
  610. orderedSet(entries), post_id,
  611. '%s - %s' % (uploader, title) if uploader else title,
  612. description)