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