]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/tvopengr.py
[extractor] Support multiple archive ids for one video (#4307)
[yt-dlp.git] / yt_dlp / extractor / tvopengr.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 determine_ext,
6 get_elements_text_and_html_by_attribute,
7 scale_thumbnails_to_max_format_width,
8 unescapeHTML,
9 )
10
11
12 class TVOpenGrBaseIE(InfoExtractor):
13 def _return_canonical_url(self, url, video_id):
14 webpage = self._download_webpage(url, video_id)
15 canonical_url = self._og_search_url(webpage)
16 title = self._og_search_title(webpage)
17 return self.url_result(canonical_url, ie=TVOpenGrWatchIE.ie_key(), video_id=video_id, video_title=title)
18
19
20 class TVOpenGrWatchIE(TVOpenGrBaseIE):
21 IE_NAME = 'tvopengr:watch'
22 IE_DESC = 'tvopen.gr (and ethnos.gr) videos'
23 _VALID_URL = r'https?://(?P<netloc>(?:www\.)?(?:tvopen|ethnos)\.gr)/watch/(?P<id>\d+)/(?P<slug>[^/]+)'
24 _API_ENDPOINT = 'https://www.tvopen.gr/templates/data/player'
25
26 _TESTS = [{
27 'url': 'https://www.ethnos.gr/watch/101009/nikoskaprabelosdenexoymekanenanasthenhsemethmethmetallaxhomikron',
28 'md5': '8728570e3a72e0f8d9475ba94859fdc1',
29 'info_dict': {
30 'id': '101009',
31 'title': 'md5:51f68773dcb6c70498cd326f45fefdf0',
32 'display_id': 'nikoskaprabelosdenexoymekanenanasthenhsemethmethmetallaxhomikron',
33 'description': 'md5:78fff49f18fb3effe41b070e5c7685d6',
34 'thumbnail': 'https://opentv-static.siliconweb.com/imgHandler/1920/d573ba71-ec5f-43c6-b4cb-d181f327d3a8.jpg',
35 'ext': 'mp4',
36 'upload_date': '20220109',
37 'timestamp': 1641686400,
38 },
39 }, {
40 'url': 'https://www.tvopen.gr/watch/100979/se28099agapaomenalla7cepeisodio267cmhthrargiapashskakias',
41 'md5': '38f98a1be0c577db4ea2d1b1c0770c48',
42 'info_dict': {
43 'id': '100979',
44 'title': 'md5:e021f3001e16088ee40fa79b20df305b',
45 'display_id': 'se28099agapaomenalla7cepeisodio267cmhthrargiapashskakias',
46 'description': 'md5:ba17db53954134eb8d625d199e2919fb',
47 'thumbnail': 'https://opentv-static.siliconweb.com/imgHandler/1920/9bb71cf1-21da-43a9-9d65-367950fde4e3.jpg',
48 'ext': 'mp4',
49 'upload_date': '20220108',
50 'timestamp': 1641600000,
51 },
52 }]
53
54 def _extract_formats_and_subs(self, response, video_id):
55 formats, subs = [], {}
56 for format_id, format_url in response.items():
57 if format_id not in ('stream', 'httpstream', 'mpegdash'):
58 continue
59 ext = determine_ext(format_url)
60 if ext == 'm3u8':
61 formats_, subs_ = self._extract_m3u8_formats_and_subtitles(
62 format_url, video_id, 'mp4', m3u8_id=format_id,
63 fatal=False)
64 elif ext == 'mpd':
65 formats_, subs_ = self._extract_mpd_formats_and_subtitles(
66 format_url, video_id, 'mp4', fatal=False)
67 else:
68 formats.append({
69 'url': format_url,
70 'format_id': format_id,
71 })
72 continue
73 formats.extend(formats_)
74 self._merge_subtitles(subs_, target=subs)
75 self._sort_formats(formats)
76 return formats, subs
77
78 def _real_extract(self, url):
79 netloc, video_id, display_id = self._match_valid_url(url).group('netloc', 'id', 'slug')
80 if netloc.find('tvopen.gr') == -1:
81 return self._return_canonical_url(url, video_id)
82 webpage = self._download_webpage(url, video_id)
83 info = self._search_json_ld(webpage, video_id, expected_type='VideoObject')
84 info['formats'], info['subtitles'] = self._extract_formats_and_subs(
85 self._download_json(self._API_ENDPOINT, video_id, query={'cid': video_id}),
86 video_id)
87 info['thumbnails'] = scale_thumbnails_to_max_format_width(
88 info['formats'], info['thumbnails'], r'(?<=/imgHandler/)\d+')
89 description, _html = next(get_elements_text_and_html_by_attribute('class', 'description', webpage))
90 if description and _html.startswith('<span '):
91 info['description'] = description
92 info['id'] = video_id
93 info['display_id'] = display_id
94 return info
95
96
97 class TVOpenGrEmbedIE(TVOpenGrBaseIE):
98 IE_NAME = 'tvopengr:embed'
99 IE_DESC = 'tvopen.gr embedded videos'
100 _VALID_URL = r'(?:https?:)?//(?:www\.|cdn\.|)(?:tvopen|ethnos).gr/embed/(?P<id>\d+)'
101 _EMBED_RE = re.compile(rf'''<iframe[^>]+?src=(?P<_q1>["'])(?P<url>{_VALID_URL})(?P=_q1)''')
102
103 _TESTS = [{
104 'url': 'https://cdn.ethnos.gr/embed/100963',
105 'md5': '2da147881f45571d81662d94d086628b',
106 'info_dict': {
107 'id': '100963',
108 'display_id': 'koronoiosapotoysdieythyntestonsxoleionselftestgiaosoysdenbrhkan',
109 'title': 'md5:2c71876fadf0cda6043da0da5fca2936',
110 'description': 'md5:17482b4432e5ed30eccd93b05d6ea509',
111 'thumbnail': 'https://opentv-static.siliconweb.com/imgHandler/1920/5804e07f-799a-4247-a696-33842c94ca37.jpg',
112 'ext': 'mp4',
113 'upload_date': '20220108',
114 'timestamp': 1641600000,
115 },
116 }]
117
118 @classmethod
119 def _extract_urls(cls, webpage):
120 for mobj in cls._EMBED_RE.finditer(webpage):
121 yield unescapeHTML(mobj.group('url'))
122
123 def _real_extract(self, url):
124 video_id = self._match_id(url)
125 return self._return_canonical_url(url, video_id)