Follow my Threads profile!
We recently launched the Threads API and my goal with this project is to 1) understand how it works and 2) inspire developers to build more useful bots for Threads.
I also take BART regularly into the office and would benefit from getting realtime updates if there are any important advisories during my commute.
I also used this opportunity to try out Cursor, the new AI code editor, to see how quickly AI can put together a simple bot. Verdict: it's pretty damn good. Except it struggles hard with parsing dates and timezones, just like humans.
fetch_and_post_bart_advisories.py
is run via a cron job on Dreamhost that is scheduled to run every 5 minutes.
fetch_advisories()
gets the latest data from the BART API, although their API is quite flaky so retries are necessary. It parses the time and advisory strings, and will only return relevant advisories that were posted within 5 minutes of the current time.
post_to_threads()
takes the advisory strings and calls the threads API to publish a post.
I also schedule refresh_access_token.py
to be a cron job that's run every month to keep the access token valid.
Clone the repository:
git clone https://github.com/0xjessel/threads-bart-bot.git
cd threads-bart-bot
Create a virtual environment:
python -m venv venv
Activate the virtual environment:
venv\Scripts\activate
source venv/bin/activate
Install the required packages:
pip install -r requirements.txt
Create a .env.local
file:
.env.example
file to create your own environment configuration:
cp .env.example .env.local
Edit the .env.local
file:
.env.local
in a text editor and fill in the required valuesRun the script
python fetch_and_post_bart_advisories.py
Run tests
python -m unittest test_fetch_advisories.py
Why is BART API so flaky. Why does BART use 24h time formatting but then also includes AM/PM. WHY?!
Writing test cases via AI is awesome.