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.

110 lines
4.7 KiB

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