summaryrefslogtreecommitdiffstats
path: root/yt_dlp/extractor/airtv.py
blob: 0b73a966edcc951ee15abee59f7d06fcd7487130 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from .common import InfoExtractor
from .youtube import YoutubeIE
from ..utils import (
    determine_ext,
    int_or_none,
    mimetype2ext,
    parse_iso8601,
    traverse_obj
)


class AirTVIE(InfoExtractor):
    _VALID_URL = r'https?://www\.air\.tv/watch\?v=(?P<id>\w+)'
    _TESTS = [{
        # without youtube_id
        'url': 'https://www.air.tv/watch?v=W87jcWleSn2hXZN47zJZsQ',
        'info_dict': {
            'id': 'W87jcWleSn2hXZN47zJZsQ',
            'ext': 'mp4',
            'release_date': '20221003',
            'release_timestamp': 1664792603,
            'channel_id': 'vgfManQlRQKgoFQ8i8peFQ',
            'title': 'md5:c12d49ed367c3dadaa67659aff43494c',
            'upload_date': '20221003',
            'duration': 151,
            'view_count': int,
            'thumbnail': 'https://cdn-sp-gcs.air.tv/videos/W/8/W87jcWleSn2hXZN47zJZsQ/b13fc56464f47d9d62a36d110b9b5a72-4096x2160_9.jpg',
            'timestamp': 1664792603,
        }
    }, {
        # with youtube_id
        'url': 'https://www.air.tv/watch?v=sv57EC8tRXG6h8dNXFUU1Q',
        'info_dict': {
            'id': '2ZTqmpee-bQ',
            'ext': 'mp4',
            'comment_count': int,
            'tags': 'count:11',
            'channel_follower_count': int,
            'like_count': int,
            'uploader': 'Newsflare',
            'thumbnail': 'https://i.ytimg.com/vi_webp/2ZTqmpee-bQ/maxresdefault.webp',
            'availability': 'public',
            'title': 'Geese Chase Alligator Across Golf Course',
            'uploader_id': 'NewsflareBreaking',
            'channel_url': 'https://www.youtube.com/channel/UCzSSoloGEz10HALUAbYhngQ',
            'description': 'md5:99b21d9cea59330149efbd9706e208f5',
            'age_limit': 0,
            'channel_id': 'UCzSSoloGEz10HALUAbYhngQ',
            'uploader_url': 'http://www.youtube.com/user/NewsflareBreaking',
            'view_count': int,
            'categories': ['News & Politics'],
            'live_status': 'not_live',
            'playable_in_embed': True,
            'channel': 'Newsflare',
            'duration': 37,
            'upload_date': '20180511',
        }
    }]

    def _get_formats_and_subtitle(self, json_data, video_id):
        formats, subtitles = [], {}
        for source in traverse_obj(json_data, 'sources', 'sources_desktop', ...):
            ext = determine_ext(source.get('src'), mimetype2ext(source.get('type')))
            if ext == 'm3u8':
                fmts, subs = self._extract_m3u8_formats_and_subtitles(source.get('src'), video_id)
                formats.extend(fmts)
                self._merge_subtitles(subs, target=subtitles)
            else:
                formats.append({'url': source.get('src'), 'ext': ext})
        return formats, subtitles

    def _real_extract(self, url):
        display_id = self._match_id(url)
        webpage = self._download_webpage(url, display_id)

        nextjs_json = self._search_nextjs_data(webpage, display_id)['props']['pageProps']['initialState']['videos'][display_id]
        if nextjs_json.get('youtube_id'):
            return self.url_result(
                f'https://www.youtube.com/watch?v={nextjs_json.get("youtube_id")}', YoutubeIE)

        formats, subtitles = self._get_formats_and_subtitle(nextjs_json, display_id)
        return {
            'id': display_id,
            'title': nextjs_json.get('title') or self._html_search_meta('og:title', webpage),
            'formats': formats,
            'subtitles': subtitles,
            'description': nextjs_json.get('description') or None,
            'duration': int_or_none(nextjs_json.get('duration')),
            'thumbnails': [
                {'url': thumbnail}
                for thumbnail in traverse_obj(nextjs_json, ('default_thumbnails', ...))],
            'channel_id': traverse_obj(nextjs_json, 'channel', 'channel_slug'),
            'timestamp': parse_iso8601(nextjs_json.get('created')),
            'release_timestamp': parse_iso8601(nextjs_json.get('published')),
            'view_count': int_or_none(nextjs_json.get('views')),
        }