]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/tvnet.py
77426f7e6800847f00e1cf4dad20c4d68af1ddb4
[yt-dlp.git] / yt_dlp / extractor / tvnet.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 int_or_none,
6 unescapeHTML,
7 url_or_none,
8 )
9
10
11 class TVNetIE(InfoExtractor):
12 _VALID_URL = r'https?://(?:[^/]+)\.tvnet\.gov\.vn/[^/]+/(?:\d+/)?(?P<id>\d+)(?:/|$)'
13 _TESTS = [{
14 # video
15 'url': 'http://de.tvnet.gov.vn/video/109788/vtv1---bac-tuyet-tai-lao-cai-va-ha-giang/tin-nong-24h',
16 'md5': 'b4d7abe0252c9b47774760b7519c7558',
17 'info_dict': {
18 'id': '109788',
19 'ext': 'mp4',
20 'title': 'VTV1 - Bắc tuyết tại Lào Cai và Hà Giang',
21 'thumbnail': r're:(?i)https?://.*\.(?:jpg|png)',
22 'is_live': False,
23 'view_count': int,
24 },
25 }, {
26 # audio
27 'url': 'http://vn.tvnet.gov.vn/radio/27017/vov1---ban-tin-chieu-10062018/doi-song-va-xa-hoi',
28 'md5': 'b5875ce9b0a2eecde029216d0e6db2ae',
29 'info_dict': {
30 'id': '27017',
31 'ext': 'm4a',
32 'title': 'VOV1 - Bản tin chiều (10/06/2018)',
33 'thumbnail': r're:(?i)https?://.*\.(?:jpg|png)',
34 'is_live': False,
35 },
36 }, {
37 'url': 'http://us.tvnet.gov.vn/video/118023/129999/ngay-0705',
38 'info_dict': {
39 'id': '129999',
40 'ext': 'mp4',
41 'title': 'VTV1 - Quốc hội với cử tri (11/06/2018)',
42 'thumbnail': r're:(?i)https?://.*\.(?:jpg|png)',
43 'is_live': False,
44 },
45 'params': {
46 'skip_download': True,
47 },
48 }, {
49 # live stream
50 'url': 'http://us.tvnet.gov.vn/kenh-truyen-hinh/1011/vtv1',
51 'info_dict': {
52 'id': '1011',
53 'ext': 'mp4',
54 'title': r're:^VTV1 \| LiveTV [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
55 'thumbnail': r're:(?i)https?://.*\.(?:jpg|png)',
56 'is_live': True,
57 },
58 'params': {
59 'skip_download': True,
60 },
61 }, {
62 # radio live stream
63 'url': 'http://vn.tvnet.gov.vn/kenh-truyen-hinh/1014',
64 'info_dict': {
65 'id': '1014',
66 'ext': 'm4a',
67 'title': r're:VOV1 \| LiveTV [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
68 'thumbnail': r're:(?i)https?://.*\.(?:jpg|png)',
69 'is_live': True,
70 },
71 'params': {
72 'skip_download': True,
73 },
74 }, {
75 'url': 'http://us.tvnet.gov.vn/phim/6136/25510/vtv3---ca-mot-doi-an-oan-tap-1-50/phim-truyen-hinh',
76 'only_matching': True,
77 }]
78
79 def _real_extract(self, url):
80 video_id = self._match_id(url)
81
82 webpage = self._download_webpage(url, video_id)
83
84 title = self._og_search_title(
85 webpage, default=None) or self._html_search_meta(
86 'title', webpage, default=None) or self._search_regex(
87 r'<title>([^<]+)<', webpage, 'title')
88 title = re.sub(r'\s*-\s*TV Net\s*$', '', title)
89
90 if '/video/' in url or '/radio/' in url:
91 is_live = False
92 elif '/kenh-truyen-hinh/' in url:
93 is_live = True
94 else:
95 is_live = None
96
97 data_file = unescapeHTML(self._search_regex(
98 r'data-file=(["\'])(?P<url>(?:https?:)?//.+?)\1', webpage,
99 'data file', group='url'))
100
101 stream_urls = set()
102 formats = []
103 for stream in self._download_json(data_file, video_id):
104 if not isinstance(stream, dict):
105 continue
106 stream_url = url_or_none(stream.get('url'))
107 if stream_url in stream_urls or not stream_url:
108 continue
109 stream_urls.add(stream_url)
110 formats.extend(self._extract_m3u8_formats(
111 stream_url, video_id, 'mp4', live=is_live, m3u8_id='hls', fatal=False))
112
113 # better support for radio streams
114 if title.startswith('VOV'):
115 for f in formats:
116 f.update({
117 'ext': 'm4a',
118 'vcodec': 'none',
119 })
120
121 thumbnail = self._og_search_thumbnail(
122 webpage, default=None) or unescapeHTML(
123 self._search_regex(
124 r'data-image=(["\'])(?P<url>(?:https?:)?//.+?)\1', webpage,
125 'thumbnail', default=None, group='url'))
126
127 view_count = int_or_none(self._search_regex(
128 r'(?s)<div[^>]+\bclass=["\'].*?view-count[^>]+>.*?(\d+).*?</div>',
129 webpage, 'view count', default=None))
130
131 return {
132 'id': video_id,
133 'title': title,
134 'thumbnail': thumbnail,
135 'is_live': is_live,
136 'view_count': view_count,
137 'formats': formats,
138 }