]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/ant1newsgr.py
[extractors] Use new framework for existing embeds (#4307)
[yt-dlp.git] / yt_dlp / extractor / ant1newsgr.py
1 import urllib.parse
2
3 from .common import InfoExtractor
4 from ..utils import (
5 HEADRequest,
6 ExtractorError,
7 determine_ext,
8 scale_thumbnails_to_max_format_width,
9 )
10
11
12 class Ant1NewsGrBaseIE(InfoExtractor):
13 def _download_and_extract_api_data(self, video_id, netloc, cid=None):
14 url = f'{self.http_scheme()}//{netloc}{self._API_PATH}'
15 info = self._download_json(url, video_id, query={'cid': cid or video_id})
16 try:
17 source = info['url']
18 except KeyError:
19 raise ExtractorError('no source found for %s' % video_id)
20 formats, subs = (self._extract_m3u8_formats_and_subtitles(source, video_id, 'mp4')
21 if determine_ext(source) == 'm3u8' else ([{'url': source}], {}))
22 self._sort_formats(formats)
23 thumbnails = scale_thumbnails_to_max_format_width(
24 formats, [{'url': info['thumb']}], r'(?<=/imgHandler/)\d+')
25 return {
26 'id': video_id,
27 'title': info.get('title'),
28 'thumbnails': thumbnails,
29 'formats': formats,
30 'subtitles': subs,
31 }
32
33
34 class Ant1NewsGrWatchIE(Ant1NewsGrBaseIE):
35 IE_NAME = 'ant1newsgr:watch'
36 IE_DESC = 'ant1news.gr videos'
37 _VALID_URL = r'https?://(?P<netloc>(?:www\.)?ant1news\.gr)/watch/(?P<id>\d+)/'
38 _API_PATH = '/templates/data/player'
39
40 _TESTS = [{
41 'url': 'https://www.ant1news.gr/watch/1506168/ant1-news-09112021-stis-18-45',
42 'md5': '95925e6b32106754235f2417e0d2dfab',
43 'info_dict': {
44 'id': '1506168',
45 'ext': 'mp4',
46 'title': 'md5:0ad00fa66ecf8aa233d26ab0dba7514a',
47 'description': 'md5:18665af715a6dcfeac1d6153a44f16b0',
48 'thumbnail': 'https://ant1media.azureedge.net/imgHandler/640/26d46bf6-8158-4f02-b197-7096c714b2de.jpg',
49 },
50 }]
51
52 def _real_extract(self, url):
53 video_id, netloc = self._match_valid_url(url).group('id', 'netloc')
54 webpage = self._download_webpage(url, video_id)
55 info = self._download_and_extract_api_data(video_id, netloc)
56 info['description'] = self._og_search_description(webpage)
57 return info
58
59
60 class Ant1NewsGrArticleIE(Ant1NewsGrBaseIE):
61 IE_NAME = 'ant1newsgr:article'
62 IE_DESC = 'ant1news.gr articles'
63 _VALID_URL = r'https?://(?:www\.)?ant1news\.gr/[^/]+/article/(?P<id>\d+)/'
64
65 _TESTS = [{
66 'url': 'https://www.ant1news.gr/afieromata/article/549468/o-tzeims-mpont-sta-meteora-oi-apeiles-kai-o-xesikomos-ton-kalogeron',
67 'md5': '294f18331bb516539d72d85a82887dcc',
68 'info_dict': {
69 'id': '_xvg/m_cmbatw=',
70 'ext': 'mp4',
71 'title': 'md5:a93e8ecf2e4073bfdffcb38f59945411',
72 'timestamp': 1603092840,
73 'upload_date': '20201019',
74 'thumbnail': 'https://ant1media.azureedge.net/imgHandler/640/756206d2-d640-40e2-b201-3555abdfc0db.jpg',
75 },
76 }, {
77 'url': 'https://ant1news.gr/Society/article/620286/symmoria-anilikon-dikigoros-thymaton-ithelan-na-toys-apoteleiosoyn',
78 'info_dict': {
79 'id': '620286',
80 'title': 'md5:91fe569e952e4d146485740ae927662b',
81 },
82 'playlist_mincount': 2,
83 'params': {
84 'skip_download': True,
85 },
86 }]
87
88 def _real_extract(self, url):
89 video_id = self._match_id(url)
90 webpage = self._download_webpage(url, video_id)
91 info = self._search_json_ld(webpage, video_id, expected_type='NewsArticle')
92 embed_urls = list(Ant1NewsGrEmbedIE._extract_embed_urls(url, webpage))
93 if not embed_urls:
94 raise ExtractorError('no videos found for %s' % video_id, expected=True)
95 return self.playlist_from_matches(
96 embed_urls, video_id, info.get('title'), ie=Ant1NewsGrEmbedIE.ie_key(),
97 video_kwargs={'url_transparent': True, 'timestamp': info.get('timestamp')})
98
99
100 class Ant1NewsGrEmbedIE(Ant1NewsGrBaseIE):
101 IE_NAME = 'ant1newsgr:embed'
102 IE_DESC = 'ant1news.gr embedded videos'
103 _BASE_PLAYER_URL_RE = r'(?:https?:)?//(?:[a-zA-Z0-9\-]+\.)?(?:antenna|ant1news)\.gr/templates/pages/player'
104 _VALID_URL = rf'{_BASE_PLAYER_URL_RE}\?([^#]+&)?cid=(?P<id>[^#&]+)'
105 _EMBED_REGEX = [rf'<iframe[^>]+?src=(?P<_q1>["\'])(?P<url>{_BASE_PLAYER_URL_RE}\?(?:(?!(?P=_q1)).)+)(?P=_q1)']
106 _API_PATH = '/news/templates/data/jsonPlayer'
107
108 _TESTS = [{
109 'url': 'https://www.antenna.gr/templates/pages/player?cid=3f_li_c_az_jw_y_u=&w=670&h=377',
110 'md5': 'dfc58c3a11a5a9aad2ba316ed447def3',
111 'info_dict': {
112 'id': '3f_li_c_az_jw_y_u=',
113 'ext': 'mp4',
114 'title': 'md5:a30c93332455f53e1e84ae0724f0adf7',
115 'thumbnail': 'https://ant1media.azureedge.net/imgHandler/640/bbe31201-3f09-4a4e-87f5-8ad2159fffe2.jpg',
116 },
117 }]
118
119 def _real_extract(self, url):
120 video_id = self._match_id(url)
121
122 canonical_url = self._request_webpage(
123 HEADRequest(url), video_id,
124 note='Resolve canonical player URL',
125 errnote='Could not resolve canonical player URL').geturl()
126 _, netloc, _, _, query, _ = urllib.parse.urlparse(canonical_url)
127 cid = urllib.parse.parse_qs(query)['cid'][0]
128
129 return self._download_and_extract_api_data(video_id, netloc, cid=cid)