Day 25 of 100 Days Coding Challenges: Python
When I post a blog, there’s always that option to clean up the slug and make the URL look like it spent a summer at finishing school. I’ve done it occasionally—shortened a few links here and there like trimming bangs on a whim—but I never committed. I wasn’t sure if it really made a difference, or if I was just breaking up with my beautifully descriptive blog titles for no good reason.
But here’s the thing: shorter URLs are easier to share, easier to remember, and—let’s be honest—they just look more confident. Like a blog post that walks into a party and knows exactly where the snacks are. How about I create my own Python URL shortener script?
So I stopped overthinking and built a Python script to handle URL shortening. It’s a humble subroutine for now, but it could easily become part of a larger “Blog Publishing Flow”—one that’s as sleek as my ambition to automate everything, including procrastination.
Today’s Motivation / Challenge
Ever copy-paste a long blog URL into a text, only to watch it stretch across the entire message like it’s trying to steal the spotlight? Me too. Today’s challenge was about making my blog links compact and elegant—like a haiku for URLs. Plus, I wanted a reusable, no-fuss script I could tuck into my content workflow.
Purpose of the Code (Object)
This little Python script takes a long URL and turns it into a neat, short version using the TinyURL service. It’s a great way to tidy up links before sharing them on social media, email newsletters, or in text messages to your less tech-savvy relatives who panic when they see a string with too many slashes.
AI Prompt:
Create a Python script to clean up my URL code.
Functions & Features
- Takes any valid URL and returns a shortened version
- Uses the TinyURL API (via pyshorteners) to handle the shortening
- Simple enough to run as a subroutine in a larger automation flow
Requirements / Setup
pip install pyshorteners
Works on Python 3.6 and above.
Minimal Code Sample
import pyshorteners
def shorten_url(long_url):
return pyshorteners.Shortener().tinyurl.short(long_url)
# Example usage:
print(shorten_url(“https://yourblog.com/very/long/descriptive/link”))
This function takes your long-winded link and trims it into something social-media-ready.
Notes / Lessons Learned
I tested it using one of my husband’s blog posts:
https://kaizennekoproject.com/the-case-of-the-missing-kilometers-a-summer-running-mystery/
It transformed into:
https://tinyurl.com/2y8vt6v5
Yes, it’s shorter—but now I can’t immediately tell what the post is about: running, whether rue crime; or a mileage-based ghost story.
And then the SEO question popped into my head like a banner ad: Does a shorter link help me show up in search results? Spoiler: probably not—at least not when using a third-party shortener. I still have some investigating to do. But the code itself? It works. And sometimes, finishing something is more satisfying than overthinking it.
Optional Ideas for Expansion
- Add a clipboard copy feature to automatically copy the short URL
- Let users choose their shortener service (e.g., Bit.ly, is.gd)
- Build a simple Tkinter GUI for those who like buttons more than typing
