From 967e574ea8eafc107cc009973208343d6b2b8e89 Mon Sep 17 00:00:00 2001 From: Katos <7927609+katosdev@users.noreply.github.com> Date: Sun, 14 Jul 2024 15:52:25 +0100 Subject: [PATCH 1/2] Create a Github Workflow to keep currencies in sync from API --- .github/scripts/update_currencies.py | 37 +++++++++++++++++++++++++ .github/workflows/update-currencies.yml | 37 +++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 .github/scripts/update_currencies.py create mode 100644 .github/workflows/update-currencies.yml diff --git a/.github/scripts/update_currencies.py b/.github/scripts/update_currencies.py new file mode 100644 index 00000000..c7be4de9 --- /dev/null +++ b/.github/scripts/update_currencies.py @@ -0,0 +1,37 @@ +import requests +import json +import os + +def fetch_currencies(): + response = requests.get('https://restcountries.com/v3.1/all') + response.raise_for_status() + countries = response.json() + + currencies_list = [] + for country in countries: + country_name = country.get('name', {}).get('common') + country_currencies = country.get('currencies', {}) + for currency_code, currency_info in country_currencies.items(): + symbol = currency_info.get('symbol', '') + # Directly use the symbol as it is + currencies_list.append({ + 'code': currency_code, + 'local': country_name, + 'symbol': symbol, + 'name': currency_info.get('name') + }) + + return currencies_list + +def save_currencies(currencies, file_path): + 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) + +def main(): + currencies = fetch_currencies() + save_path = 'backend/internal/core/currencies/currencies.json' + save_currencies(currencies, save_path) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/.github/workflows/update-currencies.yml b/.github/workflows/update-currencies.yml new file mode 100644 index 00000000..28313143 --- /dev/null +++ b/.github/workflows/update-currencies.yml @@ -0,0 +1,37 @@ +name: Update Currencies + +on: + push: + branches: + - main + workflow_dispatch: + +jobs: + update-currencies: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + + - name: Install dependencies + run: | + python -m pip install requests + + - name: Fetch and update currencies + run: python .github/scripts/update_currencies.py + + - name: Commit and push changes + run: | + 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 push + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file From f0942f0714e2e8b72c72237ab9d93d2f5214b224 Mon Sep 17 00:00:00 2001 From: Katos <7927609+katosdev@users.noreply.github.com> Date: Sun, 14 Jul 2024 16:00:25 +0100 Subject: [PATCH 2/2] Add error handling to API pulls --- .github/scripts/update_currencies.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/scripts/update_currencies.py b/.github/scripts/update_currencies.py index c7be4de9..95ad2d9a 100644 --- a/.github/scripts/update_currencies.py +++ b/.github/scripts/update_currencies.py @@ -5,6 +5,10 @@ import os def fetch_currencies(): response = requests.get('https://restcountries.com/v3.1/all') response.raise_for_status() + except requests.RequestException as e: + print(f"An error occurred: {e}") + return [] + countries = response.json() currencies_list = []