224 lines
15 KiB
Plaintext
224 lines
15 KiB
Plaintext
|
|
Episode: 4272
|
||
|
|
Title: HPR4272: Embed Mastodon Threads
|
||
|
|
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr4272/hpr4272.mp3
|
||
|
|
Transcribed: 2025-10-25 22:20:54
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
This is hacker public radio episode 4272 for Tuesday the 17th of December 2024.
|
||
|
|
Today's show is entitled Embed Mastodon Threads.
|
||
|
|
It is part of the series programming 101.
|
||
|
|
It is hosted by Harry Larry and is about 17 minutes long.
|
||
|
|
Harry's a clean flag.
|
||
|
|
The summary is I'm reconstructing the development process writing Embed Mastodon Threads.
|
||
|
|
Today's show is licensed under a Creative Commons Attribution License.
|
||
|
|
Episode 4, Embed Mastodon Threads.
|
||
|
|
This is Episode 4 of the Plaintex Programs Podcast hosted at Hacker Public Radio.
|
||
|
|
As always, I will include links with the show notes rather than reading them on the podcast,
|
||
|
|
except there will be one exception to that today.
|
||
|
|
The link to my Plaintex blog home.gamerplus.org.
|
||
|
|
My blog and this podcast were my inspiration for writing the Embed Mastodon Threads program.
|
||
|
|
Besides posting the show notes at Hacker Public Radio where they have a comment section,
|
||
|
|
I also post them at my blog.
|
||
|
|
Then I make a Mastodon post that includes a link to the show notes on my blog
|
||
|
|
and designated as being the comment thread for that episode of the podcast.
|
||
|
|
I also post a link to the comment thread on Mastodon in my show notes.
|
||
|
|
Or at least I did that in the past.
|
||
|
|
It came to mind that it would be nice to be able to display the comment thread
|
||
|
|
at the bottom of the blog post.
|
||
|
|
So I made a Mastodon post about this and I quote.
|
||
|
|
Here's my idea.
|
||
|
|
I want to use Mastodon Tutes as a comment thread for my blog posts.
|
||
|
|
At the bottom of the blog post, I want to embed the Tut and the replies.
|
||
|
|
I can pull the Tut ID from the embed code.
|
||
|
|
Then I want to make a database query to get all the replies to that Tut.
|
||
|
|
Then I can generate the embed codes needed to show the Tut and all the replies.
|
||
|
|
I'm a MySQL guy, not post GRES.
|
||
|
|
Also a Mastodon Noob.
|
||
|
|
I want to know how to get the reply IDs for a Tut.
|
||
|
|
Any help, links, etc. and quote.
|
||
|
|
I immediately got responses from some programmers expressing interest in the idea
|
||
|
|
and giving good advice.
|
||
|
|
I did some research based on their suggestions.
|
||
|
|
I had a good night's sleep and then I made another post in the morning and I quote.
|
||
|
|
Mastodon is so great.
|
||
|
|
I had this idea last night and fiddled around with it long enough to realize I was doing it wrong.
|
||
|
|
So I made a post on Mastodon and almost immediately got help.
|
||
|
|
I found some good info on the Mastodon API.
|
||
|
|
I wake up this morning to more help and I found out about using Coral in PHP to make HTTPS requests.
|
||
|
|
Then a musician friend of mine who I've been following since before Mastodon
|
||
|
|
sends a working example with code in a JavaScript environment.
|
||
|
|
And I've got a plan and quote.
|
||
|
|
So credit where credit is due.
|
||
|
|
The programmers, gamers and musicians helping me work.
|
||
|
|
Jeff, the Gen X alien, XD, Malin and Wayne Myers.
|
||
|
|
Now I've known Wayne Myers since before I was ever on Mastodon.
|
||
|
|
We shared an interest in free culture music and I have played his songs on my radio show,
|
||
|
|
Something Blue, recorded by his band, fit in the connections.
|
||
|
|
He sent some links in a couple of comments to other blogs that were embedding Mastodon threats
|
||
|
|
which confirmed that my idea could work.
|
||
|
|
Jeff, the Gen X alien gave me some significant technical help and I quote.
|
||
|
|
Use TOOT CLI to learn everything you need to know about the inner workings of Mastodon.
|
||
|
|
Whatever the API supports, so does TOOT, and quote.
|
||
|
|
So I looked into TOOT CLI and the Mastodon API.
|
||
|
|
And I realized that I didn't need to access the database for my program.
|
||
|
|
I could just use the API.
|
||
|
|
So thanks, Jeff.
|
||
|
|
My second clue came from XD and I quote.
|
||
|
|
I've got some help too.
|
||
|
|
Using the Mastodon API and CURL and PHP, it should be doable.
|
||
|
|
End quote.
|
||
|
|
So then I started to research using CURL and PHP to retrieve JSON data from the Mastodon API.
|
||
|
|
And that's what I went with.
|
||
|
|
I set up a test bed and Malin chimed in with some test results.
|
||
|
|
He continued to help with testing and ideas throughout the rest of the project.
|
||
|
|
That's why Mastodon is so great, way better than consulting an AI bot.
|
||
|
|
So I had my work cut out for me.
|
||
|
|
Here's where this program is like by plain text programs.
|
||
|
|
I work hard up front until I'm convinced that I have an idea that will be easy to implement.
|
||
|
|
This is much easier than doing it the hard way first and then rewriting the program later
|
||
|
|
after it becomes difficult to maintain.
|
||
|
|
I said I had a plan.
|
||
|
|
This was my plan.
|
||
|
|
Write a PHP program that will generate a web page that can be embedded in an iframe.
|
||
|
|
This program will take a link as a parameter included in the URL.
|
||
|
|
Get that link from the Mastodon embed code for the parent post.
|
||
|
|
Use the API to retrieve the data associated with the parent post, including the replies.
|
||
|
|
Then generate the page by inserting the appropriate data into Mastodon's existing embed structure.
|
||
|
|
That's kind of a broad framework, but it certainly seemed doable and it was.
|
||
|
|
So first I wanted to make the API call so I could look at the data.
|
||
|
|
I found this video by Alejandro A.O.
|
||
|
|
How to easily create curl API requests in PHP.
|
||
|
|
He recommended that you use curl in the terminal to test your API call.
|
||
|
|
Then you use a web app called Curl the PHP to generate your PHP code to make the same API call from your program.
|
||
|
|
The first time consuming stubborn block was what I call the problem with the colon.
|
||
|
|
There are some great documents detailing the syntax for API calls which I will link to in the show notes.
|
||
|
|
And where you are supposed to insert an ID, they show that as colon ID.
|
||
|
|
Like an idiot, I thought the colon was part of the syntax, not as they intended, a marker to indicate insert your ID here.
|
||
|
|
This is why I like to see actual code examples in syntax documents.
|
||
|
|
Anyway, I couldn't get it to work so I searched around until I found some code examples and that turned on the light bulb in my head.
|
||
|
|
Now I was able to make API calls using curl in the terminal.
|
||
|
|
I copied the work in curl command and pasted it into the Curl to PHP website and it outputs in code and it worked.
|
||
|
|
Which I was very glad about because previous research into how to make API calls with PHP was confusing to say the least.
|
||
|
|
Sometimes PHP gifts you within abundance of riches which doesn't always make life easier.
|
||
|
|
So I made my API call from my program, the curl to PHP code returned dollar result.
|
||
|
|
And then I used the json decode command to turn the result string into an array of master done data.
|
||
|
|
Dollar OBJ equal json decode parentheses dollar result comma true close parentheses.
|
||
|
|
And I could use the print our command to look at the data print our parentheses dollar OBJ close parentheses.
|
||
|
|
I immediately put the print our command at the bottom of my program work resides today as coming it out debug code.
|
||
|
|
This way while I was looking at my program output, I could just scroll down or search to find what the actual data look like.
|
||
|
|
So I fumbled around for a while before I figured out that I would need the ID and the URL to make my idea work.
|
||
|
|
Accessing json data is reading an array so easy peasy or maybe not.
|
||
|
|
This code returns the ID of the reply from inside a wall loop where dollar I is the index.
|
||
|
|
Dollar ID equal dollar OBJ score bracket single quote descendant single quote close score brackets.
|
||
|
|
Score bracket dollar I close score bracket score bracket single quote ID single quote score bracket semicolon.
|
||
|
|
Like I said, it looks easy now.
|
||
|
|
Needless to say, it took some head scratching to figure out the exact syntax.
|
||
|
|
I used to be a Mason and people would always ask me how I learned a Masonry.
|
||
|
|
I'd look them in the I and say travel and error.
|
||
|
|
There was in fact a lot of travel and error going on.
|
||
|
|
So then I generated the embed code to display each post and it worked for all of my posts, not for replies from other servers.
|
||
|
|
So I scroll down and examine the json data and I found the URL field that had all the info about the replies server username and ID.
|
||
|
|
So I picked up the URL field the same way I picked up the ID field and updated my code with the URL server and name.
|
||
|
|
This still didn't work.
|
||
|
|
After staring at the json data for a while, the light finally dawned.
|
||
|
|
The ID I was using was the gamer plus ID from my server.
|
||
|
|
The ID I needed to use was in the URL field from their server.
|
||
|
|
Now that I had become enlightened, it was easy to notice that the URL field contained the exact info that I needed to use in the embed.
|
||
|
|
Remember what I said about doing it the hard way before you place that code with the easy way?
|
||
|
|
That can happen even when you have a plan.
|
||
|
|
So by using the URL data in the embed, I have left string handling and fewer lines of code.
|
||
|
|
I went to bed and in the morning I made this post and I quote.
|
||
|
|
I am able to pull the URLs from the json call so that should solve the missing comments issue.
|
||
|
|
And then it comes down to the issue of data structures.
|
||
|
|
K-I-S-S.
|
||
|
|
I have decided for now to display the comments in chronological order without concern for whether a comment is a reply to the post or reply to another comment.
|
||
|
|
A chronological list rather than a tree.
|
||
|
|
Easy to implement, kind of relatively, and easy to understand.
|
||
|
|
Also, no indents.
|
||
|
|
This project will be licensed GPL so I am certainly open to others applying other data structures to the data display.
|
||
|
|
Everything you need to display a tree is in the json.
|
||
|
|
End quote.
|
||
|
|
So the data structure I needed is called a multi-dimensional array or an array of arrays.
|
||
|
|
In terms of a database table, it is two columns and a bunch of rows.
|
||
|
|
In terms of PHP arrays, it's an array where each element is an array with two values in it.
|
||
|
|
The ID and the URL.
|
||
|
|
Now in my case, the ID is from the gamer plus server.
|
||
|
|
The URL is from whatever server the reply or calls home.
|
||
|
|
I initialize the array with the parent post.
|
||
|
|
Dollar IDs equals array, parentheses, array, parentheses, dollar ID, dollar URL, close parentheses.
|
||
|
|
You can see the next to the array is in the code.
|
||
|
|
Then I add items to the array like this.
|
||
|
|
Dollar IDs, score bracket, close score bracket, equal, array, parentheses, dollar ID, dollar URL, close parentheses.
|
||
|
|
I access an array item like this.
|
||
|
|
For each dollar IDs as dollar ID, score brackets, dollar URL, equal dollar ID, score bracket, one, close score bracket.
|
||
|
|
The one refers to the second element of the array because programmers start counting at zero.
|
||
|
|
Then using the URL and the domain that I captured from the Git parameter that passes the parent URL into the program,
|
||
|
|
I build the I frame in bed for the post using the Macedon in bed as a template.
|
||
|
|
Which worked, but the post weren't displayed in chronological order because JSON data isn't necessarily in chronological order.
|
||
|
|
So I had to sort the multidimensional array on the ID, which is in this straightforward as the sort command.
|
||
|
|
So I found this article and stack overflow calls.
|
||
|
|
How do I sort a multidimensional array by one of the fields of the inner array in PHP?
|
||
|
|
It had a two line solution that I modified to work with my array.
|
||
|
|
And now all my posts were in chronological order.
|
||
|
|
Stack overflow code is license CCBY, which is one way compatible with the GPL.
|
||
|
|
Just include the attribution in the comment.
|
||
|
|
My first post quoted above was posted on Friday, October 25th at 8.40pm.
|
||
|
|
On Monday, October 28th at 8.52pm, I wrote, here's the blog post, Proof of Concept slash Working Code.
|
||
|
|
Three days from, I have an idea to working code.
|
||
|
|
That wasn't all I did in those three days.
|
||
|
|
Saturday, I had a repertoire session with my band jazz muskers.
|
||
|
|
Sunday, I produced my radio show, something blue.
|
||
|
|
But when I'm in the middle of a programming project, I get hyper focused.
|
||
|
|
Sometimes I have to force myself to step away.
|
||
|
|
And I have worked on the code a little bit today, and I will in the future too.
|
||
|
|
I did a lot of testing today and some Macedon servers and workouts just don't support embeds.
|
||
|
|
But if you want to use embed Macedon threads on your blogger website,
|
||
|
|
your two will probably be the parent.
|
||
|
|
And if it works on your account, you're good.
|
||
|
|
Also, posts from different servers look different.
|
||
|
|
Sometimes the links look different.
|
||
|
|
Sometimes the whole post is a link to that post on Macedon.
|
||
|
|
I decided to embrace that as a feature rather than a bug.
|
||
|
|
With the different look, make it easier to distinguish post made on game or plus from post made on other servers.
|
||
|
|
I have uploaded embed Macedon threads to home.gamerplus.org.
|
||
|
|
At my blog, I have a post called embed Macedon threads hosted on game or plus where I say.
|
||
|
|
The program is licensed to GPL, and I will put up a code bird repository
|
||
|
|
so that you can download it and install it wherever you want.
|
||
|
|
But feel free to use my server.
|
||
|
|
And then I go into detail about just how to do that in the embedded comments.
|
||
|
|
The program is 46 lines of code with 11 lines of comments,
|
||
|
|
including attribution comments and debug code that is commented out.
|
||
|
|
So 35 lines of code over three days.
|
||
|
|
That's 12 lines of code a day about double normal expectation for a program.
|
||
|
|
This has been a long podcast, certainly longer than most of my podcasts will be.
|
||
|
|
But I wrote it right after I did the project and it gave me an opportunity to discuss the development process.
|
||
|
|
There were many issues I had that I didn't mention, but I think I hit the high points.
|
||
|
|
Throughout the whole project, I was posting to my threads on Macedon.
|
||
|
|
So that also helped me check back on the development history of this three day project.
|
||
|
|
The streamer boosts and replies from my compatriots help keep me going too.
|
||
|
|
It was a rush.
|
||
|
|
So this is not exactly a plain text program because it uses a database access to the Macedon API.
|
||
|
|
Still, I do not have to maintain that database.
|
||
|
|
It's just there on every Macedon instance ready to use.
|
||
|
|
Most of my plain text programs are web apps or web pages.
|
||
|
|
This one is a web service and it is simple to use.
|
||
|
|
All you have to be able to do is copy the embed code from Macedon, extract the link and paste the link into the URL that calls the web service.
|
||
|
|
Then you put that URL into an iframe on your blog or web page.
|
||
|
|
I have a help page for using embed Macedon threads in the same directory as the thread.php program
|
||
|
|
where you can generate and copy your iframe code.
|
||
|
|
In fact, the help page is also a plain text program which I may talk about in a future podcast.
|
||
|
|
On the help page are instructions on how to get a link from the Macedon embed code.
|
||
|
|
Then you paste the link into a form and hit submit.
|
||
|
|
The page generates your iframe and embed code that you can use in your blog or web page.
|
||
|
|
The page also displays what the embedded thread will look like.
|
||
|
|
If you'd rather download the code and install your own instance of embed Macedon threads, I have a codeburg repository.
|
||
|
|
Again, all the links are in the show notes, add hacker public radio, and at my blog at home.gamerplus.org.
|
||
|
|
If you have questions, you can reply to a thread on Macedon or mail me at hurrylurry at deltabuggy.com.
|
||
|
|
If you don't have a Macedon account, you can get one at gamerplus.org.
|
||
|
|
You have been listening to Hacker Public Radio at Hacker Public Radio does work.
|
||
|
|
Today's show was contributed by a HPR listener like yourself.
|
||
|
|
If you ever thought of recording a podcast, you click on our contribute link to find out how easy it really is.
|
||
|
|
Hosting for HPR has been kindly provided by an honesthost.com, the internet archive and our syncs.net.
|
||
|
|
On the Sadois status, today's show is released under Creative Commons, Attribution, 4.0 International License.
|