Skip to main content

Downloading avatars from Tumblr

There’s an API endpoint that lets you download avatars in a variety of sizes.

I’m working on a project where I need to download Tumblr avatars. The Tumblr API has an endpoint precisely for this:

/avatar — Retrieve a Blog Avatar
You can get a blog’s avatar in 9 different sizes. The default size is 64x64.

Examples

https://api.tumblr.com/v2/blog/david.tumblr.com/avatar/512
https://api.tumblr.com/v2/blog/david.tumblr.com/avatar

This URL will redirect you to the location of the actual image file – the avatar URL in the example above redirects to https://64.media.tumblr.com/avatar_fdf0635a9d74_512.png.

Most avatars seem to be PNG, but I have seen a handful of JPEG images go past as well.

Here’s a Python snippet I wrote which will download the avatars to a filename with the appropriate extension:

from pathlib import Path

import httpx


def download_tumblr_avatar(blog_identifier: str) -> Path:
    """
    Download an avatar from Tumblr, and return a path to
    the downloaded file.
    """
    resp = httpx.get(
        f"https://api.tumblr.com/v2/blog/{blog_identifier}/avatar/512",
        follow_redirects=True,
    )
    resp.raise_for_status()

    content_type = resp.headers["content-type"]

    try:
        ext = {"image/jpeg": "jpg", "image/png": "png"}[content_type]
    except KeyError:
        raise RuntimeError(f"Unexpected Content-Type: {content_type!r}")

    dl_path = f"{blog_identifier}.{ext}"

    with open(dl_path, "xb") as out_file:
        out_file.write(resp.content)

    return dl_path


if __name__ == "__main__":
    print(download_tumblr_avatar(blog_identifier="david.tumblr.com"))
    print(download_tumblr_avatar(blog_identifier="staff"))
    print(download_tumblr_avatar(blog_identifier="marco"))
    print(download_tumblr_avatar(blog_identifier="t:WX5xItRbCiJH_GJDsIdhAQ"))