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