[youtube] Fix extraction (closes #17457, closes #17464)
[youtube-dl] / youtube_dl / extractor / mediaset.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .theplatform import ThePlatformBaseIE
7 from ..utils import (
8     ExtractorError,
9     int_or_none,
10     update_url_query,
11 )
12
13
14 class MediasetIE(ThePlatformBaseIE):
15     _TP_TLD = 'eu'
16     _VALID_URL = r'''(?x)
17                     (?:
18                         mediaset:|
19                         https?://
20                             (?:(?:www|static3)\.)?mediasetplay\.mediaset\.it/
21                             (?:
22                                 (?:video|on-demand)/(?:[^/]+/)+[^/]+_|
23                                 player/index\.html\?.*?\bprogramGuid=
24                             )
25                     )(?P<id>[0-9A-Z]{16})
26                     '''
27     _TESTS = [{
28         # full episode
29         'url': 'https://www.mediasetplay.mediaset.it/video/hellogoodbye/quarta-puntata_FAFU000000661824',
30         'md5': '9b75534d42c44ecef7bf1ffeacb7f85d',
31         'info_dict': {
32             'id': 'FAFU000000661824',
33             'ext': 'mp4',
34             'title': 'Quarta puntata',
35             'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
36             'thumbnail': r're:^https?://.*\.jpg$',
37             'duration': 1414.26,
38             'upload_date': '20161107',
39             'series': 'Hello Goodbye',
40             'timestamp': 1478532900,
41             'uploader': 'Rete 4',
42             'uploader_id': 'R4',
43         },
44     }, {
45         'url': 'https://www.mediasetplay.mediaset.it/video/matrix/puntata-del-25-maggio_F309013801000501',
46         'md5': '288532f0ad18307705b01e581304cd7b',
47         'info_dict': {
48             'id': 'F309013801000501',
49             'ext': 'mp4',
50             'title': 'Puntata del 25 maggio',
51             'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
52             'thumbnail': r're:^https?://.*\.jpg$',
53             'duration': 6565.007,
54             'upload_date': '20180526',
55             'series': 'Matrix',
56             'timestamp': 1527326245,
57             'uploader': 'Canale 5',
58             'uploader_id': 'C5',
59         },
60         'expected_warnings': ['HTTP Error 403: Forbidden'],
61     }, {
62         # clip
63         'url': 'https://www.mediasetplay.mediaset.it/video/gogglebox/un-grande-classico-della-commedia-sexy_FAFU000000661680',
64         'only_matching': True,
65     }, {
66         # iframe simple
67         'url': 'https://static3.mediasetplay.mediaset.it/player/index.html?appKey=5ad3966b1de1c4000d5cec48&programGuid=FAFU000000665924&id=665924',
68         'only_matching': True,
69     }, {
70         # iframe twitter (from http://www.wittytv.it/se-prima-mi-fidavo-zero/)
71         'url': 'https://static3.mediasetplay.mediaset.it/player/index.html?appKey=5ad3966b1de1c4000d5cec48&programGuid=FAFU000000665104&id=665104',
72         'only_matching': True,
73     }, {
74         'url': 'mediaset:FAFU000000665924',
75         'only_matching': True,
76     }]
77
78     @staticmethod
79     def _extract_urls(webpage):
80         return [
81             mobj.group('url')
82             for mobj in re.finditer(
83                 r'<iframe\b[^>]+\bsrc=(["\'])(?P<url>https?://(?:www\.)?video\.mediaset\.it/player/playerIFrame(?:Twitter)?\.shtml\?.*?\bid=\d+.*?)\1',
84                 webpage)]
85
86     def _real_extract(self, url):
87         guid = self._match_id(url)
88         tp_path = 'PR1GhC/media/guid/2702976343/' + guid
89         info = self._extract_theplatform_metadata(tp_path, guid)
90
91         formats = []
92         subtitles = {}
93         first_e = None
94         for asset_type in ('SD', 'HD'):
95             for f in ('MPEG4', 'MPEG-DASH', 'M3U', 'ISM'):
96                 try:
97                     tp_formats, tp_subtitles = self._extract_theplatform_smil(
98                         update_url_query('http://link.theplatform.%s/s/%s' % (self._TP_TLD, tp_path), {
99                             'mbr': 'true',
100                             'formats': f,
101                             'assetTypes': asset_type,
102                         }), guid, 'Downloading %s %s SMIL data' % (f, asset_type))
103                 except ExtractorError as e:
104                     if not first_e:
105                         first_e = e
106                     break
107                 for tp_f in tp_formats:
108                     tp_f['quality'] = 1 if asset_type == 'HD' else 0
109                 formats.extend(tp_formats)
110                 subtitles = self._merge_subtitles(subtitles, tp_subtitles)
111         if first_e and not formats:
112             raise first_e
113         self._sort_formats(formats)
114
115         fields = []
116         for templ, repls in (('tvSeason%sNumber', ('', 'Episode')), ('mediasetprogram$%s', ('brandTitle', 'numberOfViews', 'publishInfo'))):
117             fields.extend(templ % repl for repl in repls)
118         feed_data = self._download_json(
119             'https://feed.entertainment.tv.theplatform.eu/f/PR1GhC/mediaset-prod-all-programs/guid/-/' + guid,
120             guid, fatal=False, query={'fields': ','.join(fields)})
121         if feed_data:
122             publish_info = feed_data.get('mediasetprogram$publishInfo') or {}
123             info.update({
124                 'episode_number': int_or_none(feed_data.get('tvSeasonEpisodeNumber')),
125                 'season_number': int_or_none(feed_data.get('tvSeasonNumber')),
126                 'series': feed_data.get('mediasetprogram$brandTitle'),
127                 'uploader': publish_info.get('description'),
128                 'uploader_id': publish_info.get('channel'),
129                 'view_count': int_or_none(feed_data.get('mediasetprogram$numberOfViews')),
130             })
131
132         info.update({
133             'id': guid,
134             'formats': formats,
135             'subtitles': subtitles,
136         })
137         return info