canalalpha.py 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. clean_html,
  4. dict_get,
  5. try_get,
  6. unified_strdate,
  7. )
  8. class CanalAlphaIE(InfoExtractor):
  9. _VALID_URL = r'https?://(?:www\.)?canalalpha\.ch/play/[^/]+/[^/]+/(?P<id>\d+)/?.*'
  10. _TESTS = [{
  11. 'url': 'https://www.canalalpha.ch/play/le-journal/episode/24520/jeudi-28-octobre-2021',
  12. 'info_dict': {
  13. 'id': '24520',
  14. 'ext': 'mp4',
  15. 'title': 'Jeudi 28 octobre 2021',
  16. 'description': 'md5:d30c6c3e53f8ad40d405379601973b30',
  17. 'thumbnail': 'https://static.canalalpha.ch/poster/journal/journal_20211028.jpg',
  18. 'upload_date': '20211028',
  19. 'duration': 1125,
  20. },
  21. 'params': {'skip_download': True}
  22. }, {
  23. 'url': 'https://www.canalalpha.ch/play/le-journal/topic/24512/la-poste-fait-de-neuchatel-un-pole-cryptographique',
  24. 'info_dict': {
  25. 'id': '24512',
  26. 'ext': 'mp4',
  27. 'title': 'La Poste fait de Neuchâtel un pôle cryptographique',
  28. 'description': 'md5:4ba63ae78a0974d1a53d6703b6e1dedf',
  29. 'thumbnail': 'https://static.canalalpha.ch/poster/news/news_39712.jpg',
  30. 'upload_date': '20211028',
  31. 'duration': 138,
  32. },
  33. 'params': {'skip_download': True}
  34. }, {
  35. 'url': 'https://www.canalalpha.ch/play/eureka/episode/24484/ces-innovations-qui-veulent-rendre-lagriculture-plus-durable',
  36. 'info_dict': {
  37. 'id': '24484',
  38. 'ext': 'mp4',
  39. 'title': 'Ces innovations qui veulent rendre l’agriculture plus durable',
  40. 'description': 'md5:3de3f151180684621e85be7c10e4e613',
  41. 'thumbnail': 'https://static.canalalpha.ch/poster/magazine/magazine_10236.jpg',
  42. 'upload_date': '20211026',
  43. 'duration': 360,
  44. },
  45. 'params': {'skip_download': True}
  46. }, {
  47. 'url': 'https://www.canalalpha.ch/play/avec-le-temps/episode/23516/redonner-de-leclat-grace-au-polissage',
  48. 'info_dict': {
  49. 'id': '23516',
  50. 'ext': 'mp4',
  51. 'title': 'Redonner de l\'éclat grâce au polissage',
  52. 'description': 'md5:0d8fbcda1a5a4d6f6daa3165402177e1',
  53. 'thumbnail': 'https://static.canalalpha.ch/poster/magazine/magazine_9990.png',
  54. 'upload_date': '20210726',
  55. 'duration': 360,
  56. },
  57. 'params': {'skip_download': True}
  58. }]
  59. def _real_extract(self, url):
  60. id = self._match_id(url)
  61. webpage = self._download_webpage(url, id)
  62. data_json = self._parse_json(self._search_regex(
  63. r'window\.__SERVER_STATE__\s?=\s?({(?:(?!};)[^"]|"([^"]|\\")*")+})\s?;',
  64. webpage, 'data_json'), id)['1']['data']['data']
  65. manifests = try_get(data_json, lambda x: x['video']['manifests'], expected_type=dict) or {}
  66. subtitles = {}
  67. formats = [{
  68. 'url': video['$url'],
  69. 'ext': 'mp4',
  70. 'width': try_get(video, lambda x: x['res']['width'], expected_type=int),
  71. 'height': try_get(video, lambda x: x['res']['height'], expected_type=int),
  72. } for video in try_get(data_json, lambda x: x['video']['mp4'], expected_type=list) or [] if video.get('$url')]
  73. if manifests.get('hls'):
  74. m3u8_frmts, m3u8_subs = self._parse_m3u8_formats_and_subtitles(manifests['hls'], video_id=id)
  75. formats.extend(m3u8_frmts)
  76. subtitles = self._merge_subtitles(subtitles, m3u8_subs)
  77. if manifests.get('dash'):
  78. dash_frmts, dash_subs = self._parse_mpd_formats_and_subtitles(manifests['dash'])
  79. formats.extend(dash_frmts)
  80. subtitles = self._merge_subtitles(subtitles, dash_subs)
  81. return {
  82. 'id': id,
  83. 'title': data_json.get('title').strip(),
  84. 'description': clean_html(dict_get(data_json, ('longDesc', 'shortDesc'))),
  85. 'thumbnail': data_json.get('poster'),
  86. 'upload_date': unified_strdate(dict_get(data_json, ('webPublishAt', 'featuredAt', 'diffusionDate'))),
  87. 'duration': try_get(data_json, lambda x: x['video']['duration'], expected_type=int),
  88. 'formats': formats,
  89. 'subtitles': subtitles,
  90. }