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