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.

108 lines
4.8 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. def _formats_sort_key(f):
  73. type_ord = 1 if f['type'] == 'video/mp4' else 0
  74. quality_ord = 1 if f['quality'] == 'high' else 0
  75. return (quality_ord, type_ord)
  76. best_format = sorted(video_info['sources'][0], key=_formats_sort_key)[-1]
  77. return {'id': video_id,
  78. 'url': best_format['src'],
  79. 'ext': best_format['type'].split('/')[-1],
  80. 'title': video_info['title'],
  81. 'thumbnail': video_info['poster_frame'],
  82. 'description': description,
  83. }
  84. def _real_extract(self, url):
  85. mobj = re.match(self._VALID_URL, url)
  86. site = mobj.group('site')
  87. url_type = mobj.group('type')
  88. id = mobj.group('id')
  89. self.to_screen(u'Extracting from %s with the Condé Nast extractor' % self._SITES[site])
  90. webpage = self._download_webpage(url, id)
  91. if url_type == 'series':
  92. return self._extract_series(url, webpage)
  93. else:
  94. return self._extract_video(webpage)