mirror of
https://github.com/sysadminsmedia/homebox.git
synced 2025-12-24 06:28:34 +01:00
Update update_currencies.py to fix error handling
This commit is contained in:
37
.github/scripts/update_currencies.py
vendored
37
.github/scripts/update_currencies.py
vendored
@@ -3,13 +3,21 @@ import json
|
||||
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}")
|
||||
try:
|
||||
response = requests.get('https://restcountries.com/v3.1/all')
|
||||
response.raise_for_status() # Raise an error for HTTP errors
|
||||
except requests.exceptions.Timeout:
|
||||
print("Request to the API timed out.")
|
||||
return []
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"An error occurred while making the request: {e}")
|
||||
return []
|
||||
|
||||
try:
|
||||
countries = response.json() # Attempt to parse the JSON response
|
||||
except json.JSONDecodeError:
|
||||
print("Failed to decode JSON from the response.")
|
||||
return []
|
||||
|
||||
countries = response.json()
|
||||
|
||||
currencies_list = []
|
||||
for country in countries:
|
||||
@@ -17,7 +25,6 @@ def fetch_currencies():
|
||||
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,
|
||||
@@ -28,14 +35,18 @@ def fetch_currencies():
|
||||
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)
|
||||
try:
|
||||
os.makedirs(os.path.dirname(file_path), exist_ok=True) # Create directories if they don't exist
|
||||
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 main():
|
||||
currencies = fetch_currencies()
|
||||
save_path = 'backend/internal/core/currencies/currencies.json'
|
||||
save_currencies(currencies, save_path)
|
||||
if currencies: # Check if currencies were successfully fetched
|
||||
save_path = 'backend/internal/core/currencies/currencies.json'
|
||||
save_currencies(currencies, save_path)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user