]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/ccma.py
[ffmpeg] Cache version data
[yt-dlp.git] / yt_dlp / extractor / ccma.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6 clean_html,
7 int_or_none,
8 parse_duration,
9 parse_resolution,
10 try_get,
11 unified_timestamp,
12 url_or_none,
13 )
14
15
16 class CCMAIE(InfoExtractor):
17 _VALID_URL = r'https?://(?:www\.)?ccma\.cat/(?:[^/]+/)*?(?P<type>video|audio)/(?P<id>\d+)'
18 _TESTS = [{
19 'url': 'http://www.ccma.cat/tv3/alacarta/lespot-de-la-marato-de-tv3/lespot-de-la-marato-de-tv3/video/5630208/',
20 'md5': '7296ca43977c8ea4469e719c609b0871',
21 'info_dict': {
22 'id': '5630208',
23 'ext': 'mp4',
24 'title': 'L\'espot de La Marató de TV3',
25 'description': 'md5:f12987f320e2f6e988e9908e4fe97765',
26 'timestamp': 1478608140,
27 'upload_date': '20161108',
28 'age_limit': 0,
29 }
30 }, {
31 'url': 'http://www.ccma.cat/catradio/alacarta/programa/el-consell-de-savis-analitza-el-derbi/audio/943685/',
32 'md5': 'fa3e38f269329a278271276330261425',
33 'info_dict': {
34 'id': '943685',
35 'ext': 'mp3',
36 'title': 'El Consell de Savis analitza el derbi',
37 'description': 'md5:e2a3648145f3241cb9c6b4b624033e53',
38 'upload_date': '20170512',
39 'timestamp': 1494622500,
40 'vcodec': 'none',
41 'categories': ['Esports'],
42 }
43 }, {
44 'url': 'http://www.ccma.cat/tv3/alacarta/crims/crims-josep-tallada-lespereu-me-capitol-1/video/6031387/',
45 'md5': 'b43c3d3486f430f3032b5b160d80cbc3',
46 'info_dict': {
47 'id': '6031387',
48 'ext': 'mp4',
49 'title': 'Crims - Josep Talleda, l\'"Espereu-me" (capítol 1)',
50 'description': 'md5:7cbdafb640da9d0d2c0f62bad1e74e60',
51 'timestamp': 1582577700,
52 'upload_date': '20200224',
53 'subtitles': 'mincount:4',
54 'age_limit': 16,
55 'series': 'Crims',
56 }
57 }]
58
59 def _real_extract(self, url):
60 media_type, media_id = self._match_valid_url(url).groups()
61
62 media = self._download_json(
63 'http://dinamics.ccma.cat/pvideo/media.jsp', media_id, query={
64 'media': media_type,
65 'idint': media_id,
66 })
67
68 formats = []
69 media_url = media['media']['url']
70 if isinstance(media_url, list):
71 for format_ in media_url:
72 format_url = url_or_none(format_.get('file'))
73 if not format_url:
74 continue
75 label = format_.get('label')
76 f = parse_resolution(label)
77 f.update({
78 'url': format_url,
79 'format_id': label,
80 })
81 formats.append(f)
82 else:
83 formats.append({
84 'url': media_url,
85 'vcodec': 'none' if media_type == 'audio' else None,
86 })
87 self._sort_formats(formats)
88
89 informacio = media['informacio']
90 title = informacio['titol']
91 durada = informacio.get('durada') or {}
92 duration = int_or_none(durada.get('milisegons'), 1000) or parse_duration(durada.get('text'))
93 tematica = try_get(informacio, lambda x: x['tematica']['text'])
94
95 data_utc = try_get(informacio, lambda x: x['data_emissio']['utc'])
96 timestamp = unified_timestamp(data_utc)
97
98 subtitles = {}
99 subtitols = media.get('subtitols') or []
100 if isinstance(subtitols, dict):
101 subtitols = [subtitols]
102 for st in subtitols:
103 sub_url = st.get('url')
104 if sub_url:
105 subtitles.setdefault(
106 st.get('iso') or st.get('text') or 'ca', []).append({
107 'url': sub_url,
108 })
109
110 thumbnails = []
111 imatges = media.get('imatges', {})
112 if imatges:
113 thumbnail_url = imatges.get('url')
114 if thumbnail_url:
115 thumbnails = [{
116 'url': thumbnail_url,
117 'width': int_or_none(imatges.get('amplada')),
118 'height': int_or_none(imatges.get('alcada')),
119 }]
120
121 age_limit = None
122 codi_etic = try_get(informacio, lambda x: x['codi_etic']['id'])
123 if codi_etic:
124 codi_etic_s = codi_etic.split('_')
125 if len(codi_etic_s) == 2:
126 if codi_etic_s[1] == 'TP':
127 age_limit = 0
128 else:
129 age_limit = int_or_none(codi_etic_s[1])
130
131 return {
132 'id': media_id,
133 'title': title,
134 'description': clean_html(informacio.get('descripcio')),
135 'duration': duration,
136 'timestamp': timestamp,
137 'thumbnails': thumbnails,
138 'subtitles': subtitles,
139 'formats': formats,
140 'age_limit': age_limit,
141 'alt_title': informacio.get('titol_complet'),
142 'episode_number': int_or_none(informacio.get('capitol')),
143 'categories': [tematica] if tematica else None,
144 'series': informacio.get('programa'),
145 }