theweatherchannel.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/series/great-outdoors/video/ice-climber-is-in-for-a-shock',
  12. 'md5': 'c4cbe74c9c17c5676b704b950b73dd92',
  13. 'info_dict': {
  14. 'id': 'cc82397e-cc3f-4d11-9390-a785add090e8',
  15. 'ext': 'mp4',
  16. 'title': 'Ice Climber Is In For A Shock',
  17. 'description': 'md5:55606ce1378d4c72e6545e160c9d9695',
  18. 'uploader': 'TWC - Digital (No Distro)',
  19. 'uploader_id': '6ccd5455-16bb-46f2-9c57-ff858bb9f62c',
  20. 'upload_date': '20160720',
  21. 'timestamp': 1469018835,
  22. }
  23. }, {
  24. 'url': 'https://weather.com/en-CA/international/videos/video/unidentified-object-falls-from-sky-in-india',
  25. 'only_matching': True,
  26. }]
  27. def _real_extract(self, url):
  28. asset_name, locale, display_id = self._match_valid_url(url).groups()
  29. if not locale:
  30. locale = 'en-US'
  31. video_data = list(self._download_json(
  32. 'https://weather.com/api/v1/p/redux-dal', display_id, data=json.dumps([{
  33. 'name': 'getCMSAssetsUrlConfig',
  34. 'params': {
  35. 'language': locale.replace('-', '_'),
  36. 'query': {
  37. 'assetName': {
  38. '$in': asset_name,
  39. },
  40. },
  41. }
  42. }]).encode(), headers={
  43. 'Content-Type': 'application/json',
  44. })['dal']['getCMSAssetsUrlConfig'].values())[0]['data'][0]
  45. video_id = video_data['id']
  46. seo_meta = video_data.get('seometa', {})
  47. title = video_data.get('title') or seo_meta['title']
  48. urls = []
  49. thumbnails = []
  50. formats = []
  51. for variant_id, variant_url in video_data.get('variants', []).items():
  52. variant_url = variant_url.strip()
  53. if not variant_url or variant_url in urls:
  54. continue
  55. urls.append(variant_url)
  56. ext = determine_ext(variant_url)
  57. if ext == 'jpg':
  58. thumbnails.append({
  59. 'url': variant_url,
  60. 'id': variant_id,
  61. })
  62. elif ThePlatformIE.suitable(variant_url):
  63. tp_formats, _ = self._extract_theplatform_smil(variant_url, video_id)
  64. formats.extend(tp_formats)
  65. elif ext == 'm3u8':
  66. formats.extend(self._extract_m3u8_formats(
  67. variant_url, video_id, 'mp4', 'm3u8_native',
  68. m3u8_id=variant_id, fatal=False))
  69. elif ext == 'f4m':
  70. formats.extend(self._extract_f4m_formats(
  71. variant_url, video_id, f4m_id=variant_id, fatal=False))
  72. else:
  73. formats.append({
  74. 'url': variant_url,
  75. 'format_id': variant_id,
  76. })
  77. cc_url = video_data.get('cc_url')
  78. return {
  79. 'id': video_id,
  80. 'display_id': display_id,
  81. 'title': title,
  82. 'description': video_data.get('description') or seo_meta.get('description') or seo_meta.get('og:description'),
  83. 'duration': parse_duration(video_data.get('duration')),
  84. 'uploader': video_data.get('providername'),
  85. 'uploader_id': video_data.get('providerid'),
  86. 'timestamp': parse_iso8601(video_data.get('publishdate')),
  87. 'subtitles': {locale[:2]: [{'url': cc_url}]} if cc_url else None,
  88. 'thumbnails': thumbnails,
  89. 'formats': formats,
  90. }