[washingtonpost] remove unnecessary code
[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                 })
78         source_media_url = video_data.get('sourceMediaURL')
79         if source_media_url:
80             formats.append({
81                 'format_id': 'source_media',
82                 'url': source_media_url,
83             })
84         self._sort_formats(
85             formats, ('width', 'height', 'vbr', 'filesize', 'tbr', 'format_id'))
86
87         return {
88             'id': video_id,
89             'title': title,
90             'description': video_data.get('blurb'),
91             'uploader': video_data.get('credits', {}).get('source'),
92             'formats': formats,
93             'duration': int_or_none(video_data.get('videoDuration'), 100),
94             'timestamp': int_or_none(
95                 video_data.get('dateConfig', {}).get('dateFirstPublished'), 1000),
96         }
97
98
99 class WashingtonPostArticleIE(InfoExtractor):
100     IE_NAME = 'washingtonpost:article'
101     _VALID_URL = r'https?://(?:www\.)?washingtonpost\.com/(?:[^/]+/)*(?P<id>[^/?#]+)'
102     _TESTS = [{
103         'url': 'http://www.washingtonpost.com/sf/national/2014/03/22/sinkhole-of-bureaucracy/',
104         'info_dict': {
105             'id': 'sinkhole-of-bureaucracy',
106             'title': 'Sinkhole of bureaucracy',
107         },
108         'playlist': [{
109             'md5': 'b9be794ceb56c7267d410a13f99d801a',
110             'info_dict': {
111                 'id': 'fc433c38-b146-11e3-b8b3-44b1d1cd4c1f',
112                 'ext': 'mp4',
113                 'title': 'Breaking Points: The Paper Mine',
114                 'duration': 1290,
115                 '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.',
116                 'uploader': 'The Washington Post',
117                 'timestamp': 1395527908,
118                 'upload_date': '20140322',
119             },
120         }, {
121             'md5': '1fff6a689d8770966df78c8cb6c8c17c',
122             'info_dict': {
123                 'id': '41255e28-b14a-11e3-b8b3-44b1d1cd4c1f',
124                 'ext': 'mp4',
125                 'title': 'The town bureaucracy sustains',
126                 '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.',
127                 'duration': 2220,
128                 'timestamp': 1395528005,
129                 'upload_date': '20140322',
130                 'uploader': 'The Washington Post',
131             },
132         }],
133     }, {
134         'url': 'http://www.washingtonpost.com/blogs/wonkblog/wp/2014/12/31/one-airline-figured-out-how-to-make-sure-its-airplanes-never-disappear/',
135         'info_dict': {
136             'id': 'one-airline-figured-out-how-to-make-sure-its-airplanes-never-disappear',
137             'title': 'One airline figured out how to make sure its airplanes never disappear',
138         },
139         'playlist': [{
140             'md5': 'a7c1b5634ba5e57a6a82cdffa5b1e0d0',
141             'info_dict': {
142                 'id': '0e4bb54c-9065-11e4-a66f-0ca5037a597d',
143                 'ext': 'mp4',
144                 '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.',
145                 'upload_date': '20141230',
146                 'uploader': 'The Washington Post',
147                 'timestamp': 1419974765,
148                 'title': 'Why black boxes don’t transmit data in real time',
149             }
150         }]
151     }]
152
153     @classmethod
154     def suitable(cls, url):
155         return False if WashingtonPostIE.suitable(url) else super(WashingtonPostArticleIE, cls).suitable(url)
156
157     def _real_extract(self, url):
158         page_id = self._match_id(url)
159         webpage = self._download_webpage(url, page_id)
160
161         title = self._og_search_title(webpage)
162
163         uuids = re.findall(r'''(?x)
164             (?:
165                 <div\s+class="posttv-video-embed[^>]*?data-uuid=|
166                 data-video-uuid=
167             )"([^"]+)"''', webpage)
168         entries = [self.url_result('washingtonpost:%s' % uuid, 'WashingtonPost', uuid) for uuid in uuids]
169
170         return {
171             '_type': 'playlist',
172             'entries': entries,
173             'id': page_id,
174             'title': title,
175         }