Lately I've been spending my evenings and nights working on a super secret and above awesome project that I will talk about ... soon, when it's a bit more ready.
tl; dr -> github link to source
That project deals mostly with Twitter and harnessing url's from the stream. As anyone who's sniffed at Twitter lately can atest, therea re many, many many strange urls floating around, using any number of url shortening schemes or what seems to have become quite fashionable, a custom domains that are shortenered via services nobody could guess at.
But when you're trying to do something useful with these it's usually much easier when you have the original url to work with. Especially when it comes to doing things for specific domains.
What's needed is some sort of library that can unshort pretty much any url you throw at it. Surely someone has already made one right?
Turns out, outside of a few services, no such thing existed ... and I didn't want to use a third party service. Naturally I set out to make my own library for unshortening short links.
You can check out the source on github.
Example
It's pretty simple to use, only has one function you should care about :)
var unshortener = require("unshortener");
// you can pass in a url object or string
unshortener.expand("http://fb.me/UXVqinvO", function (url) {
console.log(url.href);
// prints: http://www.facebook.com/photo.php?pid=514346&l=30b4ab9bbd&id=132853273433650
});
Simple logic
The approach is pretty simple:
- take a url
- check if known service
- know it --> use an API
- wtf --> pretend to be a browser
As you can see, the library is pretty simple, just 68 sloc of JavaScript code. But simple solutions are good solutions I'm told and this little thing does everything I need it to do.
Right now it directly supports only bit.ly and is.gd, so if you know of any more shorteners with an expand API please tell me so I can add them. Following redirects is a bit messy and I'd prefer to do as much as possible through official API's.
Continue reading about node-unshortener can unshort any url
Semantically similar articles hand-picked by GPT-4
- Twitter embeds without JavaScript, pt1 – #CodeWithSwiz 29
- Do we rely on open APIs too much?
- Build privacy-focused blazing fast tweet embeds – CodeWithSwiz 30
- Hard work is a total waste of time
- Parsing JavaScript with JavaScript
Learned something new?
Read more Software Engineering Lessons from Production
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below 👇
Software Engineering Lessons from Production
Join Swizec's Newsletter and get insightful emails 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.
Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.
Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.
Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev
Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization
Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections
Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog
Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️