Embedding tweets in markdown notes
I wanted to embed a tweet in a note. I thought, everyone does this so it must be easy.
Now it is easy if you want to manually go to twitter each time you want to embed a tweet and copy the embed code.
But I wanted to be able to use the URL so that it would be marginally quicker (and easier to read in the markdown).
I looked for an existing python markdown extension but couldn't find one. I'm sure there must be one somewhere but finding it was beyond me.
So, instead, I wrote my own 🫣
Firstly, I used this blog post about embedding a tweet in a flask blog as inspiration.
Task one was setting up a Twitter developer account - to get credentials for accessing twitter. It's easy and free. You need to create a project, an app and then generate an access token to go with the API key.
I used the tweepy library which has a get_oembed function. This function takes a twitter url and returns the HTML for embedding the tweet.
I then created a markdown extension that would match a pattern like
{{:twitter <twitter-url>}}
For example
{{:twitter https://twitter.com/ColmBritton/status/1583455055088271360}}
Which should embed one of my tweets...
{{:twitter https://twitter.com/ColmBritton/status/1583455055088271360}}
I've had to comment this out because it has stopped working... the oembed method no longer works. Apparently it is something to do with twitter (or X) making us use v2 of the api and as far as I can see there is no endpoint you can call to get the oembed code.
What a shame. Back to the drawing board.