i guess follow me @bethposting on bsky or pillowfort


discord username:
bethposting

somewhatnifty
@somewhatnifty
Anonymous User asked:

how did you... make your lyricbot....

I don't know if there's a specific part you're wondering about so I'll just go through the whole thing. It is an inspiring story about how one can create a functional bot on cohost that makes a variety of highly insightful posts while barely knowing what one is doing. I hope this is moderately helpful!


Get a bunch of lyrics

The least interesting part first. I needed to get all of the lyrics to every Mountain Goats song in a machine-readable format. There might be a way to use APIs instead but I couldn't be bothered working that out.1 Also I thought it would be neat to have an entire corpus of Mountain Goats lyrics to fuck around with.

I ended up mostly using this PDF collection of all the songs up to 2020, formatted with enough consistency that I could just copy and paste the whole thing into text files and process the result relatively easily. Then I just copied the remaining 3 albums of lyrics from the wiki into some text files.

I then used R (which is kinda my comfort zone) to process all these text files and process them into two files: one big compendium text file with all the lyrics to all songs, and another CSV file containing information about each song and where each song starts and ends in the file. Here's the first few lines:

songtitleisOuttakealbumTitlealbumYearstartLineendLine
running away with what Freud said0Taboo VI: The Homecoming1991214
ice cream, cobra man0Taboo VI: The Homecoming19911628
move (Chicago 196?)0Taboo VI: The Homecoming19913038
don't take the dogs away0Taboo VI: The Homecoming19914055

This means I immediately know where every song is in the big file. Lines 3 to 5 of ice cream, cobra man? 18 to 20! Lines 11 to 12 of Guys On Every Corner? Lines 13,741-13,742! Easy. I assure you the code to create this is hideous but who cares, I only need to run it once. (Or every time they release a new album. Thanks a lot, John.)

Get a random bunch of lyrics

Then the code reads both these files and picks a random song, random number of lyrics, and a random place in the song to start. It also uses the other columns of the table to add a line saying what song/album/year the lyrics are from. The code for this is also wildly inefficient, but who cares I only need to run it five times a day. I used the pandas python library to read in and handle the csv table, which might be overkill for this job but it made the code very similar to R so I liked it. I can share the code if anyone wants it.

Make a computer post to cohost

There are currently two unofficial ways to make a bot post - one in python and one in rust. (These are both unofficial - an official API is in the works.)

I took the Python route because I learned Python once in like 2008 and just about remembered enough of it to be able to code in it again. Once I figured out how to use 2023 Python2 the actual posting part was pretty easy! The package makers have done a lot of the heavy lifting. Here's the "post to the website" part of the code:

from cohost.models.user import User
from cohost.models.block import MarkdownBlock

# Logging in
user = User.login(ch_username, ch_password)

project = user.getProject(project_name)

# Posting
blocks = [
    MarkdownBlock(tmg_quote)
]
newPost = project.post('', blocks, tags=['tmgbot','bot','The Cohost Bot Feed'])

where ch_username and ch_password are my username and password and tmg_quote is the formatted post to quote.

Schedule your posting

Then you set up your computer to post automatically. Windows uses something called "Scheduled Tasks" but I use linux so we have a silly named program called cron that does it. You're probably best off finding a tutorial somewhere for the details, but in short it turns out you put crontab -e into the command line and write a line (or lines) with a specific code specifying what time along with the command line to run. For example, here's my machine's crontab:

16 0 * * * python /home/nifty/tmgbot/main.py 
04 5 * * * python /home/nifty/tmgbot/main.py
52 9 * * * python /home/nifty/tmgbot/main.py
40 14 * * * python /home/nifty/tmgbot/main.py
28 19 * * * python /home/nifty/tmgbot/main.py

This means that it runs the program every day at 0:16, 5:40, 9:52, 14:40 and 19:28 in my time zone.3

You will have to keep the computer on all day if you want it to run while you're asleep/away, though that might not even be necessary. See this highly scientific poll which seems to indicate that most users are fine with a bot that posts once or twice a day. Honestly you could probably just run the code manually whenever you get round to it, nobody's setting their watch to these things.


  1. A quick google shows that APIs that claim to return lyrics are available and some claim to be free if you don't use it more than 100 times a day so that might actually work. Not actually tried any of them though.

  2. On the one hand, what the hell is a pip, but on the other it's nice to see people have now generally agreed on what major version to use, most of the time. Only took a decade or so. I assume there was extensive discourse

  3. There are actually ways in cron to specify multiple times per day in one line, but sadly cron does not have a way to ask for "every 200 beats of Swatch Internet Time" so this had to do.


You must log in to comment.