-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauditor.py
More file actions
452 lines (366 loc) · 17.7 KB
/
auditor.py
File metadata and controls
452 lines (366 loc) · 17.7 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import requests
from bs4 import BeautifulSoup
from transformers import pipeline
import time
import socket
import ssl
from urllib.parse import urlparse
import sys
import json
import os
from datetime import datetime
class SiteAuditor:
def __init__(self, url):
# Ensure URL has schema
if not url.startswith(('http://', 'https://')):
url = 'https://' + url
self.url = url
self.domain = urlparse(url).netloc
self.soup = None
self.response = None
self.report = {}
def fetch_site(self):
"""Basic setup: Gets the HTML and measures response time."""
try:
start_time = time.time()
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
self.response = requests.get(self.url, headers=headers, timeout=10)
self.load_time = round(time.time() - start_time, 2)
self.soup = BeautifulSoup(self.response.text, 'html.parser')
return True
except Exception as e:
self.report['error'] = str(e)
return False
def extract_visible_text(self):
"""Extracts and returns visible text from the HTML."""
if not self.soup:
return ""
for tag in self.soup(["script", "style", "noscript"]):
tag.decompose()
text = self.soup.get_text(separator="\n")
lines = [line.strip() for line in text.splitlines()]
visible_text = "\n".join(line for line in lines if line)
return visible_text
def summarize_content(self):
"""Uses a transfermer model to summarize the visible text."""
text = self.extract_visible_text()
if not text:
return "No visible text found."
# Limit input size to avoid model overload
short_text = text[:2000]
try:
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
summary = summarizer(short_text, max_length=200, min_length=50, do_sample=False)
return summary[0]['summary_text']
except Exception as e:
return f"Summarization failed: {e}"
# --- MODULE 1: SPEED & BASICS ---
def fetch_site(self):
print(f"Attempting to connect to: {self.url}...") # Feedback so you know it's working
try:
start_time = time.time()
# Added a shorter timeout (5 seconds) so it doesn't hang forever
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
self.response = requests.get(self.url, headers=headers, timeout=5)
self.load_time = round(time.time() - start_time, 2)
self.soup = BeautifulSoup(self.response.text, 'html.parser')
print("✅ Connection Established!")
return True
except Exception as e:
print(f"❌ ERROR: Could not connect to {self.url}")
print(f"Reason: {e}")
self.report['error'] = str(e)
return False
def check_health(self):
# SECURITY CHECK: Stop if the site didn't load
if not self.response and not self.fetch_site():
return
print(f"Analyzing Health...")
# 1. Load Speed
self.report['load_time'] = f"{self.load_time} seconds"
self.report['speed_rating'] = "GOOD" if self.load_time < 1.5 else "CRITICAL WARNING"
# 2. Mobile Viewport
viewport = self.soup.find('meta', attrs={'name': 'viewport'})
self.report['mobile_optimized'] = bool(viewport)
# --- MODULE 2: SECURITY ---
def check_security(self):
print("Checking Security...")
# 1. SSL Check
if self.url.startswith('https'):
self.report['ssl'] = True
else:
self.report['ssl'] = False
# --- MODULE 3: THE MONEY (Ad Tech) ---
def check_ad_pixels(self):
print("Checking Ad Tech...")
html_text = str(self.soup)
# Check for Facebook Pixel
has_fb_pixel = "fbq('init'" in html_text or "fbevents.js" in html_text
# Check for Google Analytics/Ads (G-Tag)
has_google_tag = "gtag(" in html_text or "googletagmanager" in html_text
self.report['facebook_pixel'] = has_fb_pixel
self.report['google_tag'] = has_google_tag
def generate_report(self):
"""Outputs the data in a readable format for the client."""
print("\n" + "="*40)
print(f"AUDIT REPORT FOR: {self.domain}")
print("="*40)
# FIRST PAUSE
print("\n📊 First, let's look at Performance...")
input(" [Press Enter to see Speed & Mobile results]")
print("\n--- SITE HEALTH CHECK ---")
print("Reporting Speed, Mobile, and Security Status:\n\n")
# Speed Section
print(f"⏱️ Load Speed: {self.report['load_time']} [{self.report['speed_rating']}]")
if self.report['speed_rating'] == "CRITICAL WARNING":
print(" -> PITCH: 'Amazon found that every 0.1s delay costs 1% in sales.'")
# Mobile Section
mobile_status = "✅ Optimized" if self.report['mobile_optimized'] else "❌ NOT OPTIMIZED"
print(f"📱 Mobile Ready: {mobile_status}")
if not self.report['mobile_optimized']:
print(" -> PITCH: '50% of traffic is mobile. You are blocking half your customers.'")
# SECOND PAUSE
print("\n🔒 Next, let's look at Security and SEO...")
input(" [Press Enter to see Security and SEO results]")
# Security Section
ssl_status = "✅ Secure" if self.report['ssl'] else "❌ NOT SECURE"
print(f"🔒 Security: {ssl_status}")
print("\n--- DIGITAL MARKETING CHECK ---")
print("Analyzing Ad Tech, SEO, and Contact Options:\n\n")
# Money Section (The Closer)
print("-" * 20)
print("💰 MISSED REVENUE DETECTOR")
fb_status = "✅ Installed" if self.report['facebook_pixel'] else "❌ MISSING (Losing Money)"
print(f" - Facebook Pixel: {fb_status}")
google_status = "✅ Installed" if self.report['google_tag'] else "❌ MISSING (Flying Blind)"
print(f" - Google Tag: {google_status}")
#SEO Section
print("-" * 20)
print("🔍 Checking SEO Health (Google visibility)")
print(f" - Title Tag: {self.report.get('seo_title', 0)}")
# Logic: Titles should be between 30-60 characters
t_len = self.report.get('seo_title_len', 0)
if t_len < 25 or t_len > 70:
print(" ⚠️ Title length is suboptimal. Aim for 30-60 characters. ⚠️")
print(f" (Current Length: {t_len} characters)")
print(f" - Meta Description: {self.report.get('seo__desc')}")
if self.report.get('seo_desc') == "❌ MISSING":
print(" ⚠️ Meta Description is missing. This hurts SEO and CTR. ⚠️")
print(" ⚠️ YOU ARE INVISIBLE ON GOOGLE WITHOUT PROPER METADATA! ⚠️")
# THIRD PAUSE
print("\n Next, lets check Socials and Contact Options results....")
input(" [Press Enter to see Socials and Contact Options results]")
# CONTACT OPTIONS Section
print("-" * 20)
print("📞 CONVERSION CHECK")
P_COUNT = self.report.get('phone_links', 0)
E_COUNT = self.report.get('email_links', 0)
print(f" - Click-to-Call: {'✅ Active' if P_COUNT > 0 else '❌ NO CLICK-TO-CALLS'} (Found: {P_COUNT})")
print(f" - Click-to-Email: {'✅ Active' if E_COUNT > 0 else '❌ NO CLICK-TO-EMAILS'} (Found: {E_COUNT})")
if not self.report['facebook_pixel'] or not self.report['google_tag']:
print("\n -> CLOSING LINE: 'You are paying for ads but not tracking them. We need to fix this today.'")
# Social Section
print("-" * 20)
print("🌐 SOCIAL MEDIA LINKS")
socials = self.report.get('social_links', [])
if socials:
for link in socials:
print(f" - {link}")
else:
print(" ⚠️ No social media links found. ⚠️")
def save_report(self, directory='reports', filename=None, fmt='json'):
"""Save the audit report in the requested format (json, md, txt).
Returns the saved file path or None on failure."""
try:
os.makedirs(directory, exist_ok=True)
ts = datetime.now().strftime('%Y%m%d_%H%M%S')
safe_domain = self.domain.replace(':', '_')
fmt = (fmt or 'json').lower()
if fmt not in ('json', 'md', 'txt'):
fmt = 'json'
ext = fmt
if not filename:
filename = f"{safe_domain}_{ts}.{ext}"
path = os.path.join(directory, filename)
payload = {
'url': self.url,
'domain': self.domain,
'timestamp': ts,
'report': self.report
}
if fmt == 'json':
with open(path, 'w', encoding='utf-8') as f:
json.dump(payload, f, ensure_ascii=False, indent=2)
else:
# Build a readable markdown/plain-text report
lines = []
lines.append(f"# Audit Report for {self.domain}")
lines.append(f"- URL: {self.url}")
lines.append(f"- Timestamp: {ts}")
lines.append("")
# Summary (if exists)
summary = self.report.get('summary') or self.report.get('summary', '')
if summary:
lines.append("## Summary")
lines.append(summary)
lines.append("")
lines.append("## Details")
for key in sorted(self.report.keys()):
if key == 'summary':
continue
val = self.report.get(key)
lines.append(f"### {key}")
if isinstance(val, list):
if val:
for item in val:
lines.append(f"- {item}")
else:
lines.append("- (none)")
else:
lines.append(str(val))
lines.append("")
content = '\n'.join(lines)
# For .txt, we can strip markdown headings
if fmt == 'txt':
content = content.replace('#', '')
with open(path, 'w', encoding='utf-8') as f:
f.write(content)
return path
except Exception as e:
print(f"Failed to save report: {e}")
return None
def check_social_links(self):
print("Checking Social Media Links...")
#The sites we are looking for
target = ['facebook', 'twitter', 'instagram', 'linkedin']
results = []
# Find links
links = self.soup.find_all('a', href=True)
for link in links:
href = link.get('href')
# Check if link contains a target name
if any(t in href for t in target):
try:
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
res = requests.head(href, headers=headers, timeout=5)
# Test if the links actually work
# Linkedin throws 999 or 403 to bots. We count those as "Found".
if res.status_code == 200:
status = "✅ Active"
elif res.status_code in [403, 429, 999]:
status = f"⚠️ Restricted, Please confirm link manually. (Status: {res.status_code})"
else:
status = f"❌ Broken (Status: {res.status_code})"
results.append(f"{href} - {status}")
except:
results.append(f"{href} - ❌ Could not reach link. Connection Failed.")
self.report['social_links'] = results
# --- Module 5: SEO CHECK ---
def check_seo(self):
print("Checking SEO Basics...")
# 1. Title Tag
if self.soup.title and self.soup.title.string:
title = self.soup.title.string.strip()
self.report['seo_title'] = title
self.report['seo_title_len'] = len(title)
else:
self.report['seo_title'] = "❌ MISSING"
self.report['seo_title_len'] = 0
# 2. Meta Description
meta_desc = self.soup.find('meta', attrs={'name': 'description'})
if meta_desc and meta_desc.get('content'):
description = meta_desc['content'].strip()
self.report['seo_meta_desc'] = description
self.report['seo_meta_desc_len'] = len(description)
else:
self.report['seo_meta_desc'] = "❌ MISSING"
self.report['seo_meta_desc_len'] = 0
# 3. H1 Tag
h1_tag = self.soup.find('h1')
if h1_tag:
h1_text = h1_tag.get_text(strip=True)
self.report['seo_h1'] = h1_text
else:
self.report['seo_h1'] = "❌ MISSING"
# 4. Image Alt Tags
images = self.soup.find_all('img')
alt_tags_count = sum(1 for img in images if img.get('alt'))
total_images = len(images)
self.report['seo_images_with_alt'] = alt_tags_count
self.report['seo_total_images'] = total_images
# ---Module 6: CONTACT OPTIONS & IMAGE ALT TAGS ---
def check_contact_options(self):
print("Checking Contact Options...")
# Check for clickable phone numbers
phone_links = self.soup.find_all('a', href=True)
tel_links = [link for link in phone_links if link['href'].startswith('tel:')]
self.report['phone_links'] = len(tel_links)
# Check for clickable emails
mail_links = [link for link in phone_links if link['href'].startswith('mailto:')]
self.report['email_links'] = len(mail_links)
# --- Module 7: Platform Check ---
def check_platform(self):
print("Detecting Web Platform being used...")
# Convert soup to string for easier pattern matching
soup_str = str(self.soup)
# Default to "Unknown"
# --- EXECUTION ---
if __name__ == "__main__":
while True:
print("\n")
print("\n" *3)
print(" 🕵️ Eternal Kosmos Auditor v0.9 🕵️ ")
print("="*50 + "\n")
# 1. Interactive Prompt
print("Type 'exit', 'quit' or 'q' to close the program.\n")
target = input("Enter the website URL to audit (e.g., example.com): ").strip()
if target.lower() in ['exit', 'quit', 'q']:
print("Exiting the program. Goodbye!")
sys.exit()
# Smart URL Handling: Add www/https if missing
if not target.startswith(('http://', 'https://')):
target = 'https://' + target
print(f"\n Initializing scanner for: {target}...\n")
audit = SiteAuditor(target)
connected = audit.fetch_site()
# retry logic: If the first try fails, we ask the user if they want to try 'www'
if not connected:
print("⚠️ Direct connection failed.")
if "www" not in target:
retry = input(f" Do you want to try adding 'www.'? (y/n): ")
if retry.lower() == 'y':
target = target.replace("https://", "https://www.")
audit = SiteAuditor(target)
connected = audit.fetch_site()
if connected:
audit.check_health()
audit.check_security()
audit.check_seo()
audit.check_contact_options()
audit.check_ad_pixels()
audit.check_social_links()
audit.report['summary'] = audit.summarize_content()
print("\n 🔍 AI Summary of Page Content:")
print(audit.report['summary'])
audit.generate_report()
# Prompt to save the audit report
try:
save_choice = input("\nSave audit report to file? (y/n): ").strip().lower()
if save_choice == 'y':
fmt_choice = input("Save format - json/md/txt (default json): ").strip().lower()
if not fmt_choice:
fmt_choice = 'json'
if fmt_choice not in ('json', 'md', 'txt'):
print("Unknown format, defaulting to JSON.")
fmt_choice = 'json'
saved_path = audit.save_report(fmt=fmt_choice)
if saved_path:
print(f"Report saved to: {saved_path}")
else:
print("Report could not be saved.")
except Exception:
print("Skipping save prompt due to input error.")
else:
print("Could not analyze site.")
print("\nAudit Complete. Thank you for using Eternal Kosmos Auditor!\n")
read = input("Press Enter to either search another site or quit the application...")