aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/s4c.py
blob: b152e668005c1f783c187297e7417a0b40f5e97e (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# coding: utf-8

from __future__ import unicode_literals

from functools import partial as partial_f

from .common import InfoExtractor
from ..utils import (
    float_or_none,
    merge_dicts,
    T,
    traverse_obj,
    txt_or_none,
    url_or_none,
)


class S4CIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?s4c\.cymru/clic/programme/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.s4c.cymru/clic/programme/861362209',
        'info_dict': {
            'id': '861362209',
            'ext': 'mp4',
            'title': 'Y Swn',
            'description': 'md5:f7681a30e4955b250b3224aa9fe70cf0',
            'duration': 5340,
            'thumbnail': 'https://www.s4c.cymru/amg/1920x1080/Y_Swn_2023S4C_099_ii.jpg',
        },
    }, {
        'url': 'https://www.s4c.cymru/clic/programme/856636948',
        'info_dict': {
            'id': '856636948',
            'ext': 'mp4',
            'title': 'Am Dro',
            'duration': 2880,
            'description': 'md5:100d8686fc9a632a0cb2db52a3433ffe',
            'thumbnail': 'https://www.s4c.cymru/amg/1920x1080/Am_Dro_2022-23S4C_P6_4005.jpg',
        },
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        details = self._download_json(
            'https://www.s4c.cymru/df/full_prog_details',
            video_id, query={
                'lang': 'e',
                'programme_id': video_id,
            }, fatal=False)

        player_config = self._download_json(
            'https://player-api.s4c-cdn.co.uk/player-configuration/prod', video_id, query={
                'programme_id': video_id,
                'signed': '0',
                'lang': 'en',
                'mode': 'od',
                'appId': 'clic',
                'streamName': '',
            }, note='Downloading player config JSON')

        m3u8_url = self._download_json(
            'https://player-api.s4c-cdn.co.uk/streaming-urls/prod', video_id, query={
                'mode': 'od',
                'application': 'clic',
                'region': 'WW',
                'extra': 'false',
                'thirdParty': 'false',
                'filename': player_config['filename'],
            }, note='Downloading streaming urls JSON')['hls']
        formats = self._extract_m3u8_formats(m3u8_url, video_id, 'mp4', m3u8_id='hls', entry_protocol='m3u8_native')
        self._sort_formats(formats)

        subtitles = {}
        for sub in traverse_obj(player_config, ('subtitles', lambda _, v: url_or_none(v['0']))):
            subtitles.setdefault(sub.get('3', 'en'), []).append({
                'url': sub['0'],
                'name': sub.get('1'),
            })

        return merge_dicts({
            'id': video_id,
            'formats': formats,
            'subtitles': subtitles,
            'thumbnail': url_or_none(player_config.get('poster')),
        }, traverse_obj(details, ('full_prog_details', 0, {
            'title': (('programme_title', 'series_title'), T(txt_or_none)),
            'description': ('full_billing', T(txt_or_none)),
            'duration': ('duration', T(partial_f(float_or_none, invscale=60))),
        }), get_all=False),
            rev=True)


class S4CSeriesIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?s4c\.cymru/clic/series/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.s4c.cymru/clic/series/864982911',
        'playlist_mincount': 6,
        'info_dict': {
            'id': '864982911',
            'title': 'Iaith ar Daith',
        },
    }, {
        'url': 'https://www.s4c.cymru/clic/series/866852587',
        'playlist_mincount': 8,
        'info_dict': {
            'id': '866852587',
            'title': 'FFIT Cymru',
        },
    }]

    def _real_extract(self, url):
        series_id = self._match_id(url)
        series_details = self._download_json(
            'https://www.s4c.cymru/df/series_details', series_id, query={
                'lang': 'e',
                'series_id': series_id,
                'show_prog_in_series': 'Y'
            }, note='Downloading series details JSON')

        return self.playlist_result(
            (self.url_result('https://www.s4c.cymru/clic/programme/' + episode_id, S4CIE, episode_id)
             for episode_id in traverse_obj(series_details, ('other_progs_in_series', Ellipsis, 'id'))),
            playlist_id=series_id, playlist_title=traverse_obj(
                series_details, ('full_prog_details', 0, 'series_title', T(txt_or_none))))