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