hjr265.me / blog /

Parsing Social Media URLs in Go With Slinky

October 14, 2023 #100DaysToOffload #Go

Toph now allows programmers to show up to 5 social media URLs on their profile pages.

Instead of showing the entire URL, I wanted to show the important bits from the URL.

Screenshot of a profile panel from Toph
Screenshot of a profile panel from Toph

To do that, I had to parse the social media URLs and extract information like the username or profile ID (when it is a GitHub, Twitter, LinkedIn, Facebook, etc. profile URL) or the instance of Mastodon.

So, I built Slinky. It is a Go package that parses social media URLs into structured data.

Right now, it supports URLs from the following social media platforms:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Facebook
"facebook.com"
"www.facebook.com"
"web.facebook.com"
"fb.me"

// FLOSS.social
"floss.social"

// Fostodon
"fosstodon.org"

// GitHub
"github.com"
"*.github.io"

// Instagram
"instagram.com"
"www.instagram.com"

// LinkedIn
"linkedin.com"
"www.linkedin.com"

// Telegram
"t.me"

// Twitter
"twitter.com"

// YouTube
"youtube.com"
"www.youtube.com"

When you pass a URL like “https://github.com/hjr265” to slinky.Parse, Slinky will parse the URL into this:

1
2
3
4
5
6
7
8
&URL{
  Service: slinky.GitHub,
  Type:    "User",
  ID:      "hjr265",
  Data:    map[string]string{,
    "username": "hjr265",
  },
}

In the case of a floss.social or a fosstodon.org URL, you will get a URL value like this:

1
2
3
4
5
6
7
8
9
&URL{
  Service: slinky.Fosstodon,
  Type:    "Profile",
  ID:      "hjr265",
  Data:    map[string]string{,
    "username": "hjr265",
    "platform": "Mastodon",
  },
}

If you need to parse social media URLs in your Go application, try Slinky.

And, if you want to extend Slinky to support additional social media platforms, feel free to open an issue with details or send a pull request.


This post is 65th of my #100DaysToOffload challenge. Want to get involved? Find out more at 100daystooffload.com.


comments powered by Disqus