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.

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