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