Update grepodata.py
This commit is contained in:
48
grepodata.py
48
grepodata.py
@@ -1,9 +1,9 @@
|
||||
from flask import Flask, request, jsonify
|
||||
from flask_cors import CORS
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
from datetime import datetime # ✅ minimal timestamp support
|
||||
from datetime import datetime
|
||||
from goldeval import goldeval # ⬅️ Add this at the top
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
@@ -25,6 +25,45 @@ def save_payload_as_player_file(payload):
|
||||
print(f"📝 Payload saved to {filename}")
|
||||
except Exception as e:
|
||||
print("❌ Failed to save payload:", e)
|
||||
goldeval(filename)
|
||||
|
||||
def append_prices_log(payload):
|
||||
"""Append resource prices and market title to prices.txt in CSV format with timestamp."""
|
||||
try:
|
||||
market = payload.get("gold_market")
|
||||
title = payload.get("gold_market_title", "").strip()
|
||||
|
||||
# Skip if title is missing, empty, or placeholder
|
||||
if not title or title.lower() in ["unknown title", "n/a", "null"]:
|
||||
print("⏭️ Skipping log: Missing or placeholder title")
|
||||
return
|
||||
|
||||
# Skip if market is missing
|
||||
if not market:
|
||||
print("⏭️ Skipping log: Missing market data")
|
||||
return
|
||||
|
||||
# Extract and sanitize values
|
||||
wood = market.get("wood", {}).get("current", "").replace(",", "")
|
||||
stone = market.get("stone", {}).get("current", "").replace(",", "")
|
||||
iron = market.get("iron", {}).get("current", "").replace(",", "")
|
||||
|
||||
# Skip if all values are missing or zero
|
||||
if not wood or not stone or not iron or (wood == "0" and stone == "0" and iron == "0"):
|
||||
print("⏭️ Skipping log: Market values are empty or zero")
|
||||
return
|
||||
|
||||
timestamp = datetime.now().isoformat()
|
||||
safe_title = title.replace('"', "'")
|
||||
|
||||
line = f'{timestamp},{wood},{stone},{iron},"{safe_title}"\n'
|
||||
|
||||
with open("prices.txt", "a", encoding="utf-8") as f:
|
||||
f.write(line)
|
||||
|
||||
print("📈 Appended to prices.txt:", line.strip())
|
||||
except Exception as log_err:
|
||||
print("⚠️ Failed to log to prices.txt:", log_err)
|
||||
|
||||
@app.route('/api/grepolis-data', methods=['POST'])
|
||||
def receive_data():
|
||||
@@ -34,10 +73,11 @@ def receive_data():
|
||||
payload = request.get_json(force=True)
|
||||
print("✅ Parsed JSON:", payload)
|
||||
|
||||
# Inject server-side timestamp (system time)
|
||||
# Inject server-side timestamp
|
||||
payload["server_received_at"] = datetime.now().isoformat()
|
||||
|
||||
save_payload_as_player_file(payload)
|
||||
append_prices_log(payload)
|
||||
|
||||
return jsonify({
|
||||
"status": "success",
|
||||
@@ -58,4 +98,4 @@ def handle_error(e):
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
|
||||
Reference in New Issue
Block a user