vimeo.py 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import re
  5. import itertools
  6. from .common import InfoExtractor
  7. from ..compat import (
  8. compat_HTTPError,
  9. compat_str,
  10. compat_urlparse,
  11. )
  12. from ..utils import (
  13. determine_ext,
  14. ExtractorError,
  15. InAdvancePagedList,
  16. int_or_none,
  17. NO_DEFAULT,
  18. RegexNotFoundError,
  19. sanitized_Request,
  20. smuggle_url,
  21. std_headers,
  22. unified_strdate,
  23. unsmuggle_url,
  24. urlencode_postdata,
  25. unescapeHTML,
  26. parse_filesize,
  27. try_get,
  28. )
  29. class VimeoBaseInfoExtractor(InfoExtractor):
  30. _NETRC_MACHINE = 'vimeo'
  31. _LOGIN_REQUIRED = False
  32. _LOGIN_URL = 'https://vimeo.com/log_in'
  33. def _login(self):
  34. (username, password) = self._get_login_info()
  35. if username is None:
  36. if self._LOGIN_REQUIRED:
  37. raise ExtractorError('No login info available, needed for using %s.' % self.IE_NAME, expected=True)
  38. return
  39. self.report_login()
  40. webpage = self._download_webpage(self._LOGIN_URL, None, False)
  41. token, vuid = self._extract_xsrft_and_vuid(webpage)
  42. data = urlencode_postdata({
  43. 'action': 'login',
  44. 'email': username,
  45. 'password': password,
  46. 'service': 'vimeo',
  47. 'token': token,
  48. })
  49. login_request = sanitized_Request(self._LOGIN_URL, data)
  50. login_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  51. login_request.add_header('Referer', self._LOGIN_URL)
  52. self._set_vimeo_cookie('vuid', vuid)
  53. self._download_webpage(login_request, None, False, 'Wrong login info')
  54. def _verify_video_password(self, url, video_id, webpage):
  55. password = self._downloader.params.get('videopassword')
  56. if password is None:
  57. raise ExtractorError('This video is protected by a password, use the --video-password option', expected=True)
  58. token, vuid = self._extract_xsrft_and_vuid(webpage)
  59. data = urlencode_postdata({
  60. 'password': password,
  61. 'token': token,
  62. })
  63. if url.startswith('http://'):
  64. # vimeo only supports https now, but the user can give an http url
  65. url = url.replace('http://', 'https://')
  66. password_request = sanitized_Request(url + '/password', data)
  67. password_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  68. password_request.add_header('Referer', url)
  69. self._set_vimeo_cookie('vuid', vuid)
  70. return self._download_webpage(
  71. password_request, video_id,
  72. 'Verifying the password', 'Wrong password')
  73. def _extract_xsrft_and_vuid(self, webpage):
  74. xsrft = self._search_regex(
  75. r'(?:(?P<q1>["\'])xsrft(?P=q1)\s*:|xsrft\s*[=:])\s*(?P<q>["\'])(?P<xsrft>.+?)(?P=q)',
  76. webpage, 'login token', group='xsrft')
  77. vuid = self._search_regex(
  78. r'["\']vuid["\']\s*:\s*(["\'])(?P<vuid>.+?)\1',
  79. webpage, 'vuid', group='vuid')
  80. return xsrft, vuid
  81. def _set_vimeo_cookie(self, name, value):
  82. self._set_cookie('vimeo.com', name, value)
  83. def _vimeo_sort_formats(self, formats):
  84. # Bitrates are completely broken. Single m3u8 may contain entries in kbps and bps
  85. # at the same time without actual units specified. This lead to wrong sorting.
  86. self._sort_formats(formats, field_preference=('preference', 'height', 'width', 'fps', 'tbr', 'format_id'))
  87. def _parse_config(self, config, video_id):
  88. video_data = config['video']
  89. # Extract title
  90. video_title = video_data['title']
  91. # Extract uploader, uploader_url and uploader_id
  92. video_uploader = video_data.get('owner', {}).get('name')
  93. video_uploader_url = video_data.get('owner', {}).get('url')
  94. video_uploader_id = video_uploader_url.split('/')[-1] if video_uploader_url else None
  95. # Extract video thumbnail
  96. video_thumbnail = video_data.get('thumbnail')
  97. if video_thumbnail is None:
  98. video_thumbs = video_data.get('thumbs')
  99. if video_thumbs and isinstance(video_thumbs, dict):
  100. _, video_thumbnail = sorted((int(width if width.isdigit() else 0), t_url) for (width, t_url) in video_thumbs.items())[-1]
  101. # Extract video duration
  102. video_duration = int_or_none(video_data.get('duration'))
  103. formats = []
  104. config_files = video_data.get('files') or config['request'].get('files', {})
  105. for f in config_files.get('progressive', []):
  106. video_url = f.get('url')
  107. if not video_url:
  108. continue
  109. formats.append({
  110. 'url': video_url,
  111. 'format_id': 'http-%s' % f.get('quality'),
  112. 'width': int_or_none(f.get('width')),
  113. 'height': int_or_none(f.get('height')),
  114. 'fps': int_or_none(f.get('fps')),
  115. 'tbr': int_or_none(f.get('bitrate')),
  116. })
  117. for files_type in ('hls', 'dash'):
  118. for cdn_name, cdn_data in config_files.get(files_type, {}).get('cdns', {}).items():
  119. manifest_url = cdn_data.get('url')
  120. if not manifest_url:
  121. continue
  122. format_id = '%s-%s' % (files_type, cdn_name)
  123. if files_type == 'hls':
  124. formats.extend(self._extract_m3u8_formats(
  125. manifest_url, video_id, 'mp4',
  126. 'm3u8_native', m3u8_id=format_id,
  127. note='Downloading %s m3u8 information' % cdn_name,
  128. fatal=False))
  129. elif files_type == 'dash':
  130. mpd_pattern = r'/%s/(?:sep/)?video/' % video_id
  131. mpd_manifest_urls = []
  132. if re.search(mpd_pattern, manifest_url):
  133. for suffix, repl in (('', 'video'), ('_sep', 'sep/video')):
  134. mpd_manifest_urls.append((format_id + suffix, re.sub(
  135. mpd_pattern, '/%s/%s/' % (video_id, repl), manifest_url)))
  136. else:
  137. mpd_manifest_urls = [(format_id, manifest_url)]
  138. for f_id, m_url in mpd_manifest_urls:
  139. formats.extend(self._extract_mpd_formats(
  140. m_url.replace('/master.json', '/master.mpd'), video_id, f_id,
  141. 'Downloading %s MPD information' % cdn_name,
  142. fatal=False))
  143. subtitles = {}
  144. text_tracks = config['request'].get('text_tracks')
  145. if text_tracks:
  146. for tt in text_tracks:
  147. subtitles[tt['lang']] = [{
  148. 'ext': 'vtt',
  149. 'url': 'https://vimeo.com' + tt['url'],
  150. }]
  151. return {
  152. 'title': video_title,
  153. 'uploader': video_uploader,
  154. 'uploader_id': video_uploader_id,
  155. 'uploader_url': video_uploader_url,
  156. 'thumbnail': video_thumbnail,
  157. 'duration': video_duration,
  158. 'formats': formats,
  159. 'subtitles': subtitles,
  160. }
  161. class VimeoIE(VimeoBaseInfoExtractor):
  162. """Information extractor for vimeo.com."""
  163. # _VALID_URL matches Vimeo URLs
  164. _VALID_URL = r'''(?x)
  165. https?://
  166. (?:
  167. (?:
  168. www|
  169. (?P<player>player)
  170. )
  171. \.
  172. )?
  173. vimeo(?P<pro>pro)?\.com/
  174. (?!(?:channels|album)/[^/?#]+/?(?:$|[?#])|[^/]+/review/|ondemand/)
  175. (?:.*?/)?
  176. (?:
  177. (?:
  178. play_redirect_hls|
  179. moogaloop\.swf)\?clip_id=
  180. )?
  181. (?:videos?/)?
  182. (?P<id>[0-9]+)
  183. (?:/[\da-f]+)?
  184. /?(?:[?&].*)?(?:[#].*)?$
  185. '''
  186. IE_NAME = 'vimeo'
  187. _TESTS = [
  188. {
  189. 'url': 'http://vimeo.com/56015672#at=0',
  190. 'md5': '8879b6cc097e987f02484baf890129e5',
  191. 'info_dict': {
  192. 'id': '56015672',
  193. 'ext': 'mp4',
  194. 'title': "youtube-dl test video - \u2605 \" ' \u5e78 / \\ \u00e4 \u21ad \U0001d550",
  195. 'description': 'md5:2d3305bad981a06ff79f027f19865021',
  196. 'upload_date': '20121220',
  197. 'uploader_url': r're:https?://(?:www\.)?vimeo\.com/user7108434',
  198. 'uploader_id': 'user7108434',
  199. 'uploader': 'Filippo Valsorda',
  200. 'duration': 10,
  201. 'license': 'by-sa',
  202. },
  203. },
  204. {
  205. 'url': 'http://vimeopro.com/openstreetmapus/state-of-the-map-us-2013/video/68093876',
  206. 'md5': '3b5ca6aa22b60dfeeadf50b72e44ed82',
  207. 'note': 'Vimeo Pro video (#1197)',
  208. 'info_dict': {
  209. 'id': '68093876',
  210. 'ext': 'mp4',
  211. 'uploader_url': r're:https?://(?:www\.)?vimeo\.com/openstreetmapus',
  212. 'uploader_id': 'openstreetmapus',
  213. 'uploader': 'OpenStreetMap US',
  214. 'title': 'Andy Allan - Putting the Carto into OpenStreetMap Cartography',
  215. 'description': 'md5:fd69a7b8d8c34a4e1d2ec2e4afd6ec30',
  216. 'duration': 1595,
  217. },
  218. },
  219. {
  220. 'url': 'http://player.vimeo.com/video/54469442',
  221. 'md5': '619b811a4417aa4abe78dc653becf511',
  222. 'note': 'Videos that embed the url in the player page',
  223. 'info_dict': {
  224. 'id': '54469442',
  225. 'ext': 'mp4',
  226. 'title': 'Kathy Sierra: Building the minimum Badass User, Business of Software 2012',
  227. 'uploader': 'The BLN & Business of Software',
  228. 'uploader_url': r're:https?://(?:www\.)?vimeo\.com/theblnbusinessofsoftware',
  229. 'uploader_id': 'theblnbusinessofsoftware',
  230. 'duration': 3610,
  231. 'description': None,
  232. },
  233. },
  234. {
  235. 'url': 'http://vimeo.com/68375962',
  236. 'md5': 'aaf896bdb7ddd6476df50007a0ac0ae7',
  237. 'note': 'Video protected with password',
  238. 'info_dict': {
  239. 'id': '68375962',
  240. 'ext': 'mp4',
  241. 'title': 'youtube-dl password protected test video',
  242. 'upload_date': '20130614',
  243. 'uploader_url': r're:https?://(?:www\.)?vimeo\.com/user18948128',
  244. 'uploader_id': 'user18948128',
  245. 'uploader': 'Jaime Marquínez Ferrándiz',
  246. 'duration': 10,
  247. 'description': 'md5:dca3ea23adb29ee387127bc4ddfce63f',
  248. },
  249. 'params': {
  250. 'videopassword': 'youtube-dl',
  251. },
  252. },
  253. {
  254. 'url': 'http://vimeo.com/channels/keypeele/75629013',
  255. 'md5': '2f86a05afe9d7abc0b9126d229bbe15d',
  256. 'info_dict': {
  257. 'id': '75629013',
  258. 'ext': 'mp4',
  259. 'title': 'Key & Peele: Terrorist Interrogation',
  260. 'description': 'md5:8678b246399b070816b12313e8b4eb5c',
  261. 'uploader_url': r're:https?://(?:www\.)?vimeo\.com/atencio',
  262. 'uploader_id': 'atencio',
  263. 'uploader': 'Peter Atencio',
  264. 'upload_date': '20130927',
  265. 'duration': 187,
  266. },
  267. },
  268. {
  269. 'url': 'http://vimeo.com/76979871',
  270. 'note': 'Video with subtitles',
  271. 'info_dict': {
  272. 'id': '76979871',
  273. 'ext': 'mp4',
  274. 'title': 'The New Vimeo Player (You Know, For Videos)',
  275. 'description': 'md5:2ec900bf97c3f389378a96aee11260ea',
  276. 'upload_date': '20131015',
  277. 'uploader_url': r're:https?://(?:www\.)?vimeo\.com/staff',
  278. 'uploader_id': 'staff',
  279. 'uploader': 'Vimeo Staff',
  280. 'duration': 62,
  281. }
  282. },
  283. {
  284. # from https://www.ouya.tv/game/Pier-Solar-and-the-Great-Architects/
  285. 'url': 'https://player.vimeo.com/video/98044508',
  286. 'note': 'The js code contains assignments to the same variable as the config',
  287. 'info_dict': {
  288. 'id': '98044508',
  289. 'ext': 'mp4',
  290. 'title': 'Pier Solar OUYA Official Trailer',
  291. 'uploader': 'Tulio Gonçalves',
  292. 'uploader_url': r're:https?://(?:www\.)?vimeo\.com/user28849593',
  293. 'uploader_id': 'user28849593',
  294. },
  295. },
  296. {
  297. # contains original format
  298. 'url': 'https://vimeo.com/33951933',
  299. 'md5': '53c688fa95a55bf4b7293d37a89c5c53',
  300. 'info_dict': {
  301. 'id': '33951933',
  302. 'ext': 'mp4',
  303. 'title': 'FOX CLASSICS - Forever Classic ID - A Full Minute',
  304. 'uploader': 'The DMCI',
  305. 'uploader_url': r're:https?://(?:www\.)?vimeo\.com/dmci',
  306. 'uploader_id': 'dmci',
  307. 'upload_date': '20111220',
  308. 'description': 'md5:ae23671e82d05415868f7ad1aec21147',
  309. },
  310. },
  311. {
  312. # only available via https://vimeo.com/channels/tributes/6213729 and
  313. # not via https://vimeo.com/6213729
  314. 'url': 'https://vimeo.com/channels/tributes/6213729',
  315. 'info_dict': {
  316. 'id': '6213729',
  317. 'ext': 'mov',
  318. 'title': 'Vimeo Tribute: The Shining',
  319. 'uploader': 'Casey Donahue',
  320. 'uploader_url': r're:https?://(?:www\.)?vimeo\.com/caseydonahue',
  321. 'uploader_id': 'caseydonahue',
  322. 'upload_date': '20090821',
  323. 'description': 'md5:bdbf314014e58713e6e5b66eb252f4a6',
  324. },
  325. 'params': {
  326. 'skip_download': True,
  327. },
  328. 'expected_warnings': ['Unable to download JSON metadata'],
  329. },
  330. {
  331. # redirects to ondemand extractor and should be passed through it
  332. # for successful extraction
  333. 'url': 'https://vimeo.com/73445910',
  334. 'info_dict': {
  335. 'id': '73445910',
  336. 'ext': 'mp4',
  337. 'title': 'The Reluctant Revolutionary',
  338. 'uploader': '10Ft Films',
  339. 'uploader_url': r're:https?://(?:www\.)?vimeo\.com/tenfootfilms',
  340. 'uploader_id': 'tenfootfilms',
  341. },
  342. 'params': {
  343. 'skip_download': True,
  344. },
  345. },
  346. {
  347. 'url': 'http://vimeo.com/moogaloop.swf?clip_id=2539741',
  348. 'only_matching': True,
  349. },
  350. {
  351. 'url': 'https://vimeo.com/109815029',
  352. 'note': 'Video not completely processed, "failed" seed status',
  353. 'only_matching': True,
  354. },
  355. {
  356. 'url': 'https://vimeo.com/groups/travelhd/videos/22439234',
  357. 'only_matching': True,
  358. },
  359. {
  360. 'url': 'https://vimeo.com/album/2632481/video/79010983',
  361. 'only_matching': True,
  362. },
  363. {
  364. # source file returns 403: Forbidden
  365. 'url': 'https://vimeo.com/7809605',
  366. 'only_matching': True,
  367. },
  368. {
  369. 'url': 'https://vimeo.com/160743502/abd0e13fb4',
  370. 'only_matching': True,
  371. }
  372. ]
  373. @staticmethod
  374. def _smuggle_referrer(url, referrer_url):
  375. return smuggle_url(url, {'http_headers': {'Referer': referrer_url}})
  376. @staticmethod
  377. def _extract_urls(url, webpage):
  378. urls = []
  379. # Look for embedded (iframe) Vimeo player
  380. for mobj in re.finditer(
  381. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//player\.vimeo\.com/video/.+?)\1',
  382. webpage):
  383. urls.append(VimeoIE._smuggle_referrer(unescapeHTML(mobj.group('url')), url))
  384. PLAIN_EMBED_RE = (
  385. # Look for embedded (swf embed) Vimeo player
  386. r'<embed[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?vimeo\.com/moogaloop\.swf.+?)\1',
  387. # Look more for non-standard embedded Vimeo player
  388. r'<video[^>]+src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?vimeo\.com/[0-9]+)\1',
  389. )
  390. for embed_re in PLAIN_EMBED_RE:
  391. for mobj in re.finditer(embed_re, webpage):
  392. urls.append(mobj.group('url'))
  393. return urls
  394. @staticmethod
  395. def _extract_url(url, webpage):
  396. urls = VimeoIE._extract_urls(url, webpage)
  397. return urls[0] if urls else None
  398. def _verify_player_video_password(self, url, video_id):
  399. password = self._downloader.params.get('videopassword')
  400. if password is None:
  401. raise ExtractorError('This video is protected by a password, use the --video-password option')
  402. data = urlencode_postdata({'password': password})
  403. pass_url = url + '/check-password'
  404. password_request = sanitized_Request(pass_url, data)
  405. password_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  406. password_request.add_header('Referer', url)
  407. return self._download_json(
  408. password_request, video_id,
  409. 'Verifying the password', 'Wrong password')
  410. def _real_initialize(self):
  411. self._login()
  412. def _real_extract(self, url):
  413. url, data = unsmuggle_url(url, {})
  414. headers = std_headers.copy()
  415. if 'http_headers' in data:
  416. headers.update(data['http_headers'])
  417. if 'Referer' not in headers:
  418. headers['Referer'] = url
  419. # Extract ID from URL
  420. mobj = re.match(self._VALID_URL, url)
  421. video_id = mobj.group('id')
  422. orig_url = url
  423. if mobj.group('pro') or mobj.group('player'):
  424. url = 'https://player.vimeo.com/video/' + video_id
  425. elif any(p in url for p in ('play_redirect_hls', 'moogaloop.swf')):
  426. url = 'https://vimeo.com/' + video_id
  427. # Retrieve video webpage to extract further information
  428. request = sanitized_Request(url, headers=headers)
  429. try:
  430. webpage, urlh = self._download_webpage_handle(request, video_id)
  431. # Some URLs redirect to ondemand can't be extracted with
  432. # this extractor right away thus should be passed through
  433. # ondemand extractor (e.g. https://vimeo.com/73445910)
  434. if VimeoOndemandIE.suitable(urlh.geturl()):
  435. return self.url_result(urlh.geturl(), VimeoOndemandIE.ie_key())
  436. except ExtractorError as ee:
  437. if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 403:
  438. errmsg = ee.cause.read()
  439. if b'Because of its privacy settings, this video cannot be played here' in errmsg:
  440. raise ExtractorError(
  441. 'Cannot download embed-only video without embedding '
  442. 'URL. Please call youtube-dl with the URL of the page '
  443. 'that embeds this video.',
  444. expected=True)
  445. raise
  446. # Now we begin extracting as much information as we can from what we
  447. # retrieved. First we extract the information common to all extractors,
  448. # and latter we extract those that are Vimeo specific.
  449. self.report_extraction(video_id)
  450. vimeo_config = self._search_regex(
  451. r'vimeo\.config\s*=\s*(?:({.+?})|_extend\([^,]+,\s+({.+?})\));', webpage,
  452. 'vimeo config', default=None)
  453. if vimeo_config:
  454. seed_status = self._parse_json(vimeo_config, video_id).get('seed_status', {})
  455. if seed_status.get('state') == 'failed':
  456. raise ExtractorError(
  457. '%s said: %s' % (self.IE_NAME, seed_status['title']),
  458. expected=True)
  459. cc_license = None
  460. # Extract the config JSON
  461. try:
  462. try:
  463. config_url = self._html_search_regex(
  464. r' data-config-url="(.+?)"', webpage,
  465. 'config URL', default=None)
  466. if not config_url:
  467. # Sometimes new react-based page is served instead of old one that require
  468. # different config URL extraction approach (see
  469. # https://github.com/rg3/youtube-dl/pull/7209)
  470. vimeo_clip_page_config = self._search_regex(
  471. r'vimeo\.clip_page_config\s*=\s*({.+?});', webpage,
  472. 'vimeo clip page config')
  473. page_config = self._parse_json(vimeo_clip_page_config, video_id)
  474. config_url = page_config['player']['config_url']
  475. cc_license = page_config.get('cc_license')
  476. config_json = self._download_webpage(config_url, video_id)
  477. config = json.loads(config_json)
  478. except RegexNotFoundError:
  479. # For pro videos or player.vimeo.com urls
  480. # We try to find out to which variable is assigned the config dic
  481. m_variable_name = re.search(r'(\w)\.video\.id', webpage)
  482. if m_variable_name is not None:
  483. config_re = r'%s=({[^}].+?});' % re.escape(m_variable_name.group(1))
  484. else:
  485. config_re = [r' = {config:({.+?}),assets:', r'(?:[abc])=({.+?});']
  486. config = self._search_regex(config_re, webpage, 'info section',
  487. flags=re.DOTALL)
  488. config = json.loads(config)
  489. except Exception as e:
  490. if re.search('The creator of this video has not given you permission to embed it on this domain.', webpage):
  491. raise ExtractorError('The author has restricted the access to this video, try with the "--referer" option')
  492. if re.search(r'<form[^>]+?id="pw_form"', webpage) is not None:
  493. if '_video_password_verified' in data:
  494. raise ExtractorError('video password verification failed!')
  495. self._verify_video_password(url, video_id, webpage)
  496. return self._real_extract(
  497. smuggle_url(url, {'_video_password_verified': 'verified'}))
  498. else:
  499. raise ExtractorError('Unable to extract info section',
  500. cause=e)
  501. else:
  502. if config.get('view') == 4:
  503. config = self._verify_player_video_password(url, video_id)
  504. def is_rented():
  505. if '>You rented this title.<' in webpage:
  506. return True
  507. if config.get('user', {}).get('purchased'):
  508. return True
  509. label = try_get(
  510. config, lambda x: x['video']['vod']['purchase_options'][0]['label_string'], compat_str)
  511. if label and label.startswith('You rented this'):
  512. return True
  513. return False
  514. if is_rented():
  515. feature_id = config.get('video', {}).get('vod', {}).get('feature_id')
  516. if feature_id and not data.get('force_feature_id', False):
  517. return self.url_result(smuggle_url(
  518. 'https://player.vimeo.com/player/%s' % feature_id,
  519. {'force_feature_id': True}), 'Vimeo')
  520. # Extract video description
  521. video_description = self._html_search_regex(
  522. r'(?s)<div\s+class="[^"]*description[^"]*"[^>]*>(.*?)</div>',
  523. webpage, 'description', default=None)
  524. if not video_description:
  525. video_description = self._html_search_meta(
  526. 'description', webpage, default=None)
  527. if not video_description and mobj.group('pro'):
  528. orig_webpage = self._download_webpage(
  529. orig_url, video_id,
  530. note='Downloading webpage for description',
  531. fatal=False)
  532. if orig_webpage:
  533. video_description = self._html_search_meta(
  534. 'description', orig_webpage, default=None)
  535. if not video_description and not mobj.group('player'):
  536. self._downloader.report_warning('Cannot find video description')
  537. # Extract upload date
  538. video_upload_date = None
  539. mobj = re.search(r'<time[^>]+datetime="([^"]+)"', webpage)
  540. if mobj is not None:
  541. video_upload_date = unified_strdate(mobj.group(1))
  542. try:
  543. view_count = int(self._search_regex(r'UserPlays:(\d+)', webpage, 'view count'))
  544. like_count = int(self._search_regex(r'UserLikes:(\d+)', webpage, 'like count'))
  545. comment_count = int(self._search_regex(r'UserComments:(\d+)', webpage, 'comment count'))
  546. except RegexNotFoundError:
  547. # This info is only available in vimeo.com/{id} urls
  548. view_count = None
  549. like_count = None
  550. comment_count = None
  551. formats = []
  552. download_request = sanitized_Request('https://vimeo.com/%s?action=load_download_config' % video_id, headers={
  553. 'X-Requested-With': 'XMLHttpRequest'})
  554. download_data = self._download_json(download_request, video_id, fatal=False)
  555. if download_data:
  556. source_file = download_data.get('source_file')
  557. if isinstance(source_file, dict):
  558. download_url = source_file.get('download_url')
  559. if download_url and not source_file.get('is_cold') and not source_file.get('is_defrosting'):
  560. source_name = source_file.get('public_name', 'Original')
  561. if self._is_valid_url(download_url, video_id, '%s video' % source_name):
  562. ext = source_file.get('extension', determine_ext(download_url)).lower()
  563. formats.append({
  564. 'url': download_url,
  565. 'ext': ext,
  566. 'width': int_or_none(source_file.get('width')),
  567. 'height': int_or_none(source_file.get('height')),
  568. 'filesize': parse_filesize(source_file.get('size')),
  569. 'format_id': source_name,
  570. 'preference': 1,
  571. })
  572. info_dict = self._parse_config(config, video_id)
  573. formats.extend(info_dict['formats'])
  574. self._vimeo_sort_formats(formats)
  575. if not cc_license:
  576. cc_license = self._search_regex(
  577. r'<link[^>]+rel=["\']license["\'][^>]+href=(["\'])(?P<license>(?:(?!\1).)+)\1',
  578. webpage, 'license', default=None, group='license')
  579. info_dict.update({
  580. 'id': video_id,
  581. 'formats': formats,
  582. 'upload_date': video_upload_date,
  583. 'description': video_description,
  584. 'webpage_url': url,
  585. 'view_count': view_count,
  586. 'like_count': like_count,
  587. 'comment_count': comment_count,
  588. 'license': cc_license,
  589. })
  590. return info_dict
  591. class VimeoOndemandIE(VimeoBaseInfoExtractor):
  592. IE_NAME = 'vimeo:ondemand'
  593. _VALID_URL = r'https?://(?:www\.)?vimeo\.com/ondemand/(?P<id>[^/?#&]+)'
  594. _TESTS = [{
  595. # ondemand video not available via https://vimeo.com/id
  596. 'url': 'https://vimeo.com/ondemand/20704',
  597. 'md5': 'c424deda8c7f73c1dfb3edd7630e2f35',
  598. 'info_dict': {
  599. 'id': '105442900',
  600. 'ext': 'mp4',
  601. 'title': 'המעבדה - במאי יותם פלדמן',
  602. 'uploader': 'גם סרטים',
  603. 'uploader_url': r're:https?://(?:www\.)?vimeo\.com/gumfilms',
  604. 'uploader_id': 'gumfilms',
  605. },
  606. 'params': {
  607. 'format': 'best[protocol=https]',
  608. },
  609. }, {
  610. # requires Referer to be passed along with og:video:url
  611. 'url': 'https://vimeo.com/ondemand/36938/126682985',
  612. 'info_dict': {
  613. 'id': '126682985',
  614. 'ext': 'mp4',
  615. 'title': 'Rävlock, rätt läte på rätt plats',
  616. 'uploader': 'Lindroth & Norin',
  617. 'uploader_url': r're:https?://(?:www\.)?vimeo\.com/user14430847',
  618. 'uploader_id': 'user14430847',
  619. },
  620. 'params': {
  621. 'skip_download': True,
  622. },
  623. }, {
  624. 'url': 'https://vimeo.com/ondemand/nazmaalik',
  625. 'only_matching': True,
  626. }, {
  627. 'url': 'https://vimeo.com/ondemand/141692381',
  628. 'only_matching': True,
  629. }, {
  630. 'url': 'https://vimeo.com/ondemand/thelastcolony/150274832',
  631. 'only_matching': True,
  632. }]
  633. def _real_extract(self, url):
  634. video_id = self._match_id(url)
  635. webpage = self._download_webpage(url, video_id)
  636. return self.url_result(
  637. # Some videos require Referer to be passed along with og:video:url
  638. # similarly to generic vimeo embeds (e.g.
  639. # https://vimeo.com/ondemand/36938/126682985).
  640. VimeoIE._smuggle_referrer(self._og_search_video_url(webpage), url),
  641. VimeoIE.ie_key())
  642. class VimeoChannelIE(VimeoBaseInfoExtractor):
  643. IE_NAME = 'vimeo:channel'
  644. _VALID_URL = r'https://vimeo\.com/channels/(?P<id>[^/?#]+)/?(?:$|[?#])'
  645. _MORE_PAGES_INDICATOR = r'<a.+?rel="next"'
  646. _TITLE = None
  647. _TITLE_RE = r'<link rel="alternate"[^>]+?title="(.*?)"'
  648. _TESTS = [{
  649. 'url': 'https://vimeo.com/channels/tributes',
  650. 'info_dict': {
  651. 'id': 'tributes',
  652. 'title': 'Vimeo Tributes',
  653. },
  654. 'playlist_mincount': 25,
  655. }]
  656. def _page_url(self, base_url, pagenum):
  657. return '%s/videos/page:%d/' % (base_url, pagenum)
  658. def _extract_list_title(self, webpage):
  659. return self._TITLE or self._html_search_regex(self._TITLE_RE, webpage, 'list title')
  660. def _login_list_password(self, page_url, list_id, webpage):
  661. login_form = self._search_regex(
  662. r'(?s)<form[^>]+?id="pw_form"(.*?)</form>',
  663. webpage, 'login form', default=None)
  664. if not login_form:
  665. return webpage
  666. password = self._downloader.params.get('videopassword')
  667. if password is None:
  668. raise ExtractorError('This album is protected by a password, use the --video-password option', expected=True)
  669. fields = self._hidden_inputs(login_form)
  670. token, vuid = self._extract_xsrft_and_vuid(webpage)
  671. fields['token'] = token
  672. fields['password'] = password
  673. post = urlencode_postdata(fields)
  674. password_path = self._search_regex(
  675. r'action="([^"]+)"', login_form, 'password URL')
  676. password_url = compat_urlparse.urljoin(page_url, password_path)
  677. password_request = sanitized_Request(password_url, post)
  678. password_request.add_header('Content-type', 'application/x-www-form-urlencoded')
  679. self._set_vimeo_cookie('vuid', vuid)
  680. self._set_vimeo_cookie('xsrft', token)
  681. return self._download_webpage(
  682. password_request, list_id,
  683. 'Verifying the password', 'Wrong password')
  684. def _title_and_entries(self, list_id, base_url):
  685. for pagenum in itertools.count(1):
  686. page_url = self._page_url(base_url, pagenum)
  687. webpage = self._download_webpage(
  688. page_url, list_id,
  689. 'Downloading page %s' % pagenum)
  690. if pagenum == 1:
  691. webpage = self._login_list_password(page_url, list_id, webpage)
  692. yield self._extract_list_title(webpage)
  693. # Try extracting href first since not all videos are available via
  694. # short https://vimeo.com/id URL (e.g. https://vimeo.com/channels/tributes/6213729)
  695. clips = re.findall(
  696. r'id="clip_(\d+)"[^>]*>\s*<a[^>]+href="(/(?:[^/]+/)*\1)(?:[^>]+\btitle="([^"]+)")?', webpage)
  697. if clips:
  698. for video_id, video_url, video_title in clips:
  699. yield self.url_result(
  700. compat_urlparse.urljoin(base_url, video_url),
  701. VimeoIE.ie_key(), video_id=video_id, video_title=video_title)
  702. # More relaxed fallback
  703. else:
  704. for video_id in re.findall(r'id=["\']clip_(\d+)', webpage):
  705. yield self.url_result(
  706. 'https://vimeo.com/%s' % video_id,
  707. VimeoIE.ie_key(), video_id=video_id)
  708. if re.search(self._MORE_PAGES_INDICATOR, webpage, re.DOTALL) is None:
  709. break
  710. def _extract_videos(self, list_id, base_url):
  711. title_and_entries = self._title_and_entries(list_id, base_url)
  712. list_title = next(title_and_entries)
  713. return self.playlist_result(title_and_entries, list_id, list_title)
  714. def _real_extract(self, url):
  715. mobj = re.match(self._VALID_URL, url)
  716. channel_id = mobj.group('id')
  717. return self._extract_videos(channel_id, 'https://vimeo.com/channels/%s' % channel_id)
  718. class VimeoUserIE(VimeoChannelIE):
  719. IE_NAME = 'vimeo:user'
  720. _VALID_URL = r'https://vimeo\.com/(?!(?:[0-9]+|watchlater)(?:$|[?#/]))(?P<name>[^/]+)(?:/videos|[#?]|$)'
  721. _TITLE_RE = r'<a[^>]+?class="user">([^<>]+?)</a>'
  722. _TESTS = [{
  723. 'url': 'https://vimeo.com/nkistudio/videos',
  724. 'info_dict': {
  725. 'title': 'Nki',
  726. 'id': 'nkistudio',
  727. },
  728. 'playlist_mincount': 66,
  729. }]
  730. def _real_extract(self, url):
  731. mobj = re.match(self._VALID_URL, url)
  732. name = mobj.group('name')
  733. return self._extract_videos(name, 'https://vimeo.com/%s' % name)
  734. class VimeoAlbumIE(VimeoChannelIE):
  735. IE_NAME = 'vimeo:album'
  736. _VALID_URL = r'https://vimeo\.com/album/(?P<id>\d+)(?:$|[?#]|/(?!video))'
  737. _TITLE_RE = r'<header id="page_header">\n\s*<h1>(.*?)</h1>'
  738. _TESTS = [{
  739. 'url': 'https://vimeo.com/album/2632481',
  740. 'info_dict': {
  741. 'id': '2632481',
  742. 'title': 'Staff Favorites: November 2013',
  743. },
  744. 'playlist_mincount': 13,
  745. }, {
  746. 'note': 'Password-protected album',
  747. 'url': 'https://vimeo.com/album/3253534',
  748. 'info_dict': {
  749. 'title': 'test',
  750. 'id': '3253534',
  751. },
  752. 'playlist_count': 1,
  753. 'params': {
  754. 'videopassword': 'youtube-dl',
  755. }
  756. }, {
  757. 'url': 'https://vimeo.com/album/2632481/sort:plays/format:thumbnail',
  758. 'only_matching': True,
  759. }, {
  760. # TODO: respect page number
  761. 'url': 'https://vimeo.com/album/2632481/page:2/sort:plays/format:thumbnail',
  762. 'only_matching': True,
  763. }]
  764. def _page_url(self, base_url, pagenum):
  765. return '%s/page:%d/' % (base_url, pagenum)
  766. def _real_extract(self, url):
  767. album_id = self._match_id(url)
  768. return self._extract_videos(album_id, 'https://vimeo.com/album/%s' % album_id)
  769. class VimeoGroupsIE(VimeoAlbumIE):
  770. IE_NAME = 'vimeo:group'
  771. _VALID_URL = r'https://vimeo\.com/groups/(?P<name>[^/]+)(?:/(?!videos?/\d+)|$)'
  772. _TESTS = [{
  773. 'url': 'https://vimeo.com/groups/rolexawards',
  774. 'info_dict': {
  775. 'id': 'rolexawards',
  776. 'title': 'Rolex Awards for Enterprise',
  777. },
  778. 'playlist_mincount': 73,
  779. }]
  780. def _extract_list_title(self, webpage):
  781. return self._og_search_title(webpage)
  782. def _real_extract(self, url):
  783. mobj = re.match(self._VALID_URL, url)
  784. name = mobj.group('name')
  785. return self._extract_videos(name, 'https://vimeo.com/groups/%s' % name)
  786. class VimeoReviewIE(VimeoBaseInfoExtractor):
  787. IE_NAME = 'vimeo:review'
  788. IE_DESC = 'Review pages on vimeo'
  789. _VALID_URL = r'https://vimeo\.com/[^/]+/review/(?P<id>[^/]+)'
  790. _TESTS = [{
  791. 'url': 'https://vimeo.com/user21297594/review/75524534/3c257a1b5d',
  792. 'md5': 'c507a72f780cacc12b2248bb4006d253',
  793. 'info_dict': {
  794. 'id': '75524534',
  795. 'ext': 'mp4',
  796. 'title': "DICK HARDWICK 'Comedian'",
  797. 'uploader': 'Richard Hardwick',
  798. 'uploader_id': 'user21297594',
  799. }
  800. }, {
  801. 'note': 'video player needs Referer',
  802. 'url': 'https://vimeo.com/user22258446/review/91613211/13f927e053',
  803. 'md5': '6295fdab8f4bf6a002d058b2c6dce276',
  804. 'info_dict': {
  805. 'id': '91613211',
  806. 'ext': 'mp4',
  807. 'title': 're:(?i)^Death by dogma versus assembling agile . Sander Hoogendoorn',
  808. 'uploader': 'DevWeek Events',
  809. 'duration': 2773,
  810. 'thumbnail': r're:^https?://.*\.jpg$',
  811. 'uploader_id': 'user22258446',
  812. }
  813. }, {
  814. 'note': 'Password protected',
  815. 'url': 'https://vimeo.com/user37284429/review/138823582/c4d865efde',
  816. 'info_dict': {
  817. 'id': '138823582',
  818. 'ext': 'mp4',
  819. 'title': 'EFFICIENT PICKUP MASTERCLASS MODULE 1',
  820. 'uploader': 'TMB',
  821. 'uploader_id': 'user37284429',
  822. },
  823. 'params': {
  824. 'videopassword': 'holygrail',
  825. },
  826. 'skip': 'video gone',
  827. }]
  828. def _real_initialize(self):
  829. self._login()
  830. def _get_config_url(self, webpage_url, video_id, video_password_verified=False):
  831. webpage = self._download_webpage(webpage_url, video_id)
  832. config_url = self._html_search_regex(
  833. r'data-config-url=(["\'])(?P<url>(?:(?!\1).)+)\1', webpage,
  834. 'config URL', default=None, group='url')
  835. if not config_url:
  836. data = self._parse_json(self._search_regex(
  837. r'window\s*=\s*_extend\(window,\s*({.+?})\);', webpage, 'data',
  838. default=NO_DEFAULT if video_password_verified else '{}'), video_id)
  839. config_url = data.get('vimeo_esi', {}).get('config', {}).get('configUrl')
  840. if config_url is None:
  841. self._verify_video_password(webpage_url, video_id, webpage)
  842. config_url = self._get_config_url(
  843. webpage_url, video_id, video_password_verified=True)
  844. return config_url
  845. def _real_extract(self, url):
  846. video_id = self._match_id(url)
  847. config_url = self._get_config_url(url, video_id)
  848. config = self._download_json(config_url, video_id)
  849. info_dict = self._parse_config(config, video_id)
  850. self._vimeo_sort_formats(info_dict['formats'])
  851. info_dict['id'] = video_id
  852. return info_dict
  853. class VimeoWatchLaterIE(VimeoChannelIE):
  854. IE_NAME = 'vimeo:watchlater'
  855. IE_DESC = 'Vimeo watch later list, "vimeowatchlater" keyword (requires authentication)'
  856. _VALID_URL = r'https://vimeo\.com/(?:home/)?watchlater|:vimeowatchlater'
  857. _TITLE = 'Watch Later'
  858. _LOGIN_REQUIRED = True
  859. _TESTS = [{
  860. 'url': 'https://vimeo.com/watchlater',
  861. 'only_matching': True,
  862. }]
  863. def _real_initialize(self):
  864. self._login()
  865. def _page_url(self, base_url, pagenum):
  866. url = '%s/page:%d/' % (base_url, pagenum)
  867. request = sanitized_Request(url)
  868. # Set the header to get a partial html page with the ids,
  869. # the normal page doesn't contain them.
  870. request.add_header('X-Requested-With', 'XMLHttpRequest')
  871. return request
  872. def _real_extract(self, url):
  873. return self._extract_videos('watchlater', 'https://vimeo.com/watchlater')
  874. class VimeoLikesIE(InfoExtractor):
  875. _VALID_URL = r'https://(?:www\.)?vimeo\.com/user(?P<id>[0-9]+)/likes/?(?:$|[?#]|sort:)'
  876. IE_NAME = 'vimeo:likes'
  877. IE_DESC = 'Vimeo user likes'
  878. _TEST = {
  879. 'url': 'https://vimeo.com/user755559/likes/',
  880. 'playlist_mincount': 293,
  881. 'info_dict': {
  882. 'id': 'user755559_likes',
  883. 'description': 'See all the videos urza likes',
  884. 'title': 'Videos urza likes',
  885. },
  886. }
  887. def _real_extract(self, url):
  888. user_id = self._match_id(url)
  889. webpage = self._download_webpage(url, user_id)
  890. page_count = self._int(
  891. self._search_regex(
  892. r'''(?x)<li><a\s+href="[^"]+"\s+data-page="([0-9]+)">
  893. .*?</a></li>\s*<li\s+class="pagination_next">
  894. ''', webpage, 'page count'),
  895. 'page count', fatal=True)
  896. PAGE_SIZE = 12
  897. title = self._html_search_regex(
  898. r'(?s)<h1>(.+?)</h1>', webpage, 'title', fatal=False)
  899. description = self._html_search_meta('description', webpage)
  900. def _get_page(idx):
  901. page_url = 'https://vimeo.com/user%s/likes/page:%d/sort:date' % (
  902. user_id, idx + 1)
  903. webpage = self._download_webpage(
  904. page_url, user_id,
  905. note='Downloading page %d/%d' % (idx + 1, page_count))
  906. video_list = self._search_regex(
  907. r'(?s)<ol class="js-browse_list[^"]+"[^>]*>(.*?)</ol>',
  908. webpage, 'video content')
  909. paths = re.findall(
  910. r'<li[^>]*>\s*<a\s+href="([^"]+)"', video_list)
  911. for path in paths:
  912. yield {
  913. '_type': 'url',
  914. 'url': compat_urlparse.urljoin(page_url, path),
  915. }
  916. pl = InAdvancePagedList(_get_page, page_count, PAGE_SIZE)
  917. return {
  918. '_type': 'playlist',
  919. 'id': 'user%s_likes' % user_id,
  920. 'title': title,
  921. 'description': description,
  922. 'entries': pl,
  923. }