DEV Community

Cover image for Tracking Crypto Prices on Autopilot: GitHub Codespaces & Actions Guide
Pizofreude
Pizofreude

Posted on

Tracking Crypto Prices on Autopilot: GitHub Codespaces & Actions Guide

Want to stay on top of your favorite crypto's price, but tired of manual checks? This guide empowers you to build an automated Cryptocurrency Price Tracker with GitHub Codespaces, a cloud-based development environment, and GitHub Actions, for commit automation. Let's dive in!

** Step 1: Secure Your Crypto Vault (Repository)**

  1. Head to GitHub and create a new private repository (e.g., "Cryptocurrency-Price-Tracker").
  2. Name it wisely, reflecting your crypto passion!

** Step 2: Enter the Codespace Arena**

  1. Open your new repository and click "Code" -> "Codespaces" -> "Create codespace on main".
  2. This unlocks your cloud-based development playground. Time to code!

** Step 3: Craft Your Price-Fetching Spell (Script)**

  1. In your codespace, conjure a tracker.py file.
  2. Let's use Python and CoinGecko API to extract the desired crypto's price:
import requests

def get_crypto_price(symbol):
    url = f"https://api.coingecko.com/api/v3/simple/price?ids={symbol}&vs_currencies=usd"
    response = requests.get(url)
    data = response.json()
    return data[symbol]['usd']

print(get_crypto_price('bitcoin'))
Enter fullscreen mode Exit fullscreen mode

** Step 4: Automate the Price Updates (GitHub Actions)**

  1. Click the "Actions" tab and select "New workflow".
  2. Choose the "Python application" template for streamlined setup.
  3. Replace the main.yml file with this magic formula:
name: Daily Crypto Price Update

on:
  schedule:
    - cron: '0 0 * * *' # Runs daily at midnight (UTC)

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Set up Python 3.8
        uses: actions/setup-python@v2
        with:
          python-version: 3.8

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install requests

      - name: Run the script
        run: python tracker.py

      - name: Commit and push changes
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add -A
          git diff-index --quiet HEAD || git commit -m "Update crypto price"
          git push
Enter fullscreen mode Exit fullscreen mode

** Step 5: Unleash the Automation!**

  1. Commit and push your code to the main branch.
  2. Visit the "Actions" tab and witness your workflow springing to life, updating prices daily!

Remember:

  • Adjust the script for your desired cryptocurrency.
  • Replace 'bitcoin' with its symbol.

Now you have a self-updating Cryptocurrency Price Tracker, powered by the magic of GitHub! Feel free to customize and explore further!

P.S.: A simple project to wish all of you: Happy coding!

Top comments (0)