Update error handling for Currencies sync, if already synced then just skip.

This commit is contained in:
Katos
2024-07-14 16:25:41 +01:00
committed by GitHub
parent 1b93672417
commit bb9672214c
2 changed files with 37 additions and 20 deletions

View File

@@ -5,7 +5,7 @@ import os
def fetch_currencies():
try:
response = requests.get('https://restcountries.com/v3.1/all')
response.raise_for_status() # Raise an error for HTTP errors
response.raise_for_status()
except requests.exceptions.Timeout:
print("Request to the API timed out.")
return []
@@ -14,7 +14,7 @@ def fetch_currencies():
return []
try:
countries = response.json() # Attempt to parse the JSON response
countries = response.json()
except json.JSONDecodeError:
print("Failed to decode JSON from the response.")
return []
@@ -36,17 +36,30 @@ def fetch_currencies():
def save_currencies(currencies, file_path):
try:
os.makedirs(os.path.dirname(file_path), exist_ok=True) # Create directories if they don't exist
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(currencies, f, ensure_ascii=False, indent=4)
except IOError as e:
print(f"An error occurred while writing to the file: {e}")
def load_existing_currencies(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as f:
return json.load(f)
except (IOError, json.JSONDecodeError):
return [] # Return an empty list if file doesn't exist or is invalid
def main():
currencies = fetch_currencies()
if currencies: # Check if currencies were successfully fetched
save_path = 'backend/internal/core/currencies/currencies.json'
save_currencies(currencies, save_path)
existing_currencies = load_existing_currencies(save_path)
new_currencies = fetch_currencies()
if new_currencies == existing_currencies:
print("Currencies up-to-date with API, skipping commit.")
else:
save_currencies(new_currencies, save_path)
print("Currencies updated and saved.")
if __name__ == "__main__":
main()

View File

@@ -4,34 +4,38 @@ on:
push:
branches:
- main
workflow_dispatch:
jobs:
update-currencies:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
python-version: '3.8'
- name: Install dependencies
run: |
python -m pip install requests
python -m pip install --upgrade pip
pip install requests
- name: Fetch and update currencies
run: python .github/scripts/update_currencies.py
- name: Commit and push changes
- name: Check for changes, commit and push changes if needed
run: |
if [[ $(git status --porcelain) ]]; then
echo "Changes detected, committing..."
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add backend/internal/core/currencies/currencies.json
git commit -m "Update currencies JSON"
git commit -m "Update currencies.json"
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
else
echo "Currencies up-to-date with API, skipping commit."
exit 0
fi