-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
73 lines (60 loc) · 2.54 KB
/
server.py
File metadata and controls
73 lines (60 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import time
from flask import Flask, jsonify, render_template
from flask_cors import CORS
import requests
from bs4 import BeautifulSoup
from datetime import datetime
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
profit_archive = [] # قائمة لتخزين الأرباح والخسائر (آخر 20 عملية فقط)
@app.route('/get-gold-prices', methods=['GET'])
def get_gold_prices():
try:
url = "https://bahrain-goldprice.com/"
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.content, "html.parser")
gold_oz = None
gold_20g = None
gold_oz_row = soup.find_all("div", class_="divTableRow")
for row in gold_oz_row:
cells = row.find_all("div", class_="divTableCell")
if len(cells) >= 2:
if "سعر سبيكة الذهب اليوم أونصة" in cells[0].text:
gold_oz = float(cells[1].text.strip().replace(",", ""))
elif "سعر سبيكة الذهب اليوم 20 جرام" in cells[0].text:
gold_20g = float(cells[1].text.strip().replace(",", ""))
if gold_oz is None or gold_20g is None:
raise ValueError("Failed to extract gold prices.")
# أسعار الشراء الثابتة
buy_price_oz = 1032
buy_price_20g = 661
# حساب الربح/الخسارة
profit_loss_oz = gold_oz - buy_price_oz
profit_loss_20g = gold_20g - buy_price_20g
total_profit_loss = profit_loss_oz + profit_loss_20g
# تسجيل البيانات في الأرشيف مع الوقت
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
profit_archive.append({
"timestamp": timestamp,
"profit_loss_oz": round(profit_loss_oz, 2),
"profit_loss_20g": round(profit_loss_20g, 2),
"total_profit_loss": round(total_profit_loss, 2)
})
# الاحتفاظ فقط بآخر 20 عملية
if len(profit_archive) > 20:
profit_archive.pop(0)
return jsonify({
"gold_oz": gold_oz,
"gold_20g": gold_20g,
"profit_archive": profit_archive
})
except Exception as e:
return jsonify({"error": "Failed to fetch gold prices", "details": str(e)}), 500
@app.route('/')
def home():
return render_template('gold_price_tracker.html')
if __name__ == '__main__':
port = int(os.getenv('PORT', 5000))
app.run(host='0.0.0.0', port=port)