Open
Conversation
milahu
added a commit
to milahu/substack2markdown
that referenced
this pull request
Dec 28, 2025
based on timf34#26
|
commit based on this PR: milahu@5811bb5 |
This was referenced Dec 28, 2025
milahu
reviewed
Jan 1, 2026
Comment on lines
+93
to
+99
| def sanitize_filename(url: str) -> str: | ||
| """Create a safe filename from URL or content.""" | ||
| # Extract original filename from CDN URL | ||
| if "substackcdn.com" in url: | ||
| # Get the actual image URL after the CDN parameters | ||
| original_url = unquote(url.split("https://")[1]) | ||
| filename = original_url.split("/")[-1] |
There was a problem hiding this comment.
filename can be wrong, because substackcdn.com/image/fetch can
- change the image size (crop) (ex: 1536x1024 → 1456x971)
- change the image format (ex: png → webp)
also, when a post uses the same original image in different sizes
then all image versions use the same filename (filename collisions)
example
cd $(mktemp -d)
$ wget 'https://substackcdn.com/image/fetch/$s_!UkZH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2c2394c-643c-4533-8d74-734252ae02ac_1536x1024.png'
$ mv * d2c2394c-643c-4533-8d74-734252ae02ac_1536x1024.png.webp
$ python
>>> import urllib.parse
>>> urllib.parse.unquote('https://substackcdn.com/image/fetch/$s_!UkZH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2c2394c-643c-4533-8d74-734252ae02ac_1536x1024.png')
'https://substackcdn.com/image/fetch/$s_!UkZH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https://substack-post-media.s3.amazonaws.com/public/images/d2c2394c-643c-4533-8d74-734252ae02ac_1536x1024.png'
$ wget 'https://substack-post-media.s3.amazonaws.com/public/images/d2c2394c-643c-4533-8d74-734252ae02ac_1536x1024.png'
$ identify *
d2c2394c-643c-4533-8d74-734252ae02ac_1536x1024.png PNG 1536x1024 1536x1024+0+0 8-bit sRGB 2.56234MiB 0.000u 0:00.000
d2c2394c-643c-4533-8d74-734252ae02ac_1536x1024.png.webp WEBP 1456x971 1456x971+0+0 8-bit sRGB 207452B 0.000u 0:00.000
simple fix: always download the original image
pro: simple
con: the original images can be huge! in my case 10x larger
def resolve_image_url(url: str) -> str:
"""Get the original image URL."""
# https://substackcdn.com/image/fetch/xxx/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fxxx
if url.startswith("https://substackcdn.com/image/fetch/"):
# substackcdn.com returns a compressed version of the original image
url = "https://" + unquote(url.split("/https%3A%2F%2F")[1])
return url url = resolve_image_url(url)
filename = sanitize_image_filename(url)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I'm using this tool to mirror some of my Substack posts to my website, and as part of that process I'd really like to host my own images instead of having them link to the Substack CDN!
In case this will help someone else, here's a PR 🙂
Here's a list of some tweaks I made to get that to happen:
--imagesflag that will download images for all posts being scraped into asubstack_images/folder--urlin the formathttps://example.substack.com/p/postname[](/path). Change these to just beso clicking on the images doesn't link to itself.As a bonus, the progress bars reflect image downloads (since they can take a while)! As an example: