]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/cpac.py
[spotify] Detect iframe embeds (#3430)
[yt-dlp.git] / yt_dlp / extractor / cpac.py
1 from .common import InfoExtractor
2 from ..compat import compat_str
3 from ..utils import (
4 int_or_none,
5 str_or_none,
6 try_get,
7 unified_timestamp,
8 update_url_query,
9 urljoin,
10 )
11
12 # compat_range
13 try:
14 if callable(xrange):
15 range = xrange
16 except (NameError, TypeError):
17 pass
18
19
20 class CPACIE(InfoExtractor):
21 IE_NAME = 'cpac'
22 _VALID_URL = r'https?://(?:www\.)?cpac\.ca/(?P<fr>l-)?episode\?id=(?P<id>[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12})'
23 _TEST = {
24 # 'url': 'http://www.cpac.ca/en/programs/primetime-politics/episodes/65490909',
25 'url': 'https://www.cpac.ca/episode?id=fc7edcae-4660-47e1-ba61-5b7f29a9db0f',
26 'md5': 'e46ad699caafd7aa6024279f2614e8fa',
27 'info_dict': {
28 'id': 'fc7edcae-4660-47e1-ba61-5b7f29a9db0f',
29 'ext': 'mp4',
30 'upload_date': '20220215',
31 'title': 'News Conference to Celebrate National Kindness Week – February 15, 2022',
32 'description': 'md5:466a206abd21f3a6f776cdef290c23fb',
33 'timestamp': 1644901200,
34 },
35 'params': {
36 'format': 'bestvideo',
37 'hls_prefer_native': True,
38 },
39 }
40
41 def _real_extract(self, url):
42 video_id = self._match_id(url)
43 url_lang = 'fr' if '/l-episode?' in url else 'en'
44
45 content = self._download_json(
46 'https://www.cpac.ca/api/1/services/contentModel.json?url=/site/website/episode/index.xml&crafterSite=cpacca&id=' + video_id,
47 video_id)
48 video_url = try_get(content, lambda x: x['page']['details']['videoUrl'], compat_str)
49 formats = []
50 if video_url:
51 content = content['page']
52 title = str_or_none(content['details']['title_%s_t' % (url_lang, )])
53 formats = self._extract_m3u8_formats(video_url, video_id, m3u8_id='hls', ext='mp4')
54 for fmt in formats:
55 # prefer language to match URL
56 fmt_lang = fmt.get('language')
57 if fmt_lang == url_lang:
58 fmt['language_preference'] = 10
59 elif not fmt_lang:
60 fmt['language_preference'] = -1
61 else:
62 fmt['language_preference'] = -10
63
64 self._sort_formats(formats)
65
66 category = str_or_none(content['details']['category_%s_t' % (url_lang, )])
67
68 def is_live(v_type):
69 return (v_type == 'live') if v_type is not None else None
70
71 return {
72 'id': video_id,
73 'formats': formats,
74 'title': title,
75 'description': str_or_none(content['details'].get('description_%s_t' % (url_lang, ))),
76 'timestamp': unified_timestamp(content['details'].get('liveDateTime')),
77 'category': [category] if category else None,
78 'thumbnail': urljoin(url, str_or_none(content['details'].get('image_%s_s' % (url_lang, )))),
79 'is_live': is_live(content['details'].get('type')),
80 }
81
82
83 class CPACPlaylistIE(InfoExtractor):
84 IE_NAME = 'cpac:playlist'
85 _VALID_URL = r'(?i)https?://(?:www\.)?cpac\.ca/(?:program|search|(?P<fr>emission|rechercher))\?(?:[^&]+&)*?(?P<id>(?:id=\d+|programId=\d+|key=[^&]+))'
86
87 _TESTS = [{
88 'url': 'https://www.cpac.ca/program?id=6',
89 'info_dict': {
90 'id': 'id=6',
91 'title': 'Headline Politics',
92 'description': 'Watch CPAC’s signature long-form coverage of the day’s pressing political events as they unfold.',
93 },
94 'playlist_count': 10,
95 }, {
96 'url': 'https://www.cpac.ca/search?key=hudson&type=all&order=desc',
97 'info_dict': {
98 'id': 'key=hudson',
99 'title': 'hudson',
100 },
101 'playlist_count': 22,
102 }, {
103 'url': 'https://www.cpac.ca/search?programId=50',
104 'info_dict': {
105 'id': 'programId=50',
106 'title': '50',
107 },
108 'playlist_count': 9,
109 }, {
110 'url': 'https://www.cpac.ca/emission?id=6',
111 'only_matching': True,
112 }, {
113 'url': 'https://www.cpac.ca/rechercher?key=hudson&type=all&order=desc',
114 'only_matching': True,
115 }]
116
117 def _real_extract(self, url):
118 video_id = self._match_id(url)
119 url_lang = 'fr' if any(x in url for x in ('/emission?', '/rechercher?')) else 'en'
120 pl_type, list_type = ('program', 'itemList') if any(x in url for x in ('/program?', '/emission?')) else ('search', 'searchResult')
121 api_url = (
122 'https://www.cpac.ca/api/1/services/contentModel.json?url=/site/website/%s/index.xml&crafterSite=cpacca&%s'
123 % (pl_type, video_id, ))
124 content = self._download_json(api_url, video_id)
125 entries = []
126 total_pages = int_or_none(try_get(content, lambda x: x['page'][list_type]['totalPages']), default=1)
127 for page in range(1, total_pages + 1):
128 if page > 1:
129 api_url = update_url_query(api_url, {'page': '%d' % (page, ), })
130 content = self._download_json(
131 api_url, video_id,
132 note='Downloading continuation - %d' % (page, ),
133 fatal=False)
134
135 for item in try_get(content, lambda x: x['page'][list_type]['item'], list) or []:
136 episode_url = urljoin(url, try_get(item, lambda x: x['url_%s_s' % (url_lang, )]))
137 if episode_url:
138 entries.append(episode_url)
139
140 return self.playlist_result(
141 (self.url_result(entry) for entry in entries),
142 playlist_id=video_id,
143 playlist_title=try_get(content, lambda x: x['page']['program']['title_%s_t' % (url_lang, )]) or video_id.split('=')[-1],
144 playlist_description=try_get(content, lambda x: x['page']['program']['description_%s_t' % (url_lang, )]),
145 )