4 from .common
import InfoExtractor
5 from ..networking
import HEADRequest
11 from ..utils
.traversal
import traverse_obj
14 class SubstackIE(InfoExtractor
):
15 _VALID_URL
= r
'https?://(?P<username>[\w-]+)\.substack\.com/p/(?P<id>[\w-]+)'
17 'url': 'https://haleynahman.substack.com/p/i-made-a-vlog?s=r',
18 'md5': 'f27e4fc6252001d48d479f45e65cdfd5',
22 'title': 'I MADE A VLOG',
23 'description': 'md5:9248af9a759321e1027226f988f54d96',
24 'thumbnail': 'md5:bec758a34d8ee9142d43bcebdf33af18',
25 'uploader': 'Maybe Baby',
26 'uploader_id': '33628',
29 'url': 'https://haleynahman.substack.com/p/-dear-danny-i-found-my-boyfriends?s=r',
30 'md5': '0a63eacec877a1171a62cfa69710fcea',
34 'title': "🎧 Dear Danny: I found my boyfriend's secret Twitter account",
35 'description': 'md5:a57f2439319e56e0af92dd0c95d75797',
36 'thumbnail': 'md5:daa40b6b79249417c14ff8103db29639',
37 'uploader': 'Maybe Baby',
38 'uploader_id': '33628',
41 'url': 'https://andrewzimmern.substack.com/p/mussels-with-black-bean-sauce-recipe',
42 'md5': 'fd3c07077b02444ff0130715b5f632bb',
46 'title': 'Mussels with Black Bean Sauce: Recipe of the Week #7',
47 'description': 'md5:b96234a2906c7d854d5229818d889515',
48 'thumbnail': 'md5:e30bfaa9da40e82aa62354263a9dd232',
49 'uploader': "Andrew Zimmern's Spilled Milk ",
50 'uploader_id': '577659',
53 # Podcast that needs its file extension resolved to mp3
54 'url': 'https://persuasion1.substack.com/p/summers',
55 'md5': '1456a755d46084744facdfac9edf900f',
59 'title': 'Larry Summers on What Went Wrong on Campus',
60 'description': 'Yascha Mounk and Larry Summers also discuss the promise and perils of artificial intelligence.',
61 'thumbnail': r
're:https://substackcdn\.com/image/.+\.jpeg',
62 'uploader': 'Persuasion',
63 'uploader_id': '61579',
68 def _extract_embed_urls(cls
, url
, webpage
):
69 if not re
.search(r
'<script[^>]+src=["\']https
://substackcdn
.com
/[^
"\']+\.js', webpage):
72 mobj = re.search(r'{[^}]*\\?["\']subdomain
\\?
["\']\s*:\s*\\?["\'](?P
<subdomain
>[^
\\"\']+)', webpage)
74 parsed = urllib.parse.urlparse(url)
75 yield parsed._replace(netloc=f'{mobj.group("subdomain
")}.substack.com').geturl()
76 raise cls.StopExtraction
78 def _extract_video_formats(self, video_id, url):
79 formats, subtitles = [], {}
80 for video_format in ('hls', 'mp4'):
81 video_url = urllib.parse.urljoin(url, f'/api/v1/video/upload/{video_id}/src?type={video_format}')
83 if video_format == 'hls':
84 fmts, subs = self._extract_m3u8_formats_and_subtitles(video_url, video_id, 'mp4', fatal=False)
86 self._merge_subtitles(subs, target=subtitles)
93 return formats, subtitles
95 def _real_extract(self, url):
96 display_id, username = self._match_valid_url(url).group('id', 'username')
97 webpage = self._download_webpage(url, display_id)
99 webpage_info = self._parse_json(self._search_json(
100 r'window\._preloads\s*=\s*JSON\.parse\(', webpage, 'json string',
101 display_id, transform_source=js_to_json, contains_pattern=r'"{(?s
:.+)}"'), display_id)
104 domain = traverse_obj(webpage_info, ('domainInfo', 'customDomain', {str}))
106 canonical_url = urllib.parse.urlparse(url)._replace(netloc=domain).geturl()
108 post_type = webpage_info['post']['type']
109 formats, subtitles = [], {}
110 if post_type == 'podcast':
111 fmt = {'url': webpage_info['post']['podcast_url']}
112 if not determine_ext(fmt['url'], default_ext=None):
113 # The redirected format URL expires but the original URL doesn't,
114 # so we only want to extract the extension from this request
115 fmt['ext'] = determine_ext(self._request_webpage(
116 HEADRequest(fmt['url']), display_id,
117 'Resolving podcast file extension',
118 'Podcast URL is invalid').url)
120 elif post_type == 'video':
121 formats, subtitles = self._extract_video_formats(webpage_info['post']['videoUpload']['id'], canonical_url)
123 self.raise_no_formats(f'Page type "{post_type}
" is not supported')
126 'id': str(webpage_info['post']['id']),
128 'subtitles': subtitles,
129 'title': traverse_obj(webpage_info, ('post', 'title')),
130 'description': traverse_obj(webpage_info, ('post', 'description')),
131 'thumbnail': traverse_obj(webpage_info, ('post', 'cover_image')),
132 'uploader': traverse_obj(webpage_info, ('pub', 'name')),
133 'uploader_id': str_or_none(traverse_obj(webpage_info, ('post', 'publication_id'))),
134 'webpage_url': canonical_url,