]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/ccma.py
debian/changelog: Reword for better consumption of users.
[youtubedl] / youtube_dl / extractor / ccma.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import (
9 clean_html,
10 int_or_none,
11 parse_duration,
12 parse_iso8601,
13 parse_resolution,
14 )
15
16
17 class CCMAIE(InfoExtractor):
18 _VALID_URL = r'https?://(?:www\.)?ccma\.cat/(?:[^/]+/)*?(?P<type>video|audio)/(?P<id>\d+)'
19 _TESTS = [{
20 'url': 'http://www.ccma.cat/tv3/alacarta/lespot-de-la-marato-de-tv3/lespot-de-la-marato-de-tv3/video/5630208/',
21 'md5': '7296ca43977c8ea4469e719c609b0871',
22 'info_dict': {
23 'id': '5630208',
24 'ext': 'mp4',
25 'title': 'L\'espot de La Marató de TV3',
26 'description': 'md5:f12987f320e2f6e988e9908e4fe97765',
27 'timestamp': 1470918540,
28 'upload_date': '20160811',
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': '20171205',
39 'timestamp': 1512507300,
40 }
41 }]
42
43 def _real_extract(self, url):
44 media_type, media_id = re.match(self._VALID_URL, url).groups()
45
46 media = self._download_json(
47 'http://dinamics.ccma.cat/pvideo/media.jsp', media_id, query={
48 'media': media_type,
49 'idint': media_id,
50 })
51
52 formats = []
53 media_url = media['media']['url']
54 if isinstance(media_url, list):
55 for format_ in media_url:
56 format_url = format_.get('file')
57 if not format_url or not isinstance(format_url, compat_str):
58 continue
59 label = format_.get('label')
60 f = parse_resolution(label)
61 f.update({
62 'url': format_url,
63 'format_id': label,
64 })
65 formats.append(f)
66 else:
67 formats.append({
68 'url': media_url,
69 'vcodec': 'none' if media_type == 'audio' else None,
70 })
71 self._sort_formats(formats)
72
73 informacio = media['informacio']
74 title = informacio['titol']
75 durada = informacio.get('durada', {})
76 duration = int_or_none(durada.get('milisegons'), 1000) or parse_duration(durada.get('text'))
77 timestamp = parse_iso8601(informacio.get('data_emissio', {}).get('utc'))
78
79 subtitles = {}
80 subtitols = media.get('subtitols', {})
81 if subtitols:
82 sub_url = subtitols.get('url')
83 if sub_url:
84 subtitles.setdefault(
85 subtitols.get('iso') or subtitols.get('text') or 'ca', []).append({
86 'url': sub_url,
87 })
88
89 thumbnails = []
90 imatges = media.get('imatges', {})
91 if imatges:
92 thumbnail_url = imatges.get('url')
93 if thumbnail_url:
94 thumbnails = [{
95 'url': thumbnail_url,
96 'width': int_or_none(imatges.get('amplada')),
97 'height': int_or_none(imatges.get('alcada')),
98 }]
99
100 return {
101 'id': media_id,
102 'title': title,
103 'description': clean_html(informacio.get('descripcio')),
104 'duration': duration,
105 'timestamp': timestamp,
106 'thumbnails': thumbnails,
107 'subtitles': subtitles,
108 'formats': formats,
109 }