]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/telewebion.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / telewebion.py
CommitLineData
65de7d20 1from __future__ import annotations
e897bd82 2
e3a3ed8a 3import functools
65de7d20 4import json
e3a3ed8a 5import textwrap
65de7d20 6
856150d0 7from .common import InfoExtractor
65de7d20
SS
8from ..utils import ExtractorError, format_field, int_or_none, parse_iso8601
9from ..utils.traversal import traverse_obj
856150d0
YCH
10
11
65de7d20 12def _fmt_url(url):
e3a3ed8a 13 return functools.partial(format_field, template=url, default=None)
856150d0 14
65de7d20
SS
15
16class TelewebionIE(InfoExtractor):
17 _VALID_URL = r'https?://(?:www\.)?telewebion\.com/episode/(?P<id>(?:0x[a-fA-F\d]+|\d+))'
18 _TESTS = [{
19 'url': 'http://www.telewebion.com/episode/0x1b3139c/',
856150d0 20 'info_dict': {
65de7d20 21 'id': '0x1b3139c',
856150d0 22 'ext': 'mp4',
65de7d20
SS
23 'title': 'قرعه‌کشی لیگ قهرمانان اروپا',
24 'series': '+ فوتبال',
25 'series_id': '0x1b2505c',
26 'channel': 'شبکه 3',
27 'channel_id': '0x1b1a761',
28 'channel_url': 'https://telewebion.com/live/tv3',
29 'timestamp': 1425522414,
30 'upload_date': '20150305',
31 'release_timestamp': 1425517020,
32 'release_date': '20150305',
33 'duration': 420,
856150d0 34 'view_count': int,
65de7d20
SS
35 'tags': ['ورزشی', 'لیگ اروپا', 'اروپا'],
36 'thumbnail': 'https://static.telewebion.com/episodeImages/YjFhM2MxMDBkMDNiZTU0MjE5YjQ3ZDY0Mjk1ZDE0ZmUwZWU3OTE3OWRmMDAyODNhNzNkNjdmMWMzMWIyM2NmMA/default',
856150d0 37 },
65de7d20
SS
38 'skip_download': 'm3u8',
39 }, {
40 'url': 'https://telewebion.com/episode/162175536',
41 'info_dict': {
42 'id': '0x9aa9a30',
43 'ext': 'mp4',
44 'title': 'کارما یعنی این !',
45 'series': 'پاورقی',
46 'series_id': '0x29a7426',
47 'channel': 'شبکه 2',
48 'channel_id': '0x1b1a719',
49 'channel_url': 'https://telewebion.com/live/tv2',
50 'timestamp': 1699979968,
51 'upload_date': '20231114',
52 'release_timestamp': 1699991638,
53 'release_date': '20231114',
54 'duration': 78,
55 'view_count': int,
56 'tags': ['کلیپ های منتخب', ' کلیپ طنز ', ' کلیپ سیاست ', 'پاورقی', 'ویژه فلسطین'],
57 'thumbnail': 'https://static.telewebion.com/episodeImages/871e9455-7567-49a5-9648-34c22c197f5f/default',
856150d0 58 },
65de7d20
SS
59 'skip_download': 'm3u8',
60 }]
61
62 def _call_graphql_api(
63 self, operation, video_id, query,
64 variables: dict[str, tuple[str, str]] | None = None,
65 note='Downloading GraphQL JSON metadata',
66 ):
67 parameters = ''
68 if variables:
69 parameters = ', '.join(f'${name}: {type_}' for name, (type_, _) in variables.items())
70 parameters = f'({parameters})'
71
72 result = self._download_json('https://graph.telewebion.com/graphql', video_id, note, data=json.dumps({
73 'operationName': operation,
74 'query': f'query {operation}{parameters} @cacheControl(maxAge: 60) {{{query}\n}}\n',
75 'variables': {name: value for name, (_, value) in (variables or {}).items()}
76 }, separators=(',', ':')).encode(), headers={
77 'Content-Type': 'application/json',
78 'Accept': 'application/json',
79 })
80 if not result or traverse_obj(result, 'errors'):
81 message = ', '.join(traverse_obj(result, ('errors', ..., 'message', {str})))
82 raise ExtractorError(message or 'Unknown GraphQL API error')
83
84 return result['data']
856150d0
YCH
85
86 def _real_extract(self, url):
87 video_id = self._match_id(url)
65de7d20
SS
88 if not video_id.startswith('0x'):
89 video_id = hex(int(video_id))
90
e3a3ed8a 91 episode_data = self._call_graphql_api('getEpisodeDetail', video_id, textwrap.dedent('''
65de7d20
SS
92 queryEpisode(filter: {EpisodeID: $EpisodeId}, first: 1) {
93 title
94 program {
95 ProgramID
96 title
97 }
98 image
99 view_count
100 duration
101 started_at
102 created_at
103 channel {
104 ChannelID
105 name
106 descriptor
107 }
108 tags {
109 name
110 }
111 }
112 '''), {'EpisodeId': ('[ID!]', video_id)})
856150d0 113
65de7d20
SS
114 info_dict = traverse_obj(episode_data, ('queryEpisode', 0, {
115 'title': ('title', {str}),
116 'view_count': ('view_count', {int_or_none}),
117 'duration': ('duration', {int_or_none}),
118 'tags': ('tags', ..., 'name', {str}),
119 'release_timestamp': ('started_at', {parse_iso8601}),
120 'timestamp': ('created_at', {parse_iso8601}),
121 'series': ('program', 'title', {str}),
122 'series_id': ('program', 'ProgramID', {str}),
123 'channel': ('channel', 'name', {str}),
124 'channel_id': ('channel', 'ChannelID', {str}),
125 'channel_url': ('channel', 'descriptor', {_fmt_url('https://telewebion.com/live/%s')}),
126 'thumbnail': ('image', {_fmt_url('https://static.telewebion.com/episodeImages/%s/default')}),
127 'formats': (
128 'channel', 'descriptor', {str},
129 {_fmt_url(f'https://cdna.telewebion.com/%s/episode/{video_id}/playlist.m3u8')},
e3a3ed8a 130 {functools.partial(self._extract_m3u8_formats, video_id=video_id, ext='mp4', m3u8_id='hls')}),
65de7d20
SS
131 }))
132 info_dict['id'] = video_id
133 return info_dict