One of the many benefits of the fediverse is the variety of instances that users can create across different domains, giving inherent independence to the network as it’s spread across many nodes instead of one centralized entity.
However, in terms of linking across the fediverse, that means every URL can potentially have a different host
— not all Mastodon links are on mastodon.social, even if your account is located there.
That means, if you’re working with those URLs, you need to prepare for links coming from any specific instance, not just your own – expecting mastodon.social doesn’t cut it.
Plus, when clicking on a link separate from your own instance, the URL is formed with your own instance as the base, but the handle/other instance at the end — an odd behavior to work with at first.
Thankfully, the solution can be amid the components found Mastodon’s URL pattern – and you can use Shortcuts to extract those individual details as needed. Here’s how:
How to get a Mastodon Instance from the Host
In Shortcuts, one can work around URL oddities by using the action Get Component of URL
action, a powerful tool for automatically isolating various components of the URL like the host
automatically.
By passing a Mastodon link into the action, Shortcuts will look for the host
and return the string of text for your instance
right away.
How to get a Mastodon handle from the Path
Even further, you can isolate a Mastodon account’s username by looking at the path
of that link, separating their name out from the rest of the link.
For profile links, the handle can be quickly retrieved from the path
component of the URL by splitting along the /
and using Get Item from List
to get the Last Item
.
But for status links, however, the URLs end with /{status_id}
and that data needs to be removed from the path
as well if we just want the handle data. Unfortunately, splitting by @
or /@
doesn’t work, since we still end up with the ID at the end.
To account for statuses, we can split the path
by /
just like profile links above, but this time it will return three items — a blank item that represents all the removed components before the first slash, everything at index 2—which is our handle data—and everything in index 3 that represents the status id
.
By using Get Item From List
set to Index
and 2
, the shortcut can look only for the second item in between each /
in the path
and save that as the handle.
Put all together, Shortcuts can take a Mastodon link, separate out the instance using the host
component of the URL, and then grab the handle by splitting along the path
, getting rid of the ID, and isolating the account handle too.
Using this as a Function in Shortcuts
On its own, this methodology is just that — a methodology and not an actual shortcut that has utility on its own.
For folks working with the Mastodon API, these steps can be recreated in any shortcut – you can pull apart Mastodon links, reformat them on the same or different instances, or simply save the separated data like the account username quickly for other purposes.
For example, in any Mastodon API shortcut, this method can be used to store your preferred instance and use it for the base URL of the Mastodon API.
For my Mastodon Lists shortcut, this method is used to start from an incoming Mastodon link from the Share sheet or some other form of input, pull instance, and open the link to my List.
Going further, I have another shortcut for opening into Lists in Ivory that reforms the Mastodon link using both the instance and handle to deep link using Ivory’s URL scheme (the original reason I was prompted to build this method).
For shortcuts like Add account to Mastodon lists, being able to properly isolate the full URL for a link on another Mastodon instance is also critical to make sure it actually functions properly — Mastodon’s API fails with simple usernames if that link comes from somewhere other than your instance.
—
Get the sample shortcut here and see the full Mastodon Lists folder on the Shortcuts Library.
2 replies on “How to extract Mastodon instance & handle data using Shortcuts”
[…] start, my shortcut builds off the method I’ve developed for scraping Mastodon instance and handle data from fediverse links — you’ll be able to take a link from input and end up with variables for the Instance and […]
[…] […]