mirror of
https://github.com/sysadminsmedia/homebox.git
synced 2025-12-21 21:33:02 +01:00
Update error handling for Currencies sync, if already synced then just skip.
This commit is contained in:
25
.github/scripts/update_currencies.py
vendored
25
.github/scripts/update_currencies.py
vendored
@@ -5,7 +5,7 @@ import os
|
|||||||
def fetch_currencies():
|
def fetch_currencies():
|
||||||
try:
|
try:
|
||||||
response = requests.get('https://restcountries.com/v3.1/all')
|
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:
|
except requests.exceptions.Timeout:
|
||||||
print("Request to the API timed out.")
|
print("Request to the API timed out.")
|
||||||
return []
|
return []
|
||||||
@@ -14,7 +14,7 @@ def fetch_currencies():
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
try:
|
try:
|
||||||
countries = response.json() # Attempt to parse the JSON response
|
countries = response.json()
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
print("Failed to decode JSON from the response.")
|
print("Failed to decode JSON from the response.")
|
||||||
return []
|
return []
|
||||||
@@ -36,17 +36,30 @@ def fetch_currencies():
|
|||||||
|
|
||||||
def save_currencies(currencies, file_path):
|
def save_currencies(currencies, file_path):
|
||||||
try:
|
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:
|
with open(file_path, 'w', encoding='utf-8') as f:
|
||||||
json.dump(currencies, f, ensure_ascii=False, indent=4)
|
json.dump(currencies, f, ensure_ascii=False, indent=4)
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
print(f"An error occurred while writing to the file: {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():
|
def main():
|
||||||
currencies = fetch_currencies()
|
|
||||||
if currencies: # Check if currencies were successfully fetched
|
|
||||||
save_path = 'backend/internal/core/currencies/currencies.json'
|
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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
20
.github/workflows/update-currencies.yml
vendored
20
.github/workflows/update-currencies.yml
vendored
@@ -4,34 +4,38 @@ on:
|
|||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
update-currencies:
|
update-currencies:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout code
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v2
|
uses: actions/setup-python@v2
|
||||||
with:
|
with:
|
||||||
python-version: '3.x'
|
python-version: '3.8'
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
python -m pip install requests
|
python -m pip install --upgrade pip
|
||||||
|
pip install requests
|
||||||
|
|
||||||
- name: Fetch and update currencies
|
- name: Fetch and update currencies
|
||||||
run: python .github/scripts/update_currencies.py
|
run: python .github/scripts/update_currencies.py
|
||||||
|
|
||||||
- name: Commit and push changes
|
- name: Check for changes, commit and push changes if needed
|
||||||
run: |
|
run: |
|
||||||
|
if [[ $(git status --porcelain) ]]; then
|
||||||
|
echo "Changes detected, committing..."
|
||||||
git config --global user.name "github-actions[bot]"
|
git config --global user.name "github-actions[bot]"
|
||||||
git config --global user.email "github-actions[bot]@users.noreply.github.com"
|
git config --global user.email "github-actions[bot]@users.noreply.github.com"
|
||||||
git add backend/internal/core/currencies/currencies.json
|
git add backend/internal/core/currencies/currencies.json
|
||||||
git commit -m "Update currencies JSON"
|
git commit -m "Update currencies.json"
|
||||||
git push
|
git push
|
||||||
env:
|
else
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
echo "Currencies up-to-date with API, skipping commit."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
Reference in New Issue
Block a user