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.

177 lines
6.9 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. video_info = None
  107. info_page = self._download_webpage(
  108. 'http://player.cnevids.com/player/video.js',
  109. video_id, 'Downloading video info', query=query, fatal=False)
  110. if info_page:
  111. video_info = self._parse_json(self._search_regex(
  112. r'loadCallback\(({.+})\)', info_page, 'video info'), video_id)['video']
  113. else:
  114. info_page = self._download_webpage(
  115. 'http://player.cnevids.com/player/loader.js',
  116. video_id, 'Downloading loader info', query=query)
  117. video_info = self._parse_json(self._search_regex(
  118. r'var\s+video\s*=\s*({.+?});', info_page, 'video info'), video_id)
  119. title = video_info['title']
  120. formats = []
  121. for fdata in video_info.get('sources', [{}])[0]:
  122. src = fdata.get('src')
  123. if not src:
  124. continue
  125. ext = mimetype2ext(fdata.get('type')) or determine_ext(src)
  126. quality = fdata.get('quality')
  127. formats.append({
  128. 'format_id': ext + ('-%s' % quality if quality else ''),
  129. 'url': src,
  130. 'ext': ext,
  131. 'quality': 1 if quality == 'high' else 0,
  132. })
  133. self._sort_formats(formats)
  134. info = self._search_json_ld(webpage, video_id) if url_type != 'embed' else {}
  135. info.update({
  136. 'id': video_id,
  137. 'formats': formats,
  138. 'title': title,
  139. 'thumbnail': video_info.get('poster_frame'),
  140. 'uploader': video_info.get('brand'),
  141. 'duration': int_or_none(video_info.get('duration')),
  142. 'tags': video_info.get('tags'),
  143. 'series': video_info.get('series_title'),
  144. 'season': video_info.get('season_title'),
  145. 'timestamp': parse_iso8601(video_info.get('premiere_date')),
  146. })
  147. return info
  148. def _real_extract(self, url):
  149. site, url_type, item_id = re.match(self._VALID_URL, url).groups()
  150. # Convert JS embed to regular embed
  151. if url_type == 'embedjs':
  152. parsed_url = compat_urlparse.urlparse(url)
  153. url = compat_urlparse.urlunparse(parsed_url._replace(
  154. path=remove_end(parsed_url.path, '.js').replace('/embedjs/', '/embed/')))
  155. url_type = 'embed'
  156. self.to_screen('Extracting from %s with the Condé Nast extractor' % self._SITES[site])
  157. webpage = self._download_webpage(url, item_id)
  158. if url_type == 'series':
  159. return self._extract_series(url, webpage)
  160. else:
  161. return self._extract_video(webpage, url_type)