2017-01-28 14:51:52 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
from .common import InfoExtractor
|
2018-05-02 00:21:24 +00:00
|
|
|
from .brightcove import BrightcoveNewIE
|
2021-09-12 20:56:19 +00:00
|
|
|
|
|
|
|
from ..compat import compat_str
|
2017-01-28 14:51:52 +00:00
|
|
|
from ..utils import (
|
2021-09-12 20:56:19 +00:00
|
|
|
base_url,
|
2021-01-01 12:26:37 +00:00
|
|
|
clean_html,
|
2018-06-21 16:12:40 +00:00
|
|
|
determine_ext,
|
2017-01-28 14:51:52 +00:00
|
|
|
extract_attributes,
|
2021-09-12 20:56:19 +00:00
|
|
|
ExtractorError,
|
2021-01-01 12:26:37 +00:00
|
|
|
get_element_by_class,
|
|
|
|
JSON_LD_RE,
|
2018-06-21 16:06:58 +00:00
|
|
|
merge_dicts,
|
2017-01-28 14:51:52 +00:00
|
|
|
parse_duration,
|
2018-05-02 00:21:24 +00:00
|
|
|
smuggle_url,
|
2020-11-08 14:07:12 +00:00
|
|
|
try_get,
|
2018-07-21 12:08:28 +00:00
|
|
|
url_or_none,
|
2021-09-12 20:56:19 +00:00
|
|
|
url_basename,
|
|
|
|
urljoin,
|
2017-01-28 14:51:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ITVIE(InfoExtractor):
|
2017-01-28 16:25:15 +00:00
|
|
|
_VALID_URL = r'https?://(?:www\.)?itv\.com/hub/[^/]+/(?P<id>[0-9a-zA-Z]+)'
|
2017-02-18 20:53:23 +00:00
|
|
|
_GEO_COUNTRIES = ['GB']
|
2017-12-13 21:49:07 +00:00
|
|
|
_TESTS = [{
|
2021-09-12 20:56:19 +00:00
|
|
|
'url': 'https://www.itv.com/hub/plebs/2a1873a0002',
|
|
|
|
'info_dict': {
|
|
|
|
'id': '2a1873a0002',
|
|
|
|
'ext': 'mp4',
|
|
|
|
'title': 'Plebs - The Orgy',
|
|
|
|
'description': 'md5:4d7159af53ebd5b36e8b3ec82a41fdb4',
|
|
|
|
'series': 'Plebs',
|
|
|
|
'season_number': 1,
|
|
|
|
'episode_number': 1,
|
|
|
|
'thumbnail': r're:https?://hubimages\.itv\.com/episode/2_1873_0002'
|
|
|
|
},
|
|
|
|
'params': {
|
|
|
|
# m3u8 download
|
|
|
|
'skip_download': True,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
'url': 'https://www.itv.com/hub/the-jonathan-ross-show/2a1166a0209',
|
2017-01-28 14:51:52 +00:00
|
|
|
'info_dict': {
|
2021-09-12 20:56:19 +00:00
|
|
|
'id': '2a1166a0209',
|
2021-01-01 12:26:37 +00:00
|
|
|
'ext': 'mp4',
|
2021-09-12 20:56:19 +00:00
|
|
|
'title': 'The Jonathan Ross Show - Series 17 - Episode 8',
|
|
|
|
'description': 'md5:3023dcdd375db1bc9967186cdb3f1399',
|
|
|
|
'series': 'The Jonathan Ross Show',
|
|
|
|
'episode_number': 8,
|
|
|
|
'season_number': 17,
|
|
|
|
'thumbnail': r're:https?://hubimages\.itv\.com/episode/2_1873_0002'
|
2017-01-28 14:51:52 +00:00
|
|
|
},
|
|
|
|
'params': {
|
2021-01-01 12:26:37 +00:00
|
|
|
# m3u8 download
|
2017-01-28 14:51:52 +00:00
|
|
|
'skip_download': True,
|
|
|
|
},
|
2017-12-13 21:49:07 +00:00
|
|
|
}, {
|
|
|
|
# unavailable via data-playlist-url
|
|
|
|
'url': 'https://www.itv.com/hub/through-the-keyhole/2a2271a0033',
|
|
|
|
'only_matching': True,
|
2018-05-01 15:46:06 +00:00
|
|
|
}, {
|
|
|
|
# InvalidVodcrid
|
|
|
|
'url': 'https://www.itv.com/hub/james-martins-saturday-morning/2a5159a0034',
|
|
|
|
'only_matching': True,
|
|
|
|
}, {
|
|
|
|
# ContentUnavailable
|
|
|
|
'url': 'https://www.itv.com/hub/whos-doing-the-dishes/2a2898a0024',
|
|
|
|
'only_matching': True,
|
2017-12-13 21:49:07 +00:00
|
|
|
}]
|
2017-01-28 14:51:52 +00:00
|
|
|
|
2021-09-12 20:56:19 +00:00
|
|
|
def _generate_api_headers(self, hmac):
|
|
|
|
return merge_dicts({
|
2021-01-01 12:26:37 +00:00
|
|
|
'Accept': 'application/vnd.itv.vod.playlist.v2+json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'hmac': hmac.upper(),
|
2021-09-12 20:56:19 +00:00
|
|
|
}, self.geo_verification_headers())
|
|
|
|
|
|
|
|
def _call_api(self, video_id, playlist_url, headers, platform_tag, featureset, fatal=True):
|
|
|
|
return self._download_json(
|
|
|
|
playlist_url, video_id, data=json.dumps({
|
2021-01-01 12:26:37 +00:00
|
|
|
'user': {
|
|
|
|
'itvUserId': '',
|
|
|
|
'entitlements': [],
|
|
|
|
'token': ''
|
|
|
|
},
|
|
|
|
'device': {
|
|
|
|
'manufacturer': 'Safari',
|
|
|
|
'model': '5',
|
|
|
|
'os': {
|
|
|
|
'name': 'Windows NT',
|
|
|
|
'version': '6.1',
|
|
|
|
'type': 'desktop'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'client': {
|
|
|
|
'version': '4.1',
|
|
|
|
'id': 'browser'
|
|
|
|
},
|
|
|
|
'variantAvailability': {
|
|
|
|
'featureset': {
|
2021-09-12 20:56:19 +00:00
|
|
|
'min': featureset,
|
|
|
|
'max': featureset
|
2021-01-01 12:26:37 +00:00
|
|
|
},
|
2021-09-12 20:56:19 +00:00
|
|
|
'platformTag': platform_tag
|
2021-01-01 12:26:37 +00:00
|
|
|
}
|
2021-09-12 20:56:19 +00:00
|
|
|
}).encode(), headers=headers, fatal=fatal)
|
|
|
|
|
|
|
|
def _get_subtitles(self, video_id, variants, ios_playlist_url, headers, *args, **kwargs):
|
|
|
|
subtitles = {}
|
2021-09-17 20:55:49 +00:00
|
|
|
# Prefer last matching featureset
|
|
|
|
# See: https://github.com/yt-dlp/yt-dlp/issues/986
|
2021-09-12 20:56:19 +00:00
|
|
|
platform_tag_subs, featureset_subs = next(
|
|
|
|
((platform_tag, featureset)
|
2021-11-23 07:43:27 +00:00
|
|
|
for platform_tag, featuresets in reversed(list(variants.items())) for featureset in featuresets
|
2021-09-12 20:56:19 +00:00
|
|
|
if try_get(featureset, lambda x: x[2]) == 'outband-webvtt'),
|
|
|
|
(None, None))
|
2021-09-17 20:55:49 +00:00
|
|
|
|
|
|
|
if platform_tag_subs and featureset_subs:
|
2021-09-12 20:56:19 +00:00
|
|
|
subs_playlist = self._call_api(
|
|
|
|
video_id, ios_playlist_url, headers, platform_tag_subs, featureset_subs, fatal=False)
|
|
|
|
subs = try_get(subs_playlist, lambda x: x['Playlist']['Video']['Subtitles'], list) or []
|
|
|
|
for sub in subs:
|
|
|
|
if not isinstance(sub, dict):
|
|
|
|
continue
|
|
|
|
href = url_or_none(sub.get('Href'))
|
|
|
|
if not href:
|
|
|
|
continue
|
|
|
|
subtitles.setdefault('en', []).append({'url': href})
|
|
|
|
return subtitles
|
|
|
|
|
|
|
|
def _real_extract(self, url):
|
|
|
|
video_id = self._match_id(url)
|
|
|
|
webpage = self._download_webpage(url, video_id)
|
|
|
|
params = extract_attributes(self._search_regex(
|
|
|
|
r'(?s)(<[^>]+id="video"[^>]*>)', webpage, 'params'))
|
|
|
|
variants = self._parse_json(
|
|
|
|
try_get(params, lambda x: x['data-video-variants'], compat_str) or '{}',
|
|
|
|
video_id, fatal=False)
|
2021-09-17 20:55:49 +00:00
|
|
|
# Prefer last matching featureset
|
|
|
|
# See: https://github.com/yt-dlp/yt-dlp/issues/986
|
2021-09-12 20:56:19 +00:00
|
|
|
platform_tag_video, featureset_video = next(
|
|
|
|
((platform_tag, featureset)
|
2021-11-23 07:43:27 +00:00
|
|
|
for platform_tag, featuresets in reversed(list(variants.items())) for featureset in featuresets
|
2021-11-24 10:08:58 +00:00
|
|
|
if set(try_get(featureset, lambda x: x[:2]) or []) == {'aes', 'hls'}),
|
2021-09-12 20:56:19 +00:00
|
|
|
(None, None))
|
|
|
|
if not platform_tag_video or not featureset_video:
|
|
|
|
raise ExtractorError('No downloads available', expected=True, video_id=video_id)
|
2017-12-13 21:49:07 +00:00
|
|
|
|
2021-09-12 20:56:19 +00:00
|
|
|
ios_playlist_url = params.get('data-video-playlist') or params['data-video-id']
|
|
|
|
headers = self._generate_api_headers(params['data-video-hmac'])
|
|
|
|
ios_playlist = self._call_api(
|
|
|
|
video_id, ios_playlist_url, headers, platform_tag_video, featureset_video)
|
|
|
|
|
|
|
|
video_data = try_get(ios_playlist, lambda x: x['Playlist']['Video'], dict) or {}
|
|
|
|
ios_base_url = video_data.get('Base')
|
2017-12-13 21:49:07 +00:00
|
|
|
formats = []
|
2021-01-01 12:26:37 +00:00
|
|
|
for media_file in (video_data.get('MediaFiles') or []):
|
|
|
|
href = media_file.get('Href')
|
|
|
|
if not href:
|
|
|
|
continue
|
|
|
|
if ios_base_url:
|
|
|
|
href = ios_base_url + href
|
|
|
|
ext = determine_ext(href)
|
|
|
|
if ext == 'm3u8':
|
|
|
|
formats.extend(self._extract_m3u8_formats(
|
|
|
|
href, video_id, 'mp4', entry_protocol='m3u8_native',
|
|
|
|
m3u8_id='hls', fatal=False))
|
2018-06-21 16:06:58 +00:00
|
|
|
else:
|
2021-01-01 12:26:37 +00:00
|
|
|
formats.append({
|
|
|
|
'url': href,
|
2018-06-21 16:06:58 +00:00
|
|
|
})
|
2021-01-01 12:26:37 +00:00
|
|
|
self._sort_formats(formats)
|
|
|
|
info = self._search_json_ld(webpage, video_id, default={})
|
|
|
|
if not info:
|
|
|
|
json_ld = self._parse_json(self._search_regex(
|
|
|
|
JSON_LD_RE, webpage, 'JSON-LD', '{}',
|
|
|
|
group='json_ld'), video_id, fatal=False)
|
|
|
|
if json_ld and json_ld.get('@type') == 'BreadcrumbList':
|
|
|
|
for ile in (json_ld.get('itemListElement:') or []):
|
|
|
|
item = ile.get('item:') or {}
|
|
|
|
if item.get('@type') == 'TVEpisode':
|
|
|
|
item['@context'] = 'http://schema.org'
|
|
|
|
info = self._json_ld(item, video_id, fatal=False) or {}
|
|
|
|
break
|
|
|
|
|
2021-09-12 20:56:19 +00:00
|
|
|
thumbnails = []
|
|
|
|
thumbnail_url = try_get(params, lambda x: x['data-video-posterframe'], compat_str)
|
|
|
|
if thumbnail_url:
|
|
|
|
thumbnails.extend([{
|
|
|
|
'url': thumbnail_url.format(width=1920, height=1080, quality=100, blur=0, bg='false'),
|
|
|
|
'width': 1920,
|
|
|
|
'height': 1080,
|
|
|
|
}, {
|
|
|
|
'url': urljoin(base_url(thumbnail_url), url_basename(thumbnail_url)),
|
|
|
|
'preference': -2
|
|
|
|
}])
|
|
|
|
|
|
|
|
thumbnail_url = self._html_search_meta(['og:image', 'twitter:image'], webpage, default=None)
|
|
|
|
if thumbnail_url:
|
|
|
|
thumbnails.append({
|
|
|
|
'url': thumbnail_url,
|
|
|
|
})
|
|
|
|
self._remove_duplicate_formats(thumbnails)
|
|
|
|
|
2021-01-01 12:26:37 +00:00
|
|
|
return merge_dicts({
|
2017-01-28 14:51:52 +00:00
|
|
|
'id': video_id,
|
2021-01-01 12:26:37 +00:00
|
|
|
'title': self._html_search_meta(['og:title', 'twitter:title'], webpage),
|
2017-01-28 14:51:52 +00:00
|
|
|
'formats': formats,
|
2021-09-12 20:56:19 +00:00
|
|
|
'subtitles': self.extract_subtitles(video_id, variants, ios_playlist_url, headers),
|
2021-01-01 12:26:37 +00:00
|
|
|
'duration': parse_duration(video_data.get('Duration')),
|
|
|
|
'description': clean_html(get_element_by_class('episode-info__synopsis', webpage)),
|
2021-09-12 20:56:19 +00:00
|
|
|
'thumbnails': thumbnails
|
2021-01-01 12:26:37 +00:00
|
|
|
}, info)
|
2018-05-02 00:21:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ITVBTCCIE(InfoExtractor):
|
2021-10-28 10:57:09 +00:00
|
|
|
_VALID_URL = r'https?://(?:www\.)?itv\.com/(?:news|btcc)/(?:[^/]+/)*(?P<id>[^/?#&]+)'
|
|
|
|
_TESTS = [{
|
2020-11-08 14:07:12 +00:00
|
|
|
'url': 'https://www.itv.com/btcc/articles/btcc-2019-brands-hatch-gp-race-action',
|
2018-05-02 00:21:24 +00:00
|
|
|
'info_dict': {
|
2020-11-08 14:07:12 +00:00
|
|
|
'id': 'btcc-2019-brands-hatch-gp-race-action',
|
|
|
|
'title': 'BTCC 2019: Brands Hatch GP race action',
|
2018-05-02 00:21:24 +00:00
|
|
|
},
|
2020-11-08 19:35:54 +00:00
|
|
|
'playlist_count': 12,
|
2021-10-28 10:57:09 +00:00
|
|
|
}, {
|
|
|
|
'url': 'https://www.itv.com/news/2021-10-27/i-have-to-protect-the-country-says-rishi-sunak-as-uk-faces-interest-rate-hike',
|
|
|
|
'info_dict': {
|
|
|
|
'id': 'i-have-to-protect-the-country-says-rishi-sunak-as-uk-faces-interest-rate-hike',
|
|
|
|
'title': 'md5:6ef054dd9f069330db3dcc66cb772d32'
|
|
|
|
},
|
|
|
|
'playlist_count': 4
|
|
|
|
}]
|
|
|
|
BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/%s_default/index.html?videoId=%s'
|
2018-05-02 00:21:24 +00:00
|
|
|
|
|
|
|
def _real_extract(self, url):
|
|
|
|
playlist_id = self._match_id(url)
|
|
|
|
|
|
|
|
webpage = self._download_webpage(url, playlist_id)
|
|
|
|
|
2022-01-19 21:55:15 +00:00
|
|
|
json_map = try_get(
|
|
|
|
self._search_nextjs_data(webpage, playlist_id),
|
2020-11-08 14:07:12 +00:00
|
|
|
lambda x: x['props']['pageProps']['article']['body']['content']) or []
|
|
|
|
|
2021-10-28 10:57:09 +00:00
|
|
|
entries = []
|
2020-11-08 14:07:12 +00:00
|
|
|
for video in json_map:
|
2021-10-28 10:57:09 +00:00
|
|
|
if not any(video['data'].get(attr) == 'Brightcove' for attr in ('name', 'type')):
|
|
|
|
continue
|
|
|
|
video_id = video['data']['id']
|
|
|
|
account_id = video['data']['accountId']
|
|
|
|
player_id = video['data']['playerId']
|
|
|
|
entries.append(self.url_result(
|
|
|
|
smuggle_url(self.BRIGHTCOVE_URL_TEMPLATE % (account_id, player_id, video_id), {
|
2018-05-02 00:21:24 +00:00
|
|
|
# ITV does not like some GB IP ranges, so here are some
|
|
|
|
# IP blocks it accepts
|
|
|
|
'geo_ip_blocks': [
|
|
|
|
'193.113.0.0/16', '54.36.162.0/23', '159.65.16.0/21'
|
|
|
|
],
|
|
|
|
'referrer': url,
|
|
|
|
}),
|
2021-10-28 10:57:09 +00:00
|
|
|
ie=BrightcoveNewIE.ie_key(), video_id=video_id))
|
2018-05-02 00:21:24 +00:00
|
|
|
|
|
|
|
title = self._og_search_title(webpage, fatal=False)
|
|
|
|
|
|
|
|
return self.playlist_result(entries, playlist_id, title)
|