74 lines
2.3 KiB
Python
74 lines
2.3 KiB
Python
import json
|
|
import os
|
|
import urllib.request
|
|
from pathlib import Path
|
|
|
|
|
|
API_URL = os.environ.get(
|
|
"API_URL", "https://statista.steelforesight.ir/api/v1/prices/latest"
|
|
)
|
|
API_TOKEN = os.environ["API_TOKEN"]
|
|
|
|
|
|
def names(rows, *keys):
|
|
out = []
|
|
for row in rows or []:
|
|
for key in keys:
|
|
value = row.get(key)
|
|
if value:
|
|
out.append(str(value))
|
|
break
|
|
return out
|
|
|
|
|
|
req = urllib.request.Request(
|
|
API_URL,
|
|
headers={"Authorization": f"Bearer {API_TOKEN}"},
|
|
)
|
|
with urllib.request.urlopen(req, timeout=30) as response:
|
|
payload = json.load(response)
|
|
|
|
datasets = payload.get("datasets", {})
|
|
|
|
catalog = {
|
|
"generated_at": payload.get("generated_at"),
|
|
"metals": names(datasets.get("metals"), "title"),
|
|
"live_commodities": names(datasets.get("live_commodities"), "name"),
|
|
"tgju_currency": names(datasets.get("currency"), "name"),
|
|
"tgju_gold": names(datasets.get("gold"), "name"),
|
|
"tgju_coin": names(datasets.get("coin"), "name"),
|
|
"yahoo_finance_commodities": names(datasets.get("commodity"), "name", "symbol"),
|
|
"companies_market_cap_steel_stocks": names(datasets.get("steel_stocks"), "name", "code"),
|
|
"coingecko_crypto": names((datasets.get("crypto") or {}).get("coins"), "name", "symbol"),
|
|
"coingecko_trending": names((datasets.get("crypto") or {}).get("trending"), "name", "symbol"),
|
|
"world_economy": names(datasets.get("world_economy"), "country"),
|
|
}
|
|
|
|
summary = {key: len(value) for key, value in catalog.items() if isinstance(value, list)}
|
|
|
|
Path("LIVE-DATA-CATALOG.json").write_text(
|
|
json.dumps({"summary": summary, **catalog}, ensure_ascii=False, indent=2),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
lines = [
|
|
"# Live API Data Catalog",
|
|
"",
|
|
f"Generated at: {catalog['generated_at']}",
|
|
"",
|
|
"## Summary",
|
|
"",
|
|
]
|
|
for key, count in summary.items():
|
|
lines.append(f"- {key}: {count}")
|
|
|
|
for key, values in catalog.items():
|
|
if not isinstance(values, list):
|
|
continue
|
|
lines += ["", f"## {key} ({len(values)})", ""]
|
|
lines += [f"- {value}" for value in values]
|
|
|
|
Path("LIVE-DATA-CATALOG.md").write_text("\n".join(lines) + "\n", encoding="utf-8")
|
|
|
|
print(json.dumps(summary, ensure_ascii=False, indent=2))
|