From 3d10b1b8a6fd3342f6b389affdbe984c87ba2f9e Mon Sep 17 00:00:00 2001 From: Francesco Frassinelli Date: Sun, 21 Jul 2019 00:24:01 +0200 Subject: [PATCH] Add support for raiplayradio.it --- docs/supportedsites.md | 2 + youtube_dl/extractor/extractors.py | 2 + youtube_dl/extractor/rai.py | 99 ++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) diff --git a/docs/supportedsites.md b/docs/supportedsites.md index 9ae6e5c96..afd34f785 100644 --- a/docs/supportedsites.md +++ b/docs/supportedsites.md @@ -718,6 +718,8 @@ - **RaiPlay** - **RaiPlayLive** - **RaiPlayPlaylist** + - **RaiPlayRadio** + - **RaiPlayRadioPlaylist** - **RayWenderlich** - **RayWenderlichCourse** - **RBMARadio** diff --git a/youtube_dl/extractor/extractors.py b/youtube_dl/extractor/extractors.py index 15f54a214..d313bb89e 100644 --- a/youtube_dl/extractor/extractors.py +++ b/youtube_dl/extractor/extractors.py @@ -919,6 +919,8 @@ from .rai import ( RaiPlayLiveIE, RaiPlayPlaylistIE, RaiIE, + RaiPlayRadioIE, + RaiPlayRadioPlaylistIE, ) from .raywenderlich import ( RayWenderlichIE, diff --git a/youtube_dl/extractor/rai.py b/youtube_dl/extractor/rai.py index 207a6c247..7cf97bb6c 100644 --- a/youtube_dl/extractor/rai.py +++ b/youtube_dl/extractor/rai.py @@ -4,6 +4,7 @@ import re from .common import InfoExtractor from ..compat import ( + compat_HTMLParser, compat_urlparse, compat_str, ) @@ -13,6 +14,7 @@ from ..utils import ( find_xpath_attr, fix_xml_ampersands, GeoRestrictedError, + get_element_by_class, int_or_none, parse_duration, strip_or_none, @@ -500,3 +502,100 @@ class RaiIE(RaiBaseIE): info.update(relinker_info) return info + + +class HTMLListAttrsParser(compat_HTMLParser): + def __init__(self): + compat_HTMLParser.__init__(self) + self.items = [] + self._level = 0 + + def handle_starttag(self, tag, attrs): + if tag == 'li' and self._level == 0: + self.items.append(dict(attrs)) + self._level += 1 + + def handle_endtag(self, tag): + self._level -= 1 + + +class RaiPlayRadioBaseIE(InfoExtractor): + _BASE = 'https://www.raiplayradio.it' + + def parse_list(self, webpage): + parser = HTMLListAttrsParser() + parser.feed(webpage) + parser.close() + return parser.items + + def get_playlist_iter(self, url): + webpage = self._download_webpage(url, url) + for attrs in self.parse_list(webpage): + title = attrs['data-title'].strip() + webpage = urljoin(url, attrs['data-mediapolis']) + audio_url = compat_str(self._request_webpage(webpage, title).geturl()) + yield { + 'url': audio_url, + 'id': attrs['data-uniquename'].lstrip('ContentItem-'), + 'title': title, + 'ext': determine_ext(audio_url), + 'thumbnail': urljoin(url, attrs['data-image']), + 'language': 'it', + } + + def get_playlist(self, *args, **kwargs): + return list(self.get_playlist_iter(*args, **kwargs)) + + +class RaiPlayRadioIE(RaiPlayRadioBaseIE): + _VALID_URL = r'%s/audio/.+?-(?P%s)\.html' % (RaiPlayRadioBaseIE._BASE, RaiBaseIE._UUID_RE) + _TEST = { + 'url': 'https://www.raiplayradio.it/audio/2019/07/RADIO3---LEZIONI-DI-MUSICA-36b099ff-4123-4443-9bf9-38e43ef5e025.html', + 'info_dict': { + 'id': '36b099ff-4123-4443-9bf9-38e43ef5e025', + 'ext': 'mp3', + 'title': 'Dal "Chiaro di luna" al "Clair de lune", prima parte con Giovanni Bietti', + 'thumbnail': 'https://www.raiplayradio.it/cropgd/400x400/dl/img/2019/07/08/1562590329054_luna.jpg', + 'language': 'it', + } + } + + def _real_extract(self, url): + list_url = url.replace('.html', '-list.html') + for entry in self.get_playlist_iter(list_url): + if entry['id'] == self._match_id(url): + return entry + + +class RaiPlayRadioPlaylistIE(RaiPlayRadioBaseIE): + _VALID_URL = r'%s/playlist/.+?-(?P%s)\.html' % (RaiPlayRadioBaseIE._BASE, RaiBaseIE._UUID_RE) + _TEST = { + 'url': 'https://www.raiplayradio.it/playlist/2017/12/Alice-nel-paese-delle-meraviglie-72371d3c-d998-49f3-8860-d168cfdf4966.html', + 'info_dict': { + 'id': '72371d3c-d998-49f3-8860-d168cfdf4966', + 'title': "Alice nel paese delle meraviglie", + 'description': "di Lewis Carrol letto da Aldo Busi", + }, + 'playlist_count': 11, + } + + def _real_extract(self, url): + playlist_id = self._match_id(url) + playlist_webpage = self._download_webpage(url, playlist_id) + playlist_title = unescapeHTML(self._html_search_regex(r'data-playlist-title="(.+?)"', playlist_webpage, 'title')) + playlist_creator = self._html_search_meta('nomeProgramma', playlist_webpage) + playlist_description = get_element_by_class('textDescriptionProgramma', playlist_webpage) + + player_href = self._html_search_regex(r'data-player-href="(.+?)"', playlist_webpage, 'href') + list_url = urljoin(url, player_href) + + entries = self.get_playlist(list_url) + for index, entry in enumerate(entries, start=1): + entry.update({ + 'track': entry['title'], + 'track_number': index, + 'album': playlist_title, + 'artist': playlist_creator, + }) + + return self.playlist_result(entries, playlist_id, playlist_title, playlist_description)