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