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