[washingtonpost] improve format extraction and add support for video pages extraction
[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     IE_NAME = 'washingtonpost'
15     _VALID_URL = r'(?:washingtonpost:|https?://(?:www\.)?washingtonpost\.com/video/(?:[^/]+/)*)(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'
16     _TEST = {
17         'url': 'https://www.washingtonpost.com/video/c/video/480ba4ee-1ec7-11e6-82c2-a7dcb313287d',
18         'md5': '6f537e1334b714eb15f9563bd4b9cdfa',
19         'info_dict': {
20             'id': '480ba4ee-1ec7-11e6-82c2-a7dcb313287d',
21             'ext': 'mp4',
22             'title': 'Egypt finds belongings, debris from plane crash',
23             'description': 'md5:a17ceee432f215a5371388c1f680bd86',
24             'upload_date': '20160520',
25             'uploader': 'Reuters',
26             'timestamp': 1463778452,
27         },
28     }
29
30     def _real_extract(self, url):
31         video_id = self._match_id(url)
32         video_data = self._download_json(
33             'http://www.washingtonpost.com/posttv/c/videojson/%s?resType=jsonp' % video_id,
34             video_id, transform_source=strip_jsonp)[0]['contentConfig']
35         title = video_data['title']
36
37         urls = []
38         formats = []
39         for s in video_data.get('streams', []):
40             s_url = s.get('url')
41             if not s_url or s_url in urls:
42                 continue
43             urls.append(s_url)
44             video_type = s.get('type')
45             if video_type == 'smil':
46                 continue
47             elif video_type in ('ts', 'hls'):
48                 m3u8_formats = self._extract_m3u8_formats(
49                     s_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
50                 for m3u8_format in m3u8_formats:
51                     width = m3u8_format.get('width')
52                     if not width:
53                         continue
54                     vbr = self._search_regex(
55                         r'%d_%d_(\d+)' % (width, m3u8_format['height']), m3u8_format['url'], 'vbr', default=None)
56                     if vbr:
57                         m3u8_format.update({
58                             'vbr': int_or_none(vbr),
59                         })
60                 formats.extend(m3u8_formats)
61             else:
62                 width = int_or_none(s.get('width'))
63                 vbr = int_or_none(s.get('bitrate'))
64                 has_width = width != 0
65                 formats.append({
66                     'format_id': (
67                         '%s-%d-%d' % (video_type, width, vbr)
68                         if width
69                         else video_type),
70                     'vbr': vbr if has_width else None,
71                     'width': width,
72                     'height': int_or_none(s.get('height')),
73                     'acodec': s.get('audioCodec'),
74                     'vcodec': s.get('videoCodec') if has_width else 'none',
75                     'filesize': int_or_none(s.get('fileSize')),
76                     'url': s_url,
77                     'ext': 'mp4',
78                     'protocol': {
79                         'mp4': 'http',
80                         'ts': 'm3u8_native',
81                         'hls': 'm3u8_native',
82                     }.get(s.get('type')),
83                 })
84         source_media_url = video_data.get('sourceMediaURL')
85         if source_media_url:
86             formats.append({
87                 'format_id': 'source_media',
88                 'url': source_media_url,
89             })
90         self._sort_formats(
91             formats, ('width', 'height', 'vbr', 'filesize', 'tbr', 'format_id'))
92
93         return {
94             'id': video_id,
95             'title': title,
96             'description': video_data.get('blurb'),
97             'uploader': video_data.get('credits', {}).get('source'),
98             'formats': formats,
99             'duration': int_or_none(video_data.get('videoDuration'), 100),
100             'timestamp': int_or_none(
101                 video_data.get('dateConfig', {}).get('dateFirstPublished'), 1000),
102         }
103
104
105 class WashingtonPostArticleIE(InfoExtractor):
106     IE_NAME = 'washingtonpost:article'
107     _VALID_URL = r'https?://(?:www\.)?washingtonpost\.com/(?:[^/]+/)*(?P<id>[^/?#]+)'
108     _TESTS = [{
109         'url': 'http://www.washingtonpost.com/sf/national/2014/03/22/sinkhole-of-bureaucracy/',
110         'info_dict': {
111             'id': 'sinkhole-of-bureaucracy',
112             'title': 'Sinkhole of bureaucracy',
113         },
114         'playlist': [{
115             'md5': 'b9be794ceb56c7267d410a13f99d801a',
116             'info_dict': {
117                 'id': 'fc433c38-b146-11e3-b8b3-44b1d1cd4c1f',
118                 'ext': 'mp4',
119                 'title': 'Breaking Points: The Paper Mine',
120                 'duration': 1290,
121                 '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.',
122                 'uploader': 'The Washington Post',
123                 'timestamp': 1395527908,
124                 'upload_date': '20140322',
125             },
126         }, {
127             'md5': '1fff6a689d8770966df78c8cb6c8c17c',
128             'info_dict': {
129                 'id': '41255e28-b14a-11e3-b8b3-44b1d1cd4c1f',
130                 'ext': 'mp4',
131                 'title': 'The town bureaucracy sustains',
132                 '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.',
133                 'duration': 2220,
134                 'timestamp': 1395528005,
135                 'upload_date': '20140322',
136                 'uploader': 'The Washington Post',
137             },
138         }],
139     }, {
140         'url': 'http://www.washingtonpost.com/blogs/wonkblog/wp/2014/12/31/one-airline-figured-out-how-to-make-sure-its-airplanes-never-disappear/',
141         'info_dict': {
142             'id': 'one-airline-figured-out-how-to-make-sure-its-airplanes-never-disappear',
143             'title': 'One airline figured out how to make sure its airplanes never disappear',
144         },
145         'playlist': [{
146             'md5': 'a7c1b5634ba5e57a6a82cdffa5b1e0d0',
147             'info_dict': {
148                 'id': '0e4bb54c-9065-11e4-a66f-0ca5037a597d',
149                 'ext': 'mp4',
150                 '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.',
151                 'upload_date': '20141230',
152                 'uploader': 'The Washington Post',
153                 'timestamp': 1419974765,
154                 'title': 'Why black boxes don’t transmit data in real time',
155             }
156         }]
157     }]
158
159     @classmethod
160     def suitable(cls, url):
161         return False if WashingtonPostIE.suitable(url) else super(WashingtonPostArticleIE, cls).suitable(url)
162
163     def _real_extract(self, url):
164         page_id = self._match_id(url)
165         webpage = self._download_webpage(url, page_id)
166
167         title = self._og_search_title(webpage)
168
169         uuids = re.findall(r'''(?x)
170             (?:
171                 <div\s+class="posttv-video-embed[^>]*?data-uuid=|
172                 data-video-uuid=
173             )"([^"]+)"''', webpage)
174         entries = [self.url_result('washingtonpost:%s' % uuid, 'WashingtonPost', uuid) for uuid in uuids]
175
176         return {
177             '_type': 'playlist',
178             'entries': entries,
179             'id': page_id,
180             'title': title,
181         }