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.

128 lines
4.6 KiB

  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..compat import (
  4. compat_urllib_parse,
  5. compat_urllib_request,
  6. )
  7. from ..utils import (
  8. ExtractorError,
  9. js_to_json,
  10. parse_duration,
  11. )
  12. class EscapistIE(InfoExtractor):
  13. _VALID_URL = r'https?://?(www\.)?escapistmagazine\.com/videos/view/[^/?#]+/(?P<id>[0-9]+)-[^/?#]*(?:$|[?#])'
  14. _USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'
  15. _TEST = {
  16. 'url': 'http://www.escapistmagazine.com/videos/view/the-escapist-presents/6618-Breaking-Down-Baldurs-Gate',
  17. 'md5': 'ab3a706c681efca53f0a35f1415cf0d1',
  18. 'info_dict': {
  19. 'id': '6618',
  20. 'ext': 'mp4',
  21. 'description': "Baldur's Gate: Original, Modded or Enhanced Edition? I'll break down what you can expect from the new Baldur's Gate: Enhanced Edition.",
  22. 'uploader_id': 'the-escapist-presents',
  23. 'uploader': 'The Escapist Presents',
  24. 'title': "Breaking Down Baldur's Gate",
  25. 'thumbnail': 're:^https?://.*\.jpg$',
  26. 'duration': 264,
  27. }
  28. }
  29. def _real_extract(self, url):
  30. video_id = self._match_id(url)
  31. webpage_req = compat_urllib_request.Request(url)
  32. webpage_req.add_header('User-Agent', self._USER_AGENT)
  33. webpage = self._download_webpage(webpage_req, video_id)
  34. uploader_id = self._html_search_regex(
  35. r"<h1\s+class='headline'>\s*<a\s+href='/videos/view/(.*?)'",
  36. webpage, 'uploader ID', fatal=False)
  37. uploader = self._html_search_regex(
  38. r"<h1\s+class='headline'>(.*?)</a>",
  39. webpage, 'uploader', fatal=False)
  40. description = self._html_search_meta('description', webpage)
  41. duration = parse_duration(self._html_search_meta('duration', webpage))
  42. raw_title = self._html_search_meta('title', webpage, fatal=True)
  43. title = raw_title.partition(' : ')[2]
  44. config_url = compat_urllib_parse.unquote(self._html_search_regex(
  45. r'''(?x)
  46. (?:
  47. <param\s+name="flashvars".*?\s+value="config=|
  48. flashvars=&quot;config=
  49. )
  50. (https?://[^"&]+)
  51. ''',
  52. webpage, 'config URL'))
  53. formats = []
  54. ad_formats = []
  55. def _add_format(name, cfg_url, quality):
  56. cfg_req = compat_urllib_request.Request(cfg_url)
  57. cfg_req.add_header('User-Agent', self._USER_AGENT)
  58. config = self._download_json(
  59. cfg_req, video_id,
  60. 'Downloading ' + name + ' configuration',
  61. 'Unable to download ' + name + ' configuration',
  62. transform_source=js_to_json)
  63. playlist = config['playlist']
  64. for p in playlist:
  65. if p.get('eventCategory') == 'Video':
  66. ar = formats
  67. elif p.get('eventCategory') == 'Video Postroll':
  68. ar = ad_formats
  69. else:
  70. continue
  71. ar.append({
  72. 'url': p['url'],
  73. 'format_id': name,
  74. 'quality': quality,
  75. 'http_headers': {
  76. 'User-Agent': self._USER_AGENT,
  77. },
  78. })
  79. _add_format('normal', config_url, quality=0)
  80. hq_url = (config_url +
  81. ('&hq=1' if '?' in config_url else config_url + '?hq=1'))
  82. try:
  83. _add_format('hq', hq_url, quality=1)
  84. except ExtractorError:
  85. pass # That's fine, we'll just use normal quality
  86. self._sort_formats(formats)
  87. if '/escapist/sales-marketing/' in formats[-1]['url']:
  88. raise ExtractorError('This IP address has been blocked by The Escapist', expected=True)
  89. res = {
  90. 'id': video_id,
  91. 'formats': formats,
  92. 'uploader': uploader,
  93. 'uploader_id': uploader_id,
  94. 'title': title,
  95. 'thumbnail': self._og_search_thumbnail(webpage),
  96. 'description': description,
  97. 'duration': duration,
  98. }
  99. if self._downloader.params.get('include_ads') and ad_formats:
  100. self._sort_formats(ad_formats)
  101. ad_res = {
  102. 'id': '%s-ad' % video_id,
  103. 'title': '%s (Postroll)' % title,
  104. 'formats': ad_formats,
  105. }
  106. return {
  107. '_type': 'playlist',
  108. 'entries': [res, ad_res],
  109. 'title': title,
  110. 'id': video_id,
  111. }
  112. return res