]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/theweatherchannel.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / extractor / theweatherchannel.py
CommitLineData
29f7c58a 1import json
29f7c58a 2
215ff6e0
RA
3from .theplatform import ThePlatformIE
4from ..utils import (
5 determine_ext,
6 parse_duration,
29f7c58a 7 parse_iso8601,
215ff6e0
RA
8)
9
10
6368e2e6 11class TheWeatherChannelIE(ThePlatformIE): # XXX: Do not subclass from concrete IE
29f7c58a 12 _VALID_URL = r'https?://(?:www\.)?weather\.com(?P<asset_name>(?:/(?P<locale>[a-z]{2}-[A-Z]{2}))?/(?:[^/]+/)*video/(?P<id>[^/?#]+))'
215ff6e0 13 _TESTS = [{
19c90e40 14 'url': 'https://weather.com/storms/hurricane/video/invest-95l-in-atlantic-has-a-medium-chance-of-development',
15 'md5': '68f0cf616435683f27ce36bd9c927394',
215ff6e0 16 'info_dict': {
19c90e40 17 'id': '81acef2d-ee8c-4545-ba83-bff3cc80db97',
215ff6e0 18 'ext': 'mp4',
19c90e40 19 'title': 'Invest 95L In Atlantic Has A Medium Chance Of Development',
20 'description': 'md5:0de720fd5f0d0e32207bd4c270fff824',
21 'uploader': 'TWC - Digital',
22 'uploader_id': 'b5a999e0-9e04-11e1-9ee2-001d092f5a10',
23 'upload_date': '20230721',
24 'timestamp': 1689967343,
25 'display_id': 'invest-95l-in-atlantic-has-a-medium-chance-of-development',
26 'duration': 34.0,
add96eb9 27 },
29f7c58a 28 }, {
29 'url': 'https://weather.com/en-CA/international/videos/video/unidentified-object-falls-from-sky-in-india',
30 'only_matching': True,
215ff6e0
RA
31 }]
32
33 def _real_extract(self, url):
5ad28e7f 34 asset_name, locale, display_id = self._match_valid_url(url).groups()
29f7c58a 35 if not locale:
36 locale = 'en-US'
add96eb9 37 video_data = next(iter(self._download_json(
29f7c58a 38 'https://weather.com/api/v1/p/redux-dal', display_id, data=json.dumps([{
39 'name': 'getCMSAssetsUrlConfig',
40 'params': {
41 'language': locale.replace('-', '_'),
42 'query': {
43 'assetName': {
44 '$in': asset_name,
45 },
46 },
add96eb9 47 },
29f7c58a 48 }]).encode(), headers={
49 'Content-Type': 'application/json',
add96eb9 50 })['dal']['getCMSAssetsUrlConfig'].values()))['data'][0]
29f7c58a 51 video_id = video_data['id']
215ff6e0
RA
52 seo_meta = video_data.get('seometa', {})
53 title = video_data.get('title') or seo_meta['title']
54
55 urls = []
56 thumbnails = []
57 formats = []
58 for variant_id, variant_url in video_data.get('variants', []).items():
59 variant_url = variant_url.strip()
60 if not variant_url or variant_url in urls:
61 continue
62 urls.append(variant_url)
63 ext = determine_ext(variant_url)
64 if ext == 'jpg':
65 thumbnails.append({
66 'url': variant_url,
67 'id': variant_id,
68 })
69 elif ThePlatformIE.suitable(variant_url):
70 tp_formats, _ = self._extract_theplatform_smil(variant_url, video_id)
71 formats.extend(tp_formats)
72 elif ext == 'm3u8':
73 formats.extend(self._extract_m3u8_formats(
74 variant_url, video_id, 'mp4', 'm3u8_native',
75 m3u8_id=variant_id, fatal=False))
76 elif ext == 'f4m':
77 formats.extend(self._extract_f4m_formats(
78 variant_url, video_id, f4m_id=variant_id, fatal=False))
79 else:
80 formats.append({
81 'url': variant_url,
82 'format_id': variant_id,
83 })
215ff6e0 84
29f7c58a 85 cc_url = video_data.get('cc_url')
86
215ff6e0
RA
87 return {
88 'id': video_id,
89 'display_id': display_id,
90 'title': title,
91 'description': video_data.get('description') or seo_meta.get('description') or seo_meta.get('og:description'),
92 'duration': parse_duration(video_data.get('duration')),
93 'uploader': video_data.get('providername'),
94 'uploader_id': video_data.get('providerid'),
29f7c58a 95 'timestamp': parse_iso8601(video_data.get('publishdate')),
96 'subtitles': {locale[:2]: [{'url': cc_url}]} if cc_url else None,
215ff6e0
RA
97 'thumbnails': thumbnails,
98 'formats': formats,
99 }