From 722e937e1405367f5d915d7a252bd24491d151b8 Mon Sep 17 00:00:00 2001 From: johc Date: Wed, 27 Mar 2024 13:00:35 +0100 Subject: [PATCH] Added fetch data workflow --- .github/workflows/fetch_data.yml | 37 ++++++++++++++++++++++++++++++++ fetch_data.py | 15 +++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 .github/workflows/fetch_data.yml create mode 100644 fetch_data.py diff --git a/.github/workflows/fetch_data.yml b/.github/workflows/fetch_data.yml new file mode 100644 index 0000000..a365be7 --- /dev/null +++ b/.github/workflows/fetch_data.yml @@ -0,0 +1,37 @@ +name: Fetch MongoDB Data + +on: + schedule: + - cron: '0 0 * * 0' # This sets it to run weekly on Sunday at midnight + +jobs: + fetch_data: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + + - name: Install MongoDB client + run: | + python -m pip install --upgrade pip + pip install pymongo + + - name: Fetch Data from MongoDB + env: + MONGODB_URI: ${{ secrets.MONGODB_URI }} + run: | + python fetch_data.py + + # Commit and Push changes + - 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 commit -m "Update data files" || true + git push origin gh-pages diff --git a/fetch_data.py b/fetch_data.py new file mode 100644 index 0000000..aa0e92e --- /dev/null +++ b/fetch_data.py @@ -0,0 +1,15 @@ +from pymongo import MongoClient +import json +import os + +# Connect to MongoDB +client = MongoClient(os.environ['MONGODB_URI']) +db = client.your_database_name # Replace with your database name + +# Fetch data from a collection +collection = db.your_collection_name # Replace with your collection name +data = list(collection.find({})) + +# Save to a JSON file +with open('data.json', 'w') as file: + json.dump(data, file)