]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/tv2dk.py
Update upstream source from tag 'upstream/2020.05.08'
[youtubedl] / youtube_dl / extractor / tv2dk.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import re
6
7 from .common import InfoExtractor
8 from ..utils import (
9 determine_ext,
10 extract_attributes,
11 js_to_json,
12 url_or_none,
13 )
14
15
16 class TV2DKIE(InfoExtractor):
17 _VALID_URL = r'''(?x)
18 https?://
19 (?:www\.)?
20 (?:
21 tvsyd|
22 tv2ostjylland|
23 tvmidtvest|
24 tv2fyn|
25 tv2east|
26 tv2lorry|
27 tv2nord
28 )\.dk/
29 (:[^/]+/)*
30 (?P<id>[^/?\#&]+)
31 '''
32 _TESTS = [{
33 'url': 'https://www.tvsyd.dk/nyheder/28-10-2019/1930/1930-28-okt-2019?autoplay=1#player',
34 'info_dict': {
35 'id': '0_52jmwa0p',
36 'ext': 'mp4',
37 'title': '19:30 - 28. okt. 2019',
38 'timestamp': 1572290248,
39 'upload_date': '20191028',
40 'uploader_id': 'tvsyd',
41 'duration': 1347,
42 'view_count': int,
43 },
44 'params': {
45 'skip_download': True,
46 },
47 'add_ie': ['Kaltura'],
48 }, {
49 'url': 'https://www.tv2ostjylland.dk/artikel/minister-gaar-ind-i-sag-om-diabetes-teknologi',
50 'only_matching': True,
51 }, {
52 'url': 'https://www.tv2ostjylland.dk/nyheder/28-10-2019/22/2200-nyhederne-mandag-d-28-oktober-2019?autoplay=1#player',
53 'only_matching': True,
54 }, {
55 'url': 'https://www.tvmidtvest.dk/nyheder/27-10-2019/1930/1930-27-okt-2019',
56 'only_matching': True,
57 }, {
58 'url': 'https://www.tv2fyn.dk/artikel/fyn-kan-faa-landets-foerste-fabrik-til-groent-jetbraendstof',
59 'only_matching': True,
60 }, {
61 'url': 'https://www.tv2east.dk/artikel/gods-faar-indleveret-tonsvis-af-aebler-100-kilo-aebler-gaar-til-en-aeblebrandy',
62 'only_matching': True,
63 }, {
64 'url': 'https://www.tv2lorry.dk/koebenhavn/rasmus-paludan-evakueret-til-egen-demonstration#player',
65 'only_matching': True,
66 }, {
67 'url': 'https://www.tv2nord.dk/artikel/dybt-uacceptabelt',
68 'only_matching': True,
69 }]
70
71 def _real_extract(self, url):
72 video_id = self._match_id(url)
73
74 webpage = self._download_webpage(url, video_id)
75
76 entries = []
77 for video_el in re.findall(r'(?s)<[^>]+\bdata-entryid\s*=[^>]*>', webpage):
78 video = extract_attributes(video_el)
79 kaltura_id = video.get('data-entryid')
80 if not kaltura_id:
81 continue
82 partner_id = video.get('data-partnerid')
83 if not partner_id:
84 continue
85 entries.append(self.url_result(
86 'kaltura:%s:%s' % (partner_id, kaltura_id), 'Kaltura',
87 video_id=kaltura_id))
88 return self.playlist_result(entries)
89
90
91 class TV2DKBornholmPlayIE(InfoExtractor):
92 _VALID_URL = r'https?://play\.tv2bornholm\.dk/\?.*?\bid=(?P<id>\d+)'
93 _TEST = {
94 'url': 'http://play.tv2bornholm.dk/?area=specifikTV&id=781021',
95 'info_dict': {
96 'id': '781021',
97 'ext': 'mp4',
98 'title': '12Nyheder-27.11.19',
99 },
100 'params': {
101 'skip_download': True,
102 },
103 }
104
105 def _real_extract(self, url):
106 video_id = self._match_id(url)
107
108 video = self._download_json(
109 'https://play.tv2bornholm.dk/controls/AJAX.aspx/specifikVideo', video_id,
110 data=json.dumps({
111 'playlist_id': video_id,
112 'serienavn': '',
113 }).encode(), headers={
114 'X-Requested-With': 'XMLHttpRequest',
115 'Content-Type': 'application/json; charset=UTF-8',
116 })['d']
117
118 # TODO: generalize flowplayer
119 title = self._search_regex(
120 r'title\s*:\s*(["\'])(?P<value>(?:(?!\1).)+)\1', video, 'title',
121 group='value')
122 sources = self._parse_json(self._search_regex(
123 r'(?s)sources:\s*(\[.+?\]),', video, 'sources'),
124 video_id, js_to_json)
125
126 formats = []
127 srcs = set()
128 for source in sources:
129 src = url_or_none(source.get('src'))
130 if not src:
131 continue
132 if src in srcs:
133 continue
134 srcs.add(src)
135 ext = determine_ext(src)
136 src_type = source.get('type')
137 if src_type == 'application/x-mpegurl' or ext == 'm3u8':
138 formats.extend(self._extract_m3u8_formats(
139 src, video_id, ext='mp4', entry_protocol='m3u8_native',
140 m3u8_id='hls', fatal=False))
141 elif src_type == 'application/dash+xml' or ext == 'mpd':
142 formats.extend(self._extract_mpd_formats(
143 src, video_id, mpd_id='dash', fatal=False))
144 else:
145 formats.append({
146 'url': src,
147 })
148 self._sort_formats(formats)
149
150 return {
151 'id': video_id,
152 'title': title,
153 'formats': formats,
154 }