aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/democracynow.py
blob: 05cfc7502d194d10a09f3b497cb97dcc25a452c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# coding: utf-8
from __future__ import unicode_literals

import re
from .common import InfoExtractor


class DemocracynowIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?democracynow.org/?(?P<id>[^\?]*)'
    IE_NAME = 'democracynow'
    _TESTS = [{
        'url': 'http://www.democracynow.org/shows/2015/7/3',
        'info_dict': {
            'id': '2015-0703-001',
            'ext': 'mp4',
            'title': 'July 03, 2015 - Democracy Now!',
            'description': 'A daily independent global news hour with Amy Goodman & Juan Gonz\xe1lez "What to the Slave is 4th of July?": James Earl Jones Reads Frederick Douglass\u2019 Historic Speech : "This Flag Comes Down Today": Bree Newsome Scales SC Capitol Flagpole, Takes Down Confederate Flag : "We Shall Overcome": Remembering Folk Icon, Activist Pete Seeger in His Own Words & Songs',
            'uploader': 'Democracy Now',
            'upload_date': None,
        },
    }, {
        'url': 'http://www.democracynow.org/2015/7/3/this_flag_comes_down_today_bree',
        'info_dict': {
            'id': '2015-0703-001',
            'ext': 'mp4',
            'title': '"This Flag Comes Down Today": Bree Newsome Scales SC Capitol Flagpole, Takes Down Confederate Flag',
            'description': 'md5:4d2bc4f0d29f5553c2210a4bc7761a21',
            'uploader': 'Democracy Now',
            'upload_date': None,
        },
    }]

    def _real_extract(self, url):
        display_id = self._match_id(url)
        base_host = re.search(r'^(.+?://[^/]+)', url).group(1)
        if display_id == '':
            display_id = 'home'
        webpage = self._download_webpage(url, display_id)
        description = self._og_search_description(webpage)

        jstr = self._search_regex(r'<script[^>]+type="text/json"[^>]*>\s*({[^>]+})', webpage, 'json')
        js = self._parse_json(jstr, display_id)
        video_id = None
        formats = []
        subtitles = {}
        for key in ('caption_file', '.......'):
            # ....... = pending vtt support that doesn't clobber srt 'chapter_file':
            url = js.get(key, '')
            if url == '' or url is None:
                continue
            if not re.match(r'^https?://', url):
                url = base_host + url
            ext = re.search(r'\.([^\.]+)$', url).group(1)
            subtitles['eng'] = [{
                'ext': ext,
                'url': url,
            }]
        for key in ('file', 'audio', 'video'):
            url = js.get(key, '')
            if url == '' or url is None:
                continue
            if not re.match(r'^https?://', url):
                url = base_host + url
            purl = re.search(r'/(?P<dir>[^/]+)/(?:dn)?(?P<fn>[^/]+?)\.(?P<ext>[^\.\?]+)(?P<hasparams>\?|$)', url)
            if video_id is None:
                video_id = purl.group('fn')
            if js.get('start') is not None:
                url += '&' if purl.group('hasparams') == '?' else '?'
                url = url + 'start=' + str(js.get('start'))
            formats.append({
                'format_id': purl.group('dir'),
                'ext': purl.group('ext'),
                'url': url,
            })
        self._sort_formats(formats)
        ret = {
            'id': video_id,
            'title': js.get('title'),
            'description': description,
            'uploader': 'Democracy Now',
            'subtitles': subtitles,
            'formats': formats,
        }
        return ret