]>
jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/substack.py
4 from .common
import InfoExtractor
5 from ..utils
import str_or_none
, traverse_obj
8 class SubstackIE(InfoExtractor
):
9 _VALID_URL
= r
'https?://(?P<username>[\w-]+)\.substack\.com/p/(?P<id>[\w-]+)'
11 'url': 'https://haleynahman.substack.com/p/i-made-a-vlog?s=r',
12 'md5': 'f27e4fc6252001d48d479f45e65cdfd5',
16 'title': 'I MADE A VLOG',
17 'description': 'md5:10c01ff93439a62e70ce963b2aa0b7f6',
18 'thumbnail': 'md5:bec758a34d8ee9142d43bcebdf33af18',
19 'uploader': 'Maybe Baby',
20 'uploader_id': '33628',
23 'url': 'https://haleynahman.substack.com/p/-dear-danny-i-found-my-boyfriends?s=r',
24 'md5': '0a63eacec877a1171a62cfa69710fcea',
28 'title': "🎧 Dear Danny: I found my boyfriend's secret Twitter account",
29 'description': 'md5:a57f2439319e56e0af92dd0c95d75797',
30 'thumbnail': 'md5:daa40b6b79249417c14ff8103db29639',
31 'uploader': 'Maybe Baby',
32 'uploader_id': '33628',
35 'url': 'https://andrewzimmern.substack.com/p/mussels-with-black-bean-sauce-recipe',
36 'md5': 'fd3c07077b02444ff0130715b5f632bb',
40 'title': 'Mussels with Black Bean Sauce: Recipe of the Week #7',
41 'description': 'md5:b96234a2906c7d854d5229818d889515',
42 'thumbnail': 'md5:e30bfaa9da40e82aa62354263a9dd232',
43 'uploader': "Andrew Zimmern's Spilled Milk ",
44 'uploader_id': '577659',
49 def _extract_url(cls
, webpage
, url
):
50 if not re
.search(r
'<script[^>]+src=["\']https
://substackcdn
.com
/[^
"\']+\.js', webpage):
53 mobj = re.search(r'{[^}]*["\']subdomain
["\']\s*:\s*["\'](?P
<subdomain
>[^
"]+)', webpage)
55 parsed = urllib.parse.urlparse(url)
56 return parsed._replace(netloc=f'{mobj.group("subdomain")}.substack.com').geturl()
58 def _extract_video_formats(self, video_id, username):
59 formats, subtitles = [], {}
60 for video_format in ('hls', 'mp4'):
61 video_url = f'https://{username}.substack.com/api/v1/video/upload/{video_id}/src?type={video_format}'
63 if video_format == 'hls':
64 fmts, subs = self._extract_m3u8_formats_and_subtitles(video_url, video_id, 'mp4', fatal=False)
66 self._merge_subtitles(subs, target=subtitles)
73 return formats, subtitles
75 def _real_extract(self, url):
76 display_id, username = self._match_valid_url(url).group('id', 'username')
77 webpage = self._download_webpage(url, display_id)
79 webpage_info = self._search_json(r'<script[^>]*>\s*window\._preloads\s*=', webpage, 'preloads', display_id)
81 post_type = webpage_info['post']['type']
82 formats, subtitles = [], {}
83 if post_type == 'podcast':
84 formats, subtitles = [{'url': webpage_info['post']['podcast_url']}], {}
85 elif post_type == 'video':
86 formats, subtitles = self._extract_video_formats(webpage_info['post']['videoUpload']['id'], username)
88 self.raise_no_formats(f'Page type "{post_type}
" is not supported')
90 self._sort_formats(formats)
92 'id': str(webpage_info['post']['id']),
94 'subtitles': subtitles,
95 'title': traverse_obj(webpage_info, ('post', 'title')),
96 'description': traverse_obj(webpage_info, ('post', 'description')),
97 'thumbnail': traverse_obj(webpage_info, ('post', 'cover_image')),
98 'uploader': traverse_obj(webpage_info, ('pub', 'name')),
99 'uploader_id': str_or_none(traverse_obj(webpage_info, ('post', 'publication_id'))),