Fix "invalid escape sequences" error on Python 3.6
[youtube-dl] / youtube_dl / extractor / voicerepublic.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_str,
8     compat_urlparse,
9 )
10 from ..utils import (
11     ExtractorError,
12     determine_ext,
13     int_or_none,
14     sanitized_Request,
15 )
16
17
18 class VoiceRepublicIE(InfoExtractor):
19     _VALID_URL = r'https?://voicerepublic\.com/(?:talks|embed)/(?P<id>[0-9a-z-]+)'
20     _TESTS = [{
21         'url': 'http://voicerepublic.com/talks/watching-the-watchers-building-a-sousveillance-state',
22         'md5': 'b9174d651323f17783000876347116e3',
23         'info_dict': {
24             'id': '2296',
25             'display_id': 'watching-the-watchers-building-a-sousveillance-state',
26             'ext': 'm4a',
27             'title': 'Watching the Watchers: Building a Sousveillance State',
28             'description': 'Secret surveillance programs have metadata too. The people and companies that operate secret surveillance programs can be surveilled.',
29             'thumbnail': r're:^https?://.*\.(?:png|jpg)$',
30             'duration': 1800,
31             'view_count': int,
32         }
33     }, {
34         'url': 'http://voicerepublic.com/embed/watching-the-watchers-building-a-sousveillance-state',
35         'only_matching': True,
36     }]
37
38     def _real_extract(self, url):
39         display_id = self._match_id(url)
40
41         req = sanitized_Request(
42             compat_urlparse.urljoin(url, '/talks/%s' % display_id))
43         # Older versions of Firefox get redirected to an "upgrade browser" page
44         req.add_header('User-Agent', 'youtube-dl')
45         webpage = self._download_webpage(req, display_id)
46
47         if '>Queued for processing, please stand by...<' in webpage:
48             raise ExtractorError(
49                 'Audio is still queued for processing', expected=True)
50
51         config = self._search_regex(
52             r'(?s)return ({.+?});\s*\n', webpage,
53             'data', default=None)
54         data = self._parse_json(config, display_id, fatal=False) if config else None
55         if data:
56             title = data['title']
57             description = data.get('teaser')
58             talk_id = compat_str(data.get('talk_id') or display_id)
59             talk = data['talk']
60             duration = int_or_none(talk.get('duration'))
61             formats = [{
62                 'url': compat_urlparse.urljoin(url, talk_url),
63                 'format_id': format_id,
64                 'ext': determine_ext(talk_url) or format_id,
65                 'vcodec': 'none',
66             } for format_id, talk_url in talk['links'].items()]
67         else:
68             title = self._og_search_title(webpage)
69             description = self._html_search_regex(
70                 r"(?s)<div class='talk-teaser'[^>]*>(.+?)</div>",
71                 webpage, 'description', fatal=False)
72             talk_id = self._search_regex(
73                 [r"id='jc-(\d+)'", r"data-shareable-id='(\d+)'"],
74                 webpage, 'talk id', default=None) or display_id
75             duration = None
76             player = self._search_regex(
77                 r"class='vr-player jp-jplayer'([^>]+)>", webpage, 'player')
78             formats = [{
79                 'url': compat_urlparse.urljoin(url, talk_url),
80                 'format_id': format_id,
81                 'ext': determine_ext(talk_url) or format_id,
82                 'vcodec': 'none',
83             } for format_id, talk_url in re.findall(r"data-([^=]+)='([^']+)'", player)]
84         self._sort_formats(formats)
85
86         thumbnail = self._og_search_thumbnail(webpage)
87         view_count = int_or_none(self._search_regex(
88             r"class='play-count[^']*'>\s*(\d+) plays",
89             webpage, 'play count', fatal=False))
90
91         return {
92             'id': talk_id,
93             'display_id': display_id,
94             'title': title,
95             'description': description,
96             'thumbnail': thumbnail,
97             'duration': duration,
98             'view_count': view_count,
99             'formats': formats,
100         }