Add helper scripts for getting exchange rates
This commit is contained in:
parent
9d16bc2546
commit
eeaa02bada
33
contrib/scripts/get_exchange_rates_since_1990.py
Normal file
33
contrib/scripts/get_exchange_rates_since_1990.py
Normal file
@ -0,0 +1,33 @@
|
||||
from datetime import datetime
|
||||
|
||||
import requests
|
||||
|
||||
url = "https://data.kurzy.cz/json/meny/b[6]den[{0}].json"
|
||||
date_format = "%Y%m%d"
|
||||
years = range(2000, datetime.now().year + 1)
|
||||
dates = [
|
||||
datetime.strftime(datetime(day=1, month=1, year=year), format=date_format)
|
||||
for year in years
|
||||
]
|
||||
for date in dates:
|
||||
final_url = url.format(date)
|
||||
year = date[:4]
|
||||
response = requests.get(final_url)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
if kurzy := data.get("kurzy"):
|
||||
with open("output.yaml", mode="a") as o:
|
||||
rates = [
|
||||
f"""
|
||||
- model: games.exchangerate
|
||||
fields:
|
||||
currency_from: {currency_name}
|
||||
currency_to: CZK
|
||||
year: {year}
|
||||
rate: {kurzy.get(currency_name, {}).get("dev_stred", 0)}
|
||||
"""
|
||||
for currency_name in ["EUR", "USD", "CNY"]
|
||||
if kurzy.get(currency_name)
|
||||
]
|
||||
o.writelines(rates)
|
||||
# time.sleep(0.5)
|
65
contrib/scripts/merge_exchange_records.py
Normal file
65
contrib/scripts/merge_exchange_records.py
Normal file
@ -0,0 +1,65 @@
|
||||
import sys
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
def load_yaml(filename):
|
||||
with open(filename, "r", encoding="utf-8") as file:
|
||||
return yaml.safe_load(file) or []
|
||||
|
||||
|
||||
def save_yaml(filename, data):
|
||||
with open(filename, "w", encoding="utf-8") as file:
|
||||
yaml.safe_dump(data, file, allow_unicode=True, default_flow_style=False)
|
||||
|
||||
|
||||
def extract_existing_combinations(data):
|
||||
return {
|
||||
(
|
||||
entry["fields"]["currency_from"],
|
||||
entry["fields"]["currency_to"],
|
||||
entry["fields"]["year"],
|
||||
)
|
||||
for entry in data
|
||||
if entry["model"] == "games.exchangerate"
|
||||
}
|
||||
|
||||
|
||||
def filter_new_entries(existing_combinations, additional_files):
|
||||
new_entries = []
|
||||
|
||||
for filename in additional_files:
|
||||
data = load_yaml(filename)
|
||||
for entry in data:
|
||||
if entry["model"] == "games.exchangerate":
|
||||
key = (
|
||||
entry["fields"]["currency_from"],
|
||||
entry["fields"]["currency_to"],
|
||||
entry["fields"]["year"],
|
||||
)
|
||||
if key not in existing_combinations:
|
||||
new_entries.append(entry)
|
||||
|
||||
return new_entries
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 3:
|
||||
print("Usage: script.py example.yaml additions1.yaml [additions2.yaml ...]")
|
||||
sys.exit(1)
|
||||
|
||||
example_file = sys.argv[1]
|
||||
additional_files = sys.argv[2:]
|
||||
output_file = "filtered_output.yaml"
|
||||
|
||||
existing_data = load_yaml(example_file)
|
||||
existing_combinations = extract_existing_combinations(existing_data)
|
||||
|
||||
new_entries = filter_new_entries(existing_combinations, additional_files)
|
||||
|
||||
save_yaml(output_file, new_entries)
|
||||
print(f"Filtered data saved to {output_file}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
x
Reference in New Issue
Block a user