AhmadFiaz commited on
Commit
fddc155
·
verified ·
1 Parent(s): c5d357c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -10
app.py CHANGED
@@ -1,8 +1,9 @@
1
-
2
  import gradio as gr
3
  import requests, time, socket, ssl, random
4
  from ping3 import ping
5
  import dns.resolver
 
 
6
 
7
  def dns_lookup(domain):
8
  try:
@@ -12,6 +13,7 @@ def dns_lookup(domain):
12
  except:
13
  return None
14
 
 
15
  def ssl_handshake(domain):
16
  try:
17
  ctx = ssl.create_default_context()
@@ -24,6 +26,7 @@ def ssl_handshake(domain):
24
  except:
25
  return None
26
 
 
27
  def test_speed(url):
28
  if not url.startswith("http"):
29
  url = "http://" + url
@@ -39,14 +42,13 @@ def test_speed(url):
39
  p = ping(domain, timeout=1)
40
  result["Ping (ms)"] = round(p * 1000, 2) if p else None
41
 
42
- # SSL handshake
43
  ssl_time = ssl_handshake(domain)
44
  result["SSL Handshake (ms)"] = ssl_time
45
 
46
  try:
47
  session = requests.Session()
48
- # Random query param to bypass cache
49
- cache_bypass_url = url + ("?" + str(random.randint(100000, 999999)))
50
 
51
  start_ttfb = time.time()
52
  r = session.get(cache_bypass_url, stream=True, timeout=20)
@@ -57,7 +59,7 @@ def test_speed(url):
57
  result["Final URL"] = r.url
58
  result["Redirect Chain"] = [resp.url for resp in r.history] + [r.url]
59
 
60
- # Full download
61
  start_dl = time.time()
62
  content = r.content
63
  download_time = (time.time() - start_dl) * 1000
@@ -65,24 +67,37 @@ def test_speed(url):
65
  result["Download Time (ms)"] = round(download_time, 2)
66
  result["Page Size (KB)"] = round(len(content) / 1024, 2)
67
 
68
- # Approximate Speed Index
69
- # Very rough approximation: DNS + SSL + TTFB + 50% of download time
70
- speed_index = sum(x for x in [dns_time, ssl_time, ttfb, download_time*0.5] if x)
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  result["Speed Index (ms)"] = round(speed_index, 2)
72
  result["Speed Index (s)"] = round(speed_index / 1000, 2)
73
 
74
  except Exception as e:
75
  return {"Error": str(e)}
76
 
77
- # Simple speed rating based on TTFB
78
  if ttfb < 200:
79
  rating = "Fast"
80
  elif ttfb < 600:
81
  rating = "Medium"
82
  else:
83
  rating = "Slow"
84
- result["Speed Rating"] = rating
85
 
 
86
  return result
87
 
88
 
 
 
1
  import gradio as gr
2
  import requests, time, socket, ssl, random
3
  from ping3 import ping
4
  import dns.resolver
5
+ from bs4 import BeautifulSoup
6
+
7
 
8
  def dns_lookup(domain):
9
  try:
 
13
  except:
14
  return None
15
 
16
+
17
  def ssl_handshake(domain):
18
  try:
19
  ctx = ssl.create_default_context()
 
26
  except:
27
  return None
28
 
29
+
30
  def test_speed(url):
31
  if not url.startswith("http"):
32
  url = "http://" + url
 
42
  p = ping(domain, timeout=1)
43
  result["Ping (ms)"] = round(p * 1000, 2) if p else None
44
 
45
+ # SSL
46
  ssl_time = ssl_handshake(domain)
47
  result["SSL Handshake (ms)"] = ssl_time
48
 
49
  try:
50
  session = requests.Session()
51
+ cache_bypass_url = url + "?" + str(random.randint(100000, 999999))
 
52
 
53
  start_ttfb = time.time()
54
  r = session.get(cache_bypass_url, stream=True, timeout=20)
 
59
  result["Final URL"] = r.url
60
  result["Redirect Chain"] = [resp.url for resp in r.history] + [r.url]
61
 
62
+ # Download
63
  start_dl = time.time()
64
  content = r.content
65
  download_time = (time.time() - start_dl) * 1000
 
67
  result["Download Time (ms)"] = round(download_time, 2)
68
  result["Page Size (KB)"] = round(len(content) / 1024, 2)
69
 
70
+ # ---- ASSET COUNT ----
71
+ soup = BeautifulSoup(content, "html.parser")
72
+
73
+ images = soup.find_all("img")
74
+ videos = soup.find_all("video")
75
+ audios = soup.find_all("audio")
76
+
77
+ result["Images Count"] = len(images)
78
+ result["Videos Count"] = len(videos)
79
+ result["Audios Count"] = len(audios)
80
+ result["Total Assets"] = len(images) + len(videos) + len(audios)
81
+
82
+ # Speed index
83
+ speed_index = sum(
84
+ x for x in [dns_time, ssl_time, ttfb, download_time * 0.5] if x
85
+ )
86
  result["Speed Index (ms)"] = round(speed_index, 2)
87
  result["Speed Index (s)"] = round(speed_index / 1000, 2)
88
 
89
  except Exception as e:
90
  return {"Error": str(e)}
91
 
92
+ # Rating
93
  if ttfb < 200:
94
  rating = "Fast"
95
  elif ttfb < 600:
96
  rating = "Medium"
97
  else:
98
  rating = "Slow"
 
99
 
100
+ result["Speed Rating"] = rating
101
  return result
102
 
103