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.

169 lines
6.5 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_urllib_parse_urlparse,
  7. compat_urlparse,
  8. )
  9. from ..utils import (
  10. orderedSet,
  11. remove_end,
  12. extract_attributes,
  13. mimetype2ext,
  14. determine_ext,
  15. int_or_none,
  16. parse_iso8601,
  17. )
  18. class CondeNastIE(InfoExtractor):
  19. """
  20. Condé Nast is a media group, some of its sites use a custom HTML5 player
  21. that works the same in all of them.
  22. """
  23. # The keys are the supported sites and the values are the name to be shown
  24. # to the user and in the extractor description.
  25. _SITES = {
  26. 'allure': 'Allure',
  27. 'architecturaldigest': 'Architectural Digest',
  28. 'arstechnica': 'Ars Technica',
  29. 'bonappetit': 'Bon Appétit',
  30. 'brides': 'Brides',
  31. 'cnevids': 'Condé Nast',
  32. 'cntraveler': 'Condé Nast Traveler',
  33. 'details': 'Details',
  34. 'epicurious': 'Epicurious',
  35. 'glamour': 'Glamour',
  36. 'golfdigest': 'Golf Digest',
  37. 'gq': 'GQ',
  38. 'newyorker': 'The New Yorker',
  39. 'self': 'SELF',
  40. 'teenvogue': 'Teen Vogue',
  41. 'vanityfair': 'Vanity Fair',
  42. 'vogue': 'Vogue',
  43. 'wired': 'WIRED',
  44. 'wmagazine': 'W Magazine',
  45. }
  46. _VALID_URL = r'https?://(?:video|www|player)\.(?P<site>%s)\.com/(?P<type>watch|series|video|embed(?:js)?)/(?P<id>[^/?#]+)' % '|'.join(_SITES.keys())
  47. IE_DESC = 'Condé Nast media group: %s' % ', '.join(sorted(_SITES.values()))
  48. EMBED_URL = r'(?:https?:)?//player\.(?P<site>%s)\.com/(?P<type>embed(?:js)?)/.+?' % '|'.join(_SITES.keys())
  49. _TESTS = [{
  50. 'url': 'http://video.wired.com/watch/3d-printed-speakers-lit-with-led',
  51. 'md5': '1921f713ed48aabd715691f774c451f7',
  52. 'info_dict': {
  53. 'id': '5171b343c2b4c00dd0c1ccb3',
  54. 'ext': 'mp4',
  55. 'title': '3D Printed Speakers Lit With LED',
  56. 'description': 'Check out these beautiful 3D printed LED speakers. You can\'t actually buy them, but LumiGeek is working on a board that will let you make you\'re own.',
  57. 'uploader': 'wired',
  58. 'upload_date': '20130314',
  59. 'timestamp': 1363219200,
  60. }
  61. }, {
  62. # JS embed
  63. 'url': 'http://player.cnevids.com/embedjs/55f9cf8b61646d1acf00000c/5511d76261646d5566020000.js',
  64. 'md5': 'f1a6f9cafb7083bab74a710f65d08999',
  65. 'info_dict': {
  66. 'id': '55f9cf8b61646d1acf00000c',
  67. 'ext': 'mp4',
  68. 'title': '3D printed TSA Travel Sentry keys really do open TSA locks',
  69. 'uploader': 'arstechnica',
  70. 'upload_date': '20150916',
  71. 'timestamp': 1442434955,
  72. }
  73. }]
  74. def _extract_series(self, url, webpage):
  75. title = self._html_search_regex(
  76. r'(?s)<div class="cne-series-info">.*?<h1>(.+?)</h1>',
  77. webpage, 'series title')
  78. url_object = compat_urllib_parse_urlparse(url)
  79. base_url = '%s://%s' % (url_object.scheme, url_object.netloc)
  80. m_paths = re.finditer(
  81. r'(?s)<p class="cne-thumb-title">.*?<a href="(/watch/.+?)["\?]', webpage)
  82. paths = orderedSet(m.group(1) for m in m_paths)
  83. build_url = lambda path: compat_urlparse.urljoin(base_url, path)
  84. entries = [self.url_result(build_url(path), 'CondeNast') for path in paths]
  85. return self.playlist_result(entries, playlist_title=title)
  86. def _extract_video(self, webpage, url_type):
  87. query = {}
  88. params = self._search_regex(
  89. r'(?s)var params = {(.+?)}[;,]', webpage, 'player params', default=None)
  90. if params:
  91. query.update({
  92. 'videoId': self._search_regex(r'videoId: [\'"](.+?)[\'"]', params, 'video id'),
  93. 'playerId': self._search_regex(r'playerId: [\'"](.+?)[\'"]', params, 'player id'),
  94. 'target': self._search_regex(r'target: [\'"](.+?)[\'"]', params, 'target'),
  95. })
  96. else:
  97. params = extract_attributes(self._search_regex(
  98. r'(<[^>]+data-js="video-player"[^>]+>)',
  99. webpage, 'player params element'))
  100. query.update({
  101. 'videoId': params['data-video'],
  102. 'playerId': params['data-player'],
  103. 'target': params['id'],
  104. })
  105. video_id = query['videoId']
  106. info_page = self._download_webpage(
  107. 'http://player.cnevids.com/player/video.js',
  108. video_id, 'Downloading video info', query=query)
  109. video_info = self._parse_json(self._search_regex(
  110. r'loadCallback\(({.+})\)', info_page, 'video info'), video_id)['video']
  111. title = video_info['title']
  112. formats = []
  113. for fdata in video_info.get('sources', [{}])[0]:
  114. src = fdata.get('src')
  115. if not src:
  116. continue
  117. ext = mimetype2ext(fdata.get('type')) or determine_ext(src)
  118. quality = fdata.get('quality')
  119. formats.append({
  120. 'format_id': ext + ('-%s' % quality if quality else ''),
  121. 'url': src,
  122. 'ext': ext,
  123. 'quality': 1 if quality == 'high' else 0,
  124. })
  125. self._sort_formats(formats)
  126. info = self._search_json_ld(webpage, video_id) if url_type != 'embed' else {}
  127. info.update({
  128. 'id': video_id,
  129. 'formats': formats,
  130. 'title': title,
  131. 'thumbnail': video_info.get('poster_frame'),
  132. 'uploader': video_info.get('brand'),
  133. 'duration': int_or_none(video_info.get('duration')),
  134. 'tags': video_info.get('tags'),
  135. 'series': video_info.get('series_title'),
  136. 'season': video_info.get('season_title'),
  137. 'timestamp': parse_iso8601(video_info.get('premiere_date')),
  138. })
  139. return info
  140. def _real_extract(self, url):
  141. site, url_type, item_id = re.match(self._VALID_URL, url).groups()
  142. # Convert JS embed to regular embed
  143. if url_type == 'embedjs':
  144. parsed_url = compat_urlparse.urlparse(url)
  145. url = compat_urlparse.urlunparse(parsed_url._replace(
  146. path=remove_end(parsed_url.path, '.js').replace('/embedjs/', '/embed/')))
  147. url_type = 'embed'
  148. self.to_screen('Extracting from %s with the Condé Nast extractor' % self._SITES[site])
  149. webpage = self._download_webpage(url, item_id)
  150. if url_type == 'series':
  151. return self._extract_series(url, webpage)
  152. else:
  153. return self._extract_video(webpage, url_type)