This Python script fetches the latest top story from Hacker News and posts it to a Mastodon account. It can operate in two modes:
- Console Mode: Prints the latest top story details to the console.
- Mastodon Mode: Posts the latest top story details to a Mastodon account.
The script is designed to work in a loop, with a configurable sleep time between each query to the Hacker News API. It also supports running in a Docker container and can store the last printed story ID in a Google Cloud Storage bucket to survive container restarts.
- Python 3.x
requests
librarymastodon.py
library- Google Cloud Storage Python SDK
MASTODON_ACCESS_TOKEN
: Access token for Mastodon APIMASTODON_API_BASE_URL
: Base URL for Mastodon APIGCS_BUCKET_NAME
: Google Cloud Storage bucket name
-
Clone the repository.
-
Create a Python virtual environment:
python3 -m venv venv
-
Activate the virtual environment:
source venv/bin/activate
-
Install the required packages:
pip install -r requirements.txt
python main.py --mode console --sleep_time 300
python main.py --mode mastodon --sleep_time 300
-
Build the Docker image:
docker build -t hn-to-mastodon-bot .
-
Run the Docker container:
docker run -e MASTODON_ACCESS_TOKEN=<token> -e MASTODON_API_BASE_URL=<url> -e GCS_BUCKET_NAME=<bucket_name> hn-to-mastodon-bot
-
Deploy the function:
gcloud functions deploy hacker_news_function --runtime python39 --trigger-http --allow-unauthenticated
-
Set the environment variables in the Google Cloud Console.
The script uses a Google Cloud Storage bucket to store the ID of the last printed story. This is particularly useful in scenarios where the script is running in a Docker container and needs to survive container restarts without losing its state. By storing the last printed story ID in a GCS bucket, the script can continue from where it left off, ensuring that no stories are missed or duplicated.