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