canalalpha.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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:85d594a3b5dc6ccfc4a85aba6e73b129',
  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. 'url': 'https://www.canalalpha.ch/play/le-journal/topic/33500/encore-des-mesures-deconomie-dans-le-jura',
  60. 'info_dict': {
  61. 'id': '33500',
  62. 'ext': 'mp4',
  63. 'title': 'Encore des mesures d\'économie dans le Jura',
  64. 'description': 'md5:938b5b556592f2d1b9ab150268082a80',
  65. 'thumbnail': 'https://static.canalalpha.ch/poster/news/news_46665.jpg',
  66. 'upload_date': '20240411',
  67. 'duration': 105,
  68. },
  69. }]
  70. def _real_extract(self, url):
  71. video_id = self._match_id(url)
  72. webpage = self._download_webpage(url, video_id)
  73. data_json = self._parse_json(self._search_regex(
  74. r'window\.__SERVER_STATE__\s?=\s?({(?:(?!};)[^"]|"([^"]|\\")*")+})\s?;',
  75. webpage, 'data_json'), video_id)['1']['data']['data']
  76. manifests = try_get(data_json, lambda x: x['video']['manifests'], expected_type=dict) or {}
  77. subtitles = {}
  78. formats = [{
  79. 'url': video['$url'],
  80. 'ext': 'mp4',
  81. 'width': try_get(video, lambda x: x['res']['width'], expected_type=int),
  82. 'height': try_get(video, lambda x: x['res']['height'], expected_type=int),
  83. } for video in try_get(data_json, lambda x: x['video']['mp4'], expected_type=list) or [] if video.get('$url')]
  84. if manifests.get('hls'):
  85. fmts, subs = self._extract_m3u8_formats_and_subtitles(
  86. manifests['hls'], video_id, m3u8_id='hls', fatal=False)
  87. formats.extend(fmts)
  88. self._merge_subtitles(subs, target=subtitles)
  89. if manifests.get('dash'):
  90. fmts, subs = self._extract_mpd_formats_and_subtitles(
  91. manifests['dash'], video_id, mpd_id='dash', fatal=False)
  92. formats.extend(fmts)
  93. self._merge_subtitles(subs, target=subtitles)
  94. return {
  95. 'id': video_id,
  96. 'title': data_json.get('title').strip(),
  97. 'description': clean_html(dict_get(data_json, ('longDesc', 'shortDesc'))),
  98. 'thumbnail': data_json.get('poster'),
  99. 'upload_date': unified_strdate(dict_get(data_json, ('webPublishAt', 'featuredAt', 'diffusionDate'))),
  100. 'duration': try_get(data_json, lambda x: x['video']['duration'], expected_type=int),
  101. 'formats': formats,
  102. 'subtitles': subtitles,
  103. }