DEV Community

Rodney Gitonga
Rodney Gitonga

Posted on

How I Built a Daily Streak Keeper with Python & GitHub Actions

A guide on automating GitHub activity to keep your contribution streak alive using Python and GitHub Actions.

๐Ÿš€ How I Built a Daily Streak Keeper with Python & GitHub Actions

We've all been thereโ€”staring at that one gray square in our GitHub contribution graph that broke a perfect streak. Whether you're busy, sick, or just forgot to push code, losing a streak can be demotivating.

I decided to solve this problem by automating a daily commit using Python and GitHub Actions. Here is how I built my Daily Streak Keeper.

๐Ÿ’ก The Idea

The goal was simple:

  1. Fetch a daily quote (to make the commit meaningful).
  2. Update the README.md with the new quote.
  3. Commit and push the changes automatically every day.

๐Ÿ› ๏ธ The Tech Stack

  • Python: To fetch the quote and update the file.
  • GitHub Actions: To schedule the script to run daily.

๐Ÿ’ป The Code

  1. The Python Script (update_quote.py)

I wrote a simple Python script that fetches a random quote from an API and updates the README.md file. I used dummyjson.com for stability.

import requests
import datetime

def get_quote():
    url = "https://dummyjson.com/quotes/random"
    try:
        response = requests.get(url)
        if response.status_code == 200:
            data = response.json()
            return f"\"{data['quote']}\" โ€” {data['author']}"
        else:
            return "Could not fetch a quote today. Keep coding!"
    except Exception as e:
        return f"Error fetching quote: {e}"

def update_readme(quote):
    date_str = datetime.datetime.now().strftime("%Y-%m-%d")

     Template for the README
    readme_content = f"""
 ๐Ÿš€ Daily Streak Keeper

This repository automatically updates itself every day at 12:00 PM Nairobi Time to keep my GitHub contribution streak alive.

 ๐Ÿ“… Quote for {date_str}

> {quote}

---
Last updated automatically by GitHub Actions.
"""

    with open("README.md", "w", encoding="utf-8") as file:
        file.write(readme_content)

if __name__ == "__main__":
    quote = get_quote()
    update_readme(quote)
Enter fullscreen mode Exit fullscreen mode
  1. The Automation (daily-quote.yml)

Next, I needed this script to run automatically. GitHub Actions is perfect for this. I set up a workflow that runs every day at 12:00 PM Nairobi Time (09:00 UTC).

name: Daily Quote Update

on:
  schedule:
     09:00 UTC = 12:00 PM Nairobi Time
    - cron: '0 9   '
  workflow_dispatch:

jobs:
  update-readme:
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.x'

      - name: Install dependencies
        run: |
          pip install -r requirements.txt

      - name: Run Python script
        run: |
          python update_quote.py

      - name: Commit and Push
        run: |
          git config --global user.name "Quote Bot"
          git config --global user.email "${{ github.actor }}@users.noreply.github.com"

          git add README.md
          if git diff-index --quiet HEAD; then
            echo "No changes to commit"
          else
            git commit -m "Daily Quote: Updated README with new inspiration ๐Ÿ“œ"
            git push
          fi
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Conclusion

This was a fun little project to ensure my profile stays active while also giving me a nice quote to read every day. Itโ€™s a great example of how powerful and easy GitHub Actions can be for automating simple tasks.

Check out the code in the repository and feel free to fork it to create your own streak keeper! REPO

Top comments (0)