[cleanup] Make more playlist entries lazy (#11763)
[yt-dlp.git] / yt_dlp / extractor / wsj.py
blobb6b656f7d3071f58aff713b629b6001bddaeef6a
1 from .common import InfoExtractor
2 from ..utils import (
3 float_or_none,
4 int_or_none,
5 join_nonempty,
6 unified_strdate,
10 class WSJIE(InfoExtractor):
11 _VALID_URL = r'''(?x)
12 (?:
13 https?://video-api\.wsj\.com/api-video/player/iframe\.html\?.*?\bguid=|
14 https?://(?:www\.)?(?:wsj|barrons)\.com/video/(?:[^/]+/)+|
15 wsj:
17 (?P<id>[a-fA-F0-9-]{36})
18 '''
19 IE_DESC = 'Wall Street Journal'
20 _TESTS = [{
21 'url': 'http://video-api.wsj.com/api-video/player/iframe.html?guid=1BD01A4C-BFE8-40A5-A42F-8A8AF9898B1A',
22 'md5': 'e230a5bb249075e40793b655a54a02e4',
23 'info_dict': {
24 'id': '1BD01A4C-BFE8-40A5-A42F-8A8AF9898B1A',
25 'ext': 'mp4',
26 'upload_date': '20150202',
27 'uploader_id': 'jdesai',
28 'creator': 'jdesai',
29 'categories': list, # a long list
30 'duration': 90,
31 'title': 'Bills Coach Rex Ryan Updates His Old Jets Tattoo',
33 }, {
34 'url': 'http://www.wsj.com/video/can-alphabet-build-a-smarter-city/359DDAA8-9AC1-489C-82E6-0429C1E430E0.html',
35 'only_matching': True,
36 }, {
37 'url': 'http://www.barrons.com/video/capitalism-deserves-more-respect-from-millennials/F301217E-6F46-43AE-B8D2-B7180D642EE9.html',
38 'only_matching': True,
39 }, {
40 'url': 'https://www.wsj.com/video/series/a-brief-history-of/the-modern-cell-carrier-how-we-got-here/980E2187-401D-48A1-B82B-1486CEE06CB9',
41 'only_matching': True,
44 def _real_extract(self, url):
45 video_id = self._match_id(url)
47 info = self._download_json(
48 'http://video-api.wsj.com/api-video/find_all_videos.asp', video_id,
49 query={
50 'type': 'guid',
51 'count': 1,
52 'query': video_id,
53 'fields': ','.join((
54 'type', 'hls', 'videoMP4List', 'thumbnailList', 'author',
55 'description', 'name', 'duration', 'videoURL', 'titletag',
56 'formattedCreationDate', 'keywords', 'editor')),
57 })['items'][0]
58 title = info.get('name', info.get('titletag'))
60 formats = []
62 f4m_url = info.get('videoURL')
63 if f4m_url:
64 formats.extend(self._extract_f4m_formats(
65 f4m_url, video_id, f4m_id='hds', fatal=False))
67 m3u8_url = info.get('hls')
68 if m3u8_url:
69 formats.extend(self._extract_m3u8_formats(
70 info['hls'], video_id, ext='mp4',
71 entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))
73 for v in info.get('videoMP4List', []):
74 mp4_url = v.get('url')
75 if not mp4_url:
76 continue
77 tbr = int_or_none(v.get('bitrate'))
78 formats.append({
79 'url': mp4_url,
80 'format_id': join_nonempty('http', tbr),
81 'tbr': tbr,
82 'width': int_or_none(v.get('width')),
83 'height': int_or_none(v.get('height')),
84 'fps': float_or_none(v.get('fps')),
87 return {
88 'id': video_id,
89 'formats': formats,
90 # Thumbnails are conveniently in the correct format already
91 'thumbnails': info.get('thumbnailList'),
92 'creator': info.get('author'),
93 'uploader_id': info.get('editor'),
94 'duration': int_or_none(info.get('duration')),
95 'upload_date': unified_strdate(info.get(
96 'formattedCreationDate'), day_first=False),
97 'title': title,
98 'categories': info.get('keywords'),
102 class WSJArticleIE(InfoExtractor):
103 _VALID_URL = r'(?i)https?://(?:www\.)?wsj\.com/articles/(?P<id>[^/?#&]+)'
104 _TEST = {
105 'url': 'https://www.wsj.com/articles/dont-like-china-no-pandas-for-you-1490366939?',
106 'info_dict': {
107 'id': '4B13FA62-1D8C-45DB-8EA1-4105CB20B362',
108 'ext': 'mp4',
109 'upload_date': '20170221',
110 'uploader_id': 'ralcaraz',
111 'title': 'Bao Bao the Panda Leaves for China',
115 def _real_extract(self, url):
116 article_id = self._match_id(url)
117 webpage = self._download_webpage(url, article_id)
118 video_id = self._search_regex(
119 r'(?:id=["\']video|video-|iframe\.html\?guid=|data-src=["\'])([a-fA-F0-9-]{36})',
120 webpage, 'video id')
121 return self.url_result(f'wsj:{video_id}', WSJIE.ie_key(), video_id)