Skip to main content

Add a function for parsing Tumblr post URLs

ID
34a5049
date
2025-12-03 07:24:08+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
a67bf25
message
Add a function for parsing Tumblr post URLs
changed files
4 files, 56 additions, 2 deletions

Changed files

CHANGELOG.md (680) → CHANGELOG.md (745)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6a371ac..1ed49b5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # CHANGELOG
 
+## v7 - 2025-12-03
+
+Add the `parse_tumblr_post_url()` function.
+
 ## v6 - 2025-12-03
 
 Add the `parse_mastodon_post_url()` function.

src/chives/__init__.py (390) → src/chives/__init__.py (390)

diff --git a/src/chives/__init__.py b/src/chives/__init__.py
index f17eeea..2b7e989 100644
--- a/src/chives/__init__.py
+++ b/src/chives/__init__.py
@@ -11,4 +11,4 @@ I share across multiple sites.
 
 """
 
-__version__ = "6"
+__version__ = "7"

src/chives/urls.py (1062) → src/chives/urls.py (1651)

diff --git a/src/chives/urls.py b/src/chives/urls.py
index d4cf7d4..03a281d 100644
--- a/src/chives/urls.py
+++ b/src/chives/urls.py
@@ -6,6 +6,13 @@ from typing import TypedDict
 import hyperlink
 
 
+__all__ = [
+    "clean_youtube_url",
+    "parse_mastodon_post_url",
+    "parse_tumblr_post_url",
+]
+
+
 def clean_youtube_url(url: str) -> str:
     """
     Remove any query parameters from a YouTube URL that I don't
@@ -43,3 +50,20 @@ def parse_mastodon_post_url(url: str) -> tuple[str, str, str]:
         acct = u.path[0].replace("@", "")
 
     return server, acct, u.path[1]
+
+
+def parse_tumblr_post_url(url: str) -> tuple[str, str]:
+    """
+    Parse a Tumblr URL into its component parts.
+
+    Returns a tuple (blog_identifier, post ID).
+    """
+    u = hyperlink.parse(url)
+
+    if u.host == "www.tumblr.com":
+        return u.path[0], u.path[1]
+
+    if u.host.endswith(".tumblr.com") and len(u.path) >= 3 and u.path[0] == "post":
+        return u.host.replace(".tumblr.com", ""), u.path[1]
+
+    raise ValueError("Cannot parse Tumblr URL!")  # pragma: no cover

tests/test_urls.py (1991) → tests/test_urls.py (2653)

diff --git a/tests/test_urls.py b/tests/test_urls.py
index e38098d..b4a0eb4 100644
--- a/tests/test_urls.py
+++ b/tests/test_urls.py
@@ -2,7 +2,11 @@
 
 import pytest
 
-from chives.urls import clean_youtube_url, parse_mastodon_post_url
+from chives.urls import (
+    clean_youtube_url,
+    parse_mastodon_post_url,
+    parse_tumblr_post_url,
+)
 
 
 @pytest.mark.parametrize(
@@ -66,3 +70,25 @@ def test_parse_mastodon_post_url_errors(url: str, error: str) -> None:
     """
     with pytest.raises(ValueError, match=error):
         parse_mastodon_post_url(url)
+
+
+@pytest.mark.parametrize(
+    "url, blog_identifier, post_id",
+    [
+        (
+            "https://www.tumblr.com/kynvillingur/792473255236796416/",
+            "kynvillingur",
+            "792473255236796416",
+        ),
+        (
+            "https://cut3panda.tumblr.com/post/94093772689/for-some-people-the-more-you-get-to-know-them",
+            "cut3panda",
+            "94093772689",
+        ),
+    ],
+)
+def test_parse_tumblr_post_url(url: str, blog_identifier: str, post_id: str) -> None:
+    """
+    Tumblr URLs are parsed correctly.
+    """
+    assert parse_tumblr_post_url(url) == (blog_identifier, post_id)