more testing
Browse files- app.py +21 -1
- metals_price.py +4 -2
app.py
CHANGED
|
@@ -336,6 +336,26 @@ def get_announcements_scrap():
|
|
| 336 |
return announcements
|
| 337 |
|
| 338 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
def get_circuit_breakers():
|
| 340 |
r = requests.get("https://dps.psx.com.pk/circuit-breakers")
|
| 341 |
|
|
@@ -432,4 +452,4 @@ def get_gold_historial_price():
|
|
| 432 |
start = int((datetime.utcnow() - timedelta(days=730)).timestamp())
|
| 433 |
|
| 434 |
data = metals_api.get_price_history("XAU", start, end)
|
| 435 |
-
return data
|
|
|
|
| 336 |
return announcements
|
| 337 |
|
| 338 |
|
| 339 |
+
def normalize_gold_prices(data: list):
|
| 340 |
+
cleaned = []
|
| 341 |
+
|
| 342 |
+
for item in data:
|
| 343 |
+
try:
|
| 344 |
+
cleaned.append({
|
| 345 |
+
"price": float(item["avg_price"]),
|
| 346 |
+
"date": datetime.strptime(
|
| 347 |
+
item["day"], "%Y-%m-%d %H:%M:%S"
|
| 348 |
+
).date().isoformat()
|
| 349 |
+
})
|
| 350 |
+
except Exception:
|
| 351 |
+
continue # skip bad rows safely
|
| 352 |
+
|
| 353 |
+
# VERY IMPORTANT for charts
|
| 354 |
+
cleaned.sort(key=lambda x: x["date"])
|
| 355 |
+
|
| 356 |
+
return cleaned
|
| 357 |
+
|
| 358 |
+
|
| 359 |
def get_circuit_breakers():
|
| 360 |
r = requests.get("https://dps.psx.com.pk/circuit-breakers")
|
| 361 |
|
|
|
|
| 452 |
start = int((datetime.utcnow() - timedelta(days=730)).timestamp())
|
| 453 |
|
| 454 |
data = metals_api.get_price_history("XAU", start, end)
|
| 455 |
+
return normalize_gold_prices(data=data)
|
metals_price.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import requests
|
| 2 |
from datetime import datetime, timedelta
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
api_key_metal = os.getenv("metail_api")
|
| 6 |
|
|
@@ -18,7 +20,7 @@ class MetalsPrice: # Fixed typo: "Metails" -> "Metals"
|
|
| 18 |
group_by: str = "day",
|
| 19 |
aggregation: str = "avg",
|
| 20 |
order_by: str = "desc"
|
| 21 |
-
):
|
| 22 |
"""
|
| 23 |
Fetch price history for a given asset from Gold API.
|
| 24 |
"""
|
|
@@ -52,7 +54,7 @@ class MetalsPrice: # Fixed typo: "Metails" -> "Metals"
|
|
| 52 |
except requests.exceptions.RequestException as err:
|
| 53 |
print(f"Request failed: {err}")
|
| 54 |
|
| 55 |
-
return
|
| 56 |
|
| 57 |
|
| 58 |
# # ✅ Example Usage
|
|
|
|
| 1 |
import requests
|
| 2 |
from datetime import datetime, timedelta
|
| 3 |
import os
|
| 4 |
+
from typing import List, Dict, Any
|
| 5 |
+
|
| 6 |
|
| 7 |
api_key_metal = os.getenv("metail_api")
|
| 8 |
|
|
|
|
| 20 |
group_by: str = "day",
|
| 21 |
aggregation: str = "avg",
|
| 22 |
order_by: str = "desc"
|
| 23 |
+
) -> List[Dict[str, Any]]:
|
| 24 |
"""
|
| 25 |
Fetch price history for a given asset from Gold API.
|
| 26 |
"""
|
|
|
|
| 54 |
except requests.exceptions.RequestException as err:
|
| 55 |
print(f"Request failed: {err}")
|
| 56 |
|
| 57 |
+
return []
|
| 58 |
|
| 59 |
|
| 60 |
# # ✅ Example Usage
|