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.

120 lines
5.0 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. from .common import InfoExtractor
  6. from ..compat import (
  7. compat_urllib_parse,
  8. compat_urllib_parse_urlparse,
  9. compat_urlparse,
  10. )
  11. from ..utils import (
  12. orderedSet,
  13. )
  14. class CondeNastIE(InfoExtractor):
  15. """
  16. Condé Nast is a media group, some of its sites use a custom HTML5 player
  17. that works the same in all of them.
  18. """
  19. # The keys are the supported sites and the values are the name to be shown
  20. # to the user and in the extractor description.
  21. _SITES = {
  22. 'wired': 'WIRED',
  23. 'gq': 'GQ',
  24. 'vogue': 'Vogue',
  25. 'glamour': 'Glamour',
  26. 'wmagazine': 'W Magazine',
  27. 'vanityfair': 'Vanity Fair',
  28. 'cnevids': 'Condé Nast',
  29. }
  30. _VALID_URL = r'http://(video|www|player)\.(?P<site>%s)\.com/(?P<type>watch|series|video|embed)/(?P<id>[^/?#]+)' % '|'.join(_SITES.keys())
  31. IE_DESC = 'Condé Nast media group: %s' % ', '.join(sorted(_SITES.values()))
  32. EMBED_URL = r'(?:https?:)?//player\.(?P<site>%s)\.com/(?P<type>embed)/.+?' % '|'.join(_SITES.keys())
  33. _TEST = {
  34. 'url': 'http://video.wired.com/watch/3d-printed-speakers-lit-with-led',
  35. 'md5': '1921f713ed48aabd715691f774c451f7',
  36. 'info_dict': {
  37. 'id': '5171b343c2b4c00dd0c1ccb3',
  38. 'ext': 'mp4',
  39. 'title': '3D Printed Speakers Lit With LED',
  40. '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.',
  41. }
  42. }
  43. def _extract_series(self, url, webpage):
  44. title = self._html_search_regex(r'<div class="cne-series-info">.*?<h1>(.+?)</h1>',
  45. webpage, 'series title', flags=re.DOTALL)
  46. url_object = compat_urllib_parse_urlparse(url)
  47. base_url = '%s://%s' % (url_object.scheme, url_object.netloc)
  48. m_paths = re.finditer(r'<p class="cne-thumb-title">.*?<a href="(/watch/.+?)["\?]',
  49. webpage, flags=re.DOTALL)
  50. paths = orderedSet(m.group(1) for m in m_paths)
  51. build_url = lambda path: compat_urlparse.urljoin(base_url, path)
  52. entries = [self.url_result(build_url(path), 'CondeNast') for path in paths]
  53. return self.playlist_result(entries, playlist_title=title)
  54. def _extract_video(self, webpage, url_type):
  55. if url_type != 'embed':
  56. description = self._html_search_regex(
  57. [
  58. r'<div class="cne-video-description">(.+?)</div>',
  59. r'<div class="video-post-content">(.+?)</div>',
  60. ],
  61. webpage, 'description', fatal=False, flags=re.DOTALL)
  62. else:
  63. description = None
  64. params = self._search_regex(r'var params = {(.+?)}[;,]', webpage,
  65. 'player params', flags=re.DOTALL)
  66. video_id = self._search_regex(r'videoId: [\'"](.+?)[\'"]', params, 'video id')
  67. player_id = self._search_regex(r'playerId: [\'"](.+?)[\'"]', params, 'player id')
  68. target = self._search_regex(r'target: [\'"](.+?)[\'"]', params, 'target')
  69. data = compat_urllib_parse.urlencode({'videoId': video_id,
  70. 'playerId': player_id,
  71. 'target': target,
  72. })
  73. base_info_url = self._search_regex(r'url = [\'"](.+?)[\'"][,;]',
  74. webpage, 'base info url',
  75. default='http://player.cnevids.com/player/loader.js?')
  76. info_url = base_info_url + data
  77. info_page = self._download_webpage(info_url, video_id,
  78. 'Downloading video info')
  79. video_info = self._search_regex(r'var video = ({.+?});', info_page, 'video info')
  80. video_info = json.loads(video_info)
  81. formats = [{
  82. 'format_id': '%s-%s' % (fdata['type'].split('/')[-1], fdata['quality']),
  83. 'url': fdata['src'],
  84. 'ext': fdata['type'].split('/')[-1],
  85. 'quality': 1 if fdata['quality'] == 'high' else 0,
  86. } for fdata in video_info['sources'][0]]
  87. self._sort_formats(formats)
  88. return {
  89. 'id': video_id,
  90. 'formats': formats,
  91. 'title': video_info['title'],
  92. 'thumbnail': video_info['poster_frame'],
  93. 'description': description,
  94. }
  95. def _real_extract(self, url):
  96. mobj = re.match(self._VALID_URL, url)
  97. site = mobj.group('site')
  98. url_type = mobj.group('type')
  99. item_id = mobj.group('id')
  100. self.to_screen('Extracting from %s with the Condé Nast extractor' % self._SITES[site])
  101. webpage = self._download_webpage(url, item_id)
  102. if url_type == 'series':
  103. return self._extract_series(url, webpage)
  104. else:
  105. return self._extract_video(webpage, url_type)