]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/megatvcom.py
[cleanup] Misc fixes
[yt-dlp.git] / yt_dlp / extractor / megatvcom.py
CommitLineData
32b95bb6
ZM
1# coding: utf-8
2from __future__ import unicode_literals
3
4import re
5
6from .common import InfoExtractor
7from ..utils import (
8 clean_html,
9 determine_ext,
10 ExtractorError,
11 extract_attributes,
12 get_element_by_class,
13 get_element_html_by_id,
14 HEADRequest,
15 parse_qs,
16 unescapeHTML,
17 unified_timestamp,
18)
19
20
21class MegaTVComBaseIE(InfoExtractor):
22 _PLAYER_DIV_ID = 'player_div_id'
23
24 def _extract_player_attrs(self, webpage):
25 player_el = get_element_html_by_id(self._PLAYER_DIV_ID, webpage)
26 return {
27 re.sub(r'^data-(?:kwik_)?', '', k): v
28 for k, v in extract_attributes(player_el).items()
29 if k not in ('id',)
30 }
31
32
33class MegaTVComIE(MegaTVComBaseIE):
34 IE_NAME = 'megatvcom'
35 IE_DESC = 'megatv.com videos'
36 _VALID_URL = r'https?://(?:www\.)?megatv\.com/(?:\d{4}/\d{2}/\d{2}|[^/]+/(?P<id>\d+))/(?P<slug>[^/]+)'
37
38 _TESTS = [{
39 'url': 'https://www.megatv.com/2021/10/23/egkainia-gia-ti-nea-skini-omega-tou-dimotikou-theatrou-peiraia/',
40 'md5': '6546a1a37fff0dd51c9dce5f490b7d7d',
41 'info_dict': {
42 'id': '520979',
43 'ext': 'mp4',
44 'title': 'md5:70eef71a9cd2c1ecff7ee428354dded2',
45 'description': 'md5:0209fa8d318128569c0d256a5c404db1',
46 'timestamp': 1634975747,
47 'upload_date': '20211023',
48 'display_id': 'egkainia-gia-ti-nea-skini-omega-tou-dimotikou-theatrou-peiraia',
49 'thumbnail': 'https://www.megatv.com/wp-content/uploads/2021/10/ΠΕΙΡΑΙΑΣ-1024x450.jpg',
50 },
51 }, {
52 'url': 'https://www.megatv.com/tvshows/527800/epeisodio-65-12/',
53 'md5': 'cba2085d45c1abeb8e7e9b7e1d6c0072',
54 'info_dict': {
55 'id': '527800',
56 'ext': 'mp4',
57 'title': 'md5:fc322cb51f682eecfe2f54cd5ab3a157',
58 'description': 'md5:b2b7ed3690a78f2a0156eb790fdc00df',
59 'timestamp': 1636048859,
60 'upload_date': '20211104',
61 'display_id': 'epeisodio-65-12',
62 'thumbnail': 'https://www.megatv.com/wp-content/uploads/2021/11/16-1-1.jpg',
63 },
64 }]
65
66 def _real_extract(self, url):
67 video_id, display_id = self._match_valid_url(url).group('id', 'slug')
68 _is_article = video_id is None
69 webpage = self._download_webpage(url, video_id or display_id)
70 if _is_article:
71 video_id = self._search_regex(
72 r'<article[^>]*\sid=["\']Article_(\d+)["\']', webpage, 'article id')
73 player_attrs = self._extract_player_attrs(webpage)
74 title = player_attrs.get('label') or self._og_search_title(webpage)
75 description = get_element_by_class(
76 'article-wrapper' if _is_article else 'story_content',
77 webpage)
78 description = clean_html(re.sub(r'<script[^>]*>[^<]+</script>', '', description))
79 if not description:
80 description = self._og_search_description(webpage)
81 thumbnail = player_attrs.get('image') or self._og_search_thumbnail(webpage)
82 timestamp = unified_timestamp(self._html_search_meta(
83 'article:published_time', webpage))
84 source = player_attrs.get('source')
85 if not source:
86 raise ExtractorError('No source found', video_id=video_id)
87 if determine_ext(source) == 'm3u8':
88 formats, subs = self._extract_m3u8_formats_and_subtitles(source, video_id, 'mp4')
89 else:
90 formats, subs = [{'url': source}], {}
91 if player_attrs.get('subs'):
92 self._merge_subtitles({'und': [{'url': player_attrs['subs']}]}, target=subs)
93 self._sort_formats(formats)
94 return {
95 'id': video_id,
96 'display_id': display_id,
97 'title': title,
98 'description': description,
99 'thumbnail': thumbnail,
100 'timestamp': timestamp,
101 'formats': formats,
102 'subtitles': subs,
103 }
104
105
106class MegaTVComEmbedIE(MegaTVComBaseIE):
107 IE_NAME = 'megatvcom:embed'
108 IE_DESC = 'megatv.com embedded videos'
109 _VALID_URL = r'(?:https?:)?//(?:www\.)?megatv\.com/embed/?\?p=(?P<id>\d+)'
110 _EMBED_RE = re.compile(rf'''<iframe[^>]+?src=(?P<_q1>["'])(?P<url>{_VALID_URL})(?P=_q1)''')
111
112 _TESTS = [{
113 'url': 'https://www.megatv.com/embed/?p=2020520979',
114 'md5': '6546a1a37fff0dd51c9dce5f490b7d7d',
115 'info_dict': {
116 'id': '520979',
117 'ext': 'mp4',
118 'title': 'md5:70eef71a9cd2c1ecff7ee428354dded2',
119 'description': 'md5:0209fa8d318128569c0d256a5c404db1',
120 'timestamp': 1634975747,
121 'upload_date': '20211023',
122 'display_id': 'egkainia-gia-ti-nea-skini-omega-tou-dimotikou-theatrou-peiraia',
123 'thumbnail': 'https://www.megatv.com/wp-content/uploads/2021/10/ΠΕΙΡΑΙΑΣ-1024x450.jpg',
124 },
125 }, {
126 'url': 'https://www.megatv.com/embed/?p=2020534081',
127 'md5': '6ac8b3ce4dc6120c802f780a1e6b3812',
128 'info_dict': {
129 'id': '534081',
130 'ext': 'mp4',
131 'title': 'md5:062e9d5976ef854d8bdc1f5724d9b2d0',
132 'description': 'md5:36dbe4c3762d2ede9513eea8d07f6d52',
133 'timestamp': 1636376351,
134 'upload_date': '20211108',
135 'display_id': 'neo-rekor-stin-timi-tou-ilektrikou-reymatos-pano-apo-ta-200e-i-xondriki-timi-tou-ilektrikou',
136 'thumbnail': 'https://www.megatv.com/wp-content/uploads/2021/11/Capture-266.jpg',
137 },
138 }]
139
140 @classmethod
141 def _extract_urls(cls, webpage):
142 for mobj in cls._EMBED_RE.finditer(webpage):
143 yield unescapeHTML(mobj.group('url'))
144
145 def _match_canonical_url(self, webpage):
146 LINK_RE = r'''(?x)
147 <link(?:
148 rel=(?P<_q1>["'])(?P<canonical>canonical)(?P=_q1)|
149 href=(?P<_q2>["'])(?P<href>(?:(?!(?P=_q2)).)+)(?P=_q2)|
150 [^>]*?
151 )+>
152 '''
153 for mobj in re.finditer(LINK_RE, webpage):
154 canonical, href = mobj.group('canonical', 'href')
155 if canonical and href:
156 return unescapeHTML(href)
157
158 def _real_extract(self, url):
159 video_id = self._match_id(url)
160 webpage = self._download_webpage(url, video_id)
161 player_attrs = self._extract_player_attrs(webpage)
162 canonical_url = player_attrs.get('share_url') or self._match_canonical_url(webpage)
163 if not canonical_url:
164 raise ExtractorError('canonical URL not found')
165 video_id = parse_qs(canonical_url)['p'][0]
166
167 # Defer to megatvcom as the metadata extracted from the embeddable page some
168 # times are slightly different, for the same video
169 canonical_url = self._request_webpage(
170 HEADRequest(canonical_url), video_id,
171 note='Resolve canonical URL',
172 errnote='Could not resolve canonical URL').geturl()
173 return self.url_result(canonical_url, MegaTVComIE.ie_key(), video_id)