theweatherchannel.py 4.0 KB

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