Merge branch 'pr-twitter' of https://github.com/atomicdryad/youtube-dl into atomicdry...
[youtube-dl] / youtube_dl / extractor / washingtonpost.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     int_or_none,
9     strip_jsonp,
10 )
11
12
13 class WashingtonPostIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?washingtonpost\.com/.*?/(?P<id>[^/]+)/(?:$|[?#])'
15     _TESTS = [{
16         'url': 'http://www.washingtonpost.com/sf/national/2014/03/22/sinkhole-of-bureaucracy/',
17         'info_dict': {
18             'id': 'sinkhole-of-bureaucracy',
19             'title': 'Sinkhole of bureaucracy',
20         },
21         'playlist': [{
22             'md5': 'b9be794ceb56c7267d410a13f99d801a',
23             'info_dict': {
24                 'id': 'fc433c38-b146-11e3-b8b3-44b1d1cd4c1f',
25                 'ext': 'mp4',
26                 'title': 'Breaking Points: The Paper Mine',
27                 'duration': 1290,
28                 'description': 'Overly complicated paper pushing is nothing new to government bureaucracy. But the way federal retirement applications are filed may be the most outdated. David Fahrenthold explains.',
29                 'uploader': 'The Washington Post',
30                 'timestamp': 1395527908,
31                 'upload_date': '20140322',
32             },
33         }, {
34             'md5': '1fff6a689d8770966df78c8cb6c8c17c',
35             'info_dict': {
36                 'id': '41255e28-b14a-11e3-b8b3-44b1d1cd4c1f',
37                 'ext': 'mp4',
38                 'title': 'The town bureaucracy sustains',
39                 'description': 'Underneath the friendly town of Boyers is a sea of government paperwork. In a disused limestone mine, hundreds of locals now track, file and process retirement applications for the federal government. We set out to find out what it\'s like to do paperwork 230 feet underground.',
40                 'duration': 2220,
41                 'timestamp': 1395528005,
42                 'upload_date': '20140322',
43                 'uploader': 'The Washington Post',
44             },
45         }],
46     }, {
47         'url': 'http://www.washingtonpost.com/blogs/wonkblog/wp/2014/12/31/one-airline-figured-out-how-to-make-sure-its-airplanes-never-disappear/',
48         'info_dict': {
49             'id': 'one-airline-figured-out-how-to-make-sure-its-airplanes-never-disappear',
50             'title': 'One airline figured out how to make sure its airplanes never disappear',
51         },
52         'playlist': [{
53             'md5': 'a7c1b5634ba5e57a6a82cdffa5b1e0d0',
54             'info_dict': {
55                 'id': '0e4bb54c-9065-11e4-a66f-0ca5037a597d',
56                 'ext': 'mp4',
57                 'description': 'Washington Post transportation reporter Ashley Halsey III explains why a plane\'s black box needs to be recovered from a crash site instead of having its information streamed in real time throughout the flight.',
58                 'upload_date': '20141230',
59                 'uploader': 'The Washington Post',
60                 'timestamp': 1419974765,
61                 'title': 'Why black boxes don’t transmit data in real time',
62             }
63         }]
64     }]
65
66     def _real_extract(self, url):
67         page_id = self._match_id(url)
68         webpage = self._download_webpage(url, page_id)
69
70         title = self._og_search_title(webpage)
71
72         uuids = re.findall(r'''(?x)
73             (?:
74                 <div\s+class="posttv-video-embed[^>]*?data-uuid=|
75                 data-video-uuid=
76             )"([^"]+)"''', webpage)
77         entries = []
78         for i, uuid in enumerate(uuids, start=1):
79             vinfo_all = self._download_json(
80                 'http://www.washingtonpost.com/posttv/c/videojson/%s?resType=jsonp' % uuid,
81                 page_id,
82                 transform_source=strip_jsonp,
83                 note='Downloading information of video %d/%d' % (i, len(uuids))
84             )
85             vinfo = vinfo_all[0]['contentConfig']
86             uploader = vinfo.get('credits', {}).get('source')
87             timestamp = int_or_none(
88                 vinfo.get('dateConfig', {}).get('dateFirstPublished'), 1000)
89
90             formats = [{
91                 'format_id': (
92                     '%s-%s-%s' % (s.get('type'), s.get('width'), s.get('bitrate'))
93                     if s.get('width')
94                     else s.get('type')),
95                 'vbr': s.get('bitrate') if s.get('width') != 0 else None,
96                 'width': s.get('width'),
97                 'height': s.get('height'),
98                 'acodec': s.get('audioCodec'),
99                 'vcodec': s.get('videoCodec') if s.get('width') != 0 else 'none',
100                 'filesize': s.get('fileSize'),
101                 'url': s.get('url'),
102                 'ext': 'mp4',
103                 'preference': -100 if s.get('type') == 'smil' else None,
104                 'protocol': {
105                     'MP4': 'http',
106                     'F4F': 'f4m',
107                 }.get(s.get('type')),
108             } for s in vinfo.get('streams', [])]
109             source_media_url = vinfo.get('sourceMediaURL')
110             if source_media_url:
111                 formats.append({
112                     'format_id': 'source_media',
113                     'url': source_media_url,
114                 })
115             self._sort_formats(formats)
116             entries.append({
117                 'id': uuid,
118                 'title': vinfo['title'],
119                 'description': vinfo.get('blurb'),
120                 'uploader': uploader,
121                 'formats': formats,
122                 'duration': int_or_none(vinfo.get('videoDuration'), 100),
123                 'timestamp': timestamp,
124             })
125
126         return {
127             '_type': 'playlist',
128             'entries': entries,
129             'id': page_id,
130             'title': title,
131         }