srinetwork commited on
Commit
4a99aa4
·
verified ·
1 Parent(s): 5ea2cf1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -5
app.py CHANGED
@@ -1,5 +1,6 @@
1
  from flask import Flask, render_template, request, jsonify
2
  import requests, time, json, os, threading
 
3
 
4
  app = Flask(__name__)
5
 
@@ -61,6 +62,18 @@ def check_sites():
61
  save_urls(urls)
62
  time.sleep(5)
63
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  # ---------------- ROUTES ----------------
65
  @app.route("/")
66
  def index():
@@ -72,13 +85,11 @@ def data():
72
  now = time.time()
73
  for u in urls:
74
  u["remaining"] = max(0, int(u["next_check"] - now))
75
- # formatted uptime string
76
  total_sec = int(now - u["first_added"])
77
  d, rem = divmod(total_sec, 86400)
78
  h, rem = divmod(rem, 3600)
79
  m, s = divmod(rem, 60)
80
  u["uptime_str"] = f"{d}d {h}h {m}m {s}s"
81
-
82
  u["uptime_percent"] = round(
83
  (u["up_checks"] / u["total_checks"])*100,2
84
  ) if u["total_checks"] else 0
@@ -94,11 +105,14 @@ def add():
94
 
95
  now = time.time()
96
  data = load_urls()
 
 
 
97
  data.append({
98
  "id": int(now*1000),
99
  "name": request.form["name"],
100
- "url": request.form["url"],
101
- "img": request.form.get("img", ""), # optional image URL
102
  "interval": int(request.form["interval"]),
103
  "status": "UNKNOWN",
104
  "last_ping": None,
@@ -122,7 +136,8 @@ def edit(id):
122
  if s["id"] == id:
123
  s["name"] = request.form["name"]
124
  s["url"] = request.form["url"]
125
- s["img"] = request.form.get("img", "")
 
126
  s["interval"] = int(request.form["interval"])
127
  break
128
  save_urls(data)
 
1
  from flask import Flask, render_template, request, jsonify
2
  import requests, time, json, os, threading
3
+ from bs4 import BeautifulSoup
4
 
5
  app = Flask(__name__)
6
 
 
62
  save_urls(urls)
63
  time.sleep(5)
64
 
65
+ # ---------------- OG IMAGE FETCH ----------------
66
+ def fetch_og_image(url):
67
+ try:
68
+ r = requests.get(url, timeout=10)
69
+ soup = BeautifulSoup(r.text, "html.parser")
70
+ tag = soup.find("meta", property="og:image")
71
+ if tag and tag.get("content"):
72
+ return tag["content"]
73
+ except:
74
+ pass
75
+ return "" # fallback empty
76
+
77
  # ---------------- ROUTES ----------------
78
  @app.route("/")
79
  def index():
 
85
  now = time.time()
86
  for u in urls:
87
  u["remaining"] = max(0, int(u["next_check"] - now))
 
88
  total_sec = int(now - u["first_added"])
89
  d, rem = divmod(total_sec, 86400)
90
  h, rem = divmod(rem, 3600)
91
  m, s = divmod(rem, 60)
92
  u["uptime_str"] = f"{d}d {h}h {m}m {s}s"
 
93
  u["uptime_percent"] = round(
94
  (u["up_checks"] / u["total_checks"])*100,2
95
  ) if u["total_checks"] else 0
 
105
 
106
  now = time.time()
107
  data = load_urls()
108
+ url = request.form["url"]
109
+ og_img = fetch_og_image(url)
110
+
111
  data.append({
112
  "id": int(now*1000),
113
  "name": request.form["name"],
114
+ "url": url,
115
+ "img": og_img, # auto fetched
116
  "interval": int(request.form["interval"]),
117
  "status": "UNKNOWN",
118
  "last_ping": None,
 
136
  if s["id"] == id:
137
  s["name"] = request.form["name"]
138
  s["url"] = request.form["url"]
139
+ # update OG image automatically
140
+ s["img"] = fetch_og_image(s["url"])
141
  s["interval"] = int(request.form["interval"])
142
  break
143
  save_urls(data)