Release 2024.12.03
[yt-dlp3.git] / yt_dlp / extractor / wordpress.py
bloba0a31940535a3141f6532490a713883bd408926a
1 import re
3 from .common import InfoExtractor
4 from ..utils import (
5 extract_attributes,
6 get_elements_by_class,
7 get_elements_text_and_html_by_attribute,
8 int_or_none,
9 parse_duration,
10 traverse_obj,
14 # https://codex.wordpress.org/Playlist_Shortcode
15 class WordpressPlaylistEmbedIE(InfoExtractor):
16 _VALID_URL = False
17 IE_NAME = 'wordpress:playlist'
18 _WEBPAGE_TESTS = [{
19 # 5 WordPress playlists. This is using wpse-playlist, which is similar.
20 # See: https://github.com/birgire/wpse-playlist
21 'url': 'https://xlino.com/wordpress-playlist-shortcode-with-external-audio-or-video-files/',
22 'info_dict': {
23 'id': 'wordpress-playlist-shortcode-with-external-audio-or-video-files',
24 'title': 'WordPress: Playlist shortcode with external audio or video files – Birgir Erlendsson (birgire)',
25 'age_limit': 0,
27 'playlist_count': 5,
28 }, {
29 'url': 'https://pianoadventures.com/products/piano-adventures-level-1-lesson-book-enhanced-cd/',
30 'info_dict': {
31 'id': 'piano-adventures-level-1-lesson-book-enhanced-cd-wp-playlist-1',
32 'title': 'Wordpress Playlist',
33 'thumbnail': 'https://pianoadventures.com/wp-content/uploads/sites/13/2022/01/CD1002cover.jpg',
34 'age_limit': 0,
36 'playlist': [{
37 'info_dict': {
38 'id': 'CD1002-21',
39 'ext': 'mp3',
40 'title': '21 Half-Time Show',
41 'thumbnail': 'https://pianoadventures.com/wp-content/plugins/media-library-assistant/images/crystal/audio.png',
42 'album': 'Piano Adventures Level 1 Lesson Book (2nd Edition)',
43 'genre': 'Classical',
44 'duration': 49.0,
45 'artist': 'Nancy and Randall Faber',
46 'description': 'md5:a9f8e9aeabbd2912bc13cc0fab1a4ce8',
48 }],
49 'playlist_count': 6,
50 'params': {'skip_download': True},
53 def _extract_from_webpage(self, url, webpage):
54 # class should always be "wp-playlist-script"
55 # See: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/media.php#L2930
56 for i, j in enumerate(get_elements_by_class('wp-playlist-script', webpage)):
57 playlist_json = self._parse_json(j, self._generic_id(url), fatal=False, ignore_extra=True, errnote='') or {}
58 if not playlist_json:
59 continue
60 entries = [{
61 'id': self._generic_id(track['src']),
62 'title': track.get('title'),
63 'url': track.get('src'),
64 'thumbnail': traverse_obj(track, ('thumb', 'src')),
65 'album': traverse_obj(track, ('meta', 'album')),
66 'artist': traverse_obj(track, ('meta', 'artist')),
67 'genre': traverse_obj(track, ('meta', 'genre')),
68 'duration': parse_duration(traverse_obj(track, ('meta', 'length_formatted'))),
69 'description': track.get('description'),
70 'height': int_or_none(traverse_obj(track, ('dimensions', 'original', 'height'))),
71 'width': int_or_none(traverse_obj(track, ('dimensions', 'original', 'width'))),
72 } for track in traverse_obj(playlist_json, ('tracks', ...), expected_type=dict)]
73 yield self.playlist_result(entries, self._generic_id(url) + f'-wp-playlist-{i + 1}', 'Wordpress Playlist')
76 class WordpressMiniAudioPlayerEmbedIE(InfoExtractor):
77 # WordPress MB Mini Player Plugin
78 # https://wordpress.org/plugins/wp-miniaudioplayer/
79 # Note: This is for the WordPress plugin version only.
80 _VALID_URL = False
81 IE_NAME = 'wordpress:mb.miniAudioPlayer'
82 _WEBPAGE_TESTS = [{
83 # Version 1.8.10: https://plugins.trac.wordpress.org/browser/wp-miniaudioplayer/tags/1.8.10
84 'url': 'https://news.samsung.com/global/over-the-horizon-the-evolution-of-the-samsung-galaxy-brand-sound',
85 'info_dict': {
86 'id': 'over-the-horizon-the-evolution-of-the-samsung-galaxy-brand-sound',
87 'title': 'Over the Horizon: The Evolution of the Samsung Galaxy Brand Sound',
88 'age_limit': 0,
89 'thumbnail': 'https://img.global.news.samsung.com/global/wp-content/uploads/2015/04/OTH_Main_Title-e1429612467870.jpg',
90 'description': 'md5:bc3dd738d1f11d9232e94e6629983bf7',
92 'playlist': [{
93 'info_dict': {
94 'id': 'over_the_horizon_2013',
95 'ext': 'mp3',
96 'title': 'Over the Horizon 2013',
97 'url': 'http://news.samsung.com/global/wp-content/uploads/ringtones/over_the_horizon_2013.mp3',
99 }],
100 'playlist_count': 6,
101 'params': {'skip_download': True},
102 }, {
103 # Version 1.9.3: https://plugins.trac.wordpress.org/browser/wp-miniaudioplayer/tags/1.9.3
104 'url': 'https://www.booksontape.com/collections/audiobooks-with-teacher-guides/',
105 'info_dict': {
106 'id': 'audiobooks-with-teacher-guides',
107 'title': 'Audiobooks with Teacher Guides | Books on Tape',
108 'age_limit': 0,
109 'thumbnail': 'https://www.booksontape.com/wp-content/uploads/2016/09/bot-logo-1200x630.jpg',
111 'playlist_mincount': 12,
112 }, {
113 # Version 1.9.7: https://plugins.trac.wordpress.org/browser/wp-miniaudioplayer/tags/1.9.7
114 # But has spaces around href filter
115 'url': 'https://www.estudiords.com.br/temas/',
116 'info_dict': {
117 'id': 'temas',
118 'title': 'Temas Variados',
119 'age_limit': 0,
120 'timestamp': float,
121 'upload_date': str,
122 'thumbnail': 'https://www.estudiords.com.br/wp-content/uploads/2021/03/LOGO-TEMAS.png',
123 'description': 'md5:ab24d6a7ed0312ad2d466e721679f5a0',
125 'playlist_mincount': 30,
128 def _extract_from_webpage(self, url, webpage):
129 # Common function for the WordPress plugin version only.
130 mb_player_params = self._search_regex(
131 r'function\s*initializeMiniAudioPlayer\(\){[^}]+jQuery([^;]+)\.mb_miniPlayer',
132 webpage, 'mb player params', default=None)
133 if not mb_player_params:
134 return
135 # v1.55 - 1.9.3 has "a[href*='.mp3'] ,a[href*='.m4a']"
136 # v1.9.4+ has "a[href*='.mp3']" only
137 file_exts = re.findall(r'a\[href\s*\*=\s*\'\.([a-zA-Z\d]+)\'', mb_player_params)
138 if not file_exts:
139 return
141 candidates = get_elements_text_and_html_by_attribute(
142 'href', rf'(?:[^\"\']+\.(?:{"|".join(file_exts)}))', webpage, escape_value=False, tag='a')
144 for title, html in candidates:
145 attrs = extract_attributes(html)
146 # XXX: not tested - have not found any example of it being used
147 if any(c in (attrs.get('class') or '') for c in re.findall(r'\.not\("\.([^"]+)', mb_player_params)):
148 continue
149 href = attrs['href']
150 yield {
151 'id': self._generic_id(href),
152 'title': title or self._generic_title(href),
153 'url': href,