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