Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import socket
|
2 |
+
import requests
|
3 |
+
from urllib.parse import urlparse
|
4 |
+
from bs4 import BeautifulSoup
|
5 |
+
import streamlit as st
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
from reportlab.lib.pagesizes import letter
|
8 |
+
from reportlab.pdfgen import canvas
|
9 |
+
|
10 |
+
# تحليل عنوان IP والموقع الجغرافي باستخدام مكتبة GeoLite2
|
11 |
+
import geoip2.database
|
12 |
+
|
13 |
+
def analyze_ip_free(url):
|
14 |
+
try:
|
15 |
+
domain = urlparse(url).netloc
|
16 |
+
ip = socket.gethostbyname(domain)
|
17 |
+
with geoip2.database.Reader('GeoLite2-City.mmdb') as reader:
|
18 |
+
response = reader.city(ip)
|
19 |
+
return {
|
20 |
+
"ip": ip,
|
21 |
+
"city": response.city.name or "Unknown",
|
22 |
+
"region": response.subdivisions.most_specific.name or "Unknown",
|
23 |
+
"country": response.country.name or "Unknown",
|
24 |
+
"latitude": response.location.latitude or "Unknown",
|
25 |
+
"longitude": response.location.longitude or "Unknown",
|
26 |
+
}
|
27 |
+
except Exception as e:
|
28 |
+
return {"error": str(e)}
|
29 |
+
|
30 |
+
# تحليل توفر الموقع باستخدام requests
|
31 |
+
def analyze_uptime_free(url):
|
32 |
+
try:
|
33 |
+
response = requests.get(url, timeout=5)
|
34 |
+
return {
|
35 |
+
"status": "Up" if response.status_code == 200 else "Down",
|
36 |
+
"status_code": response.status_code,
|
37 |
+
}
|
38 |
+
except requests.exceptions.RequestException as e:
|
39 |
+
return {"status": "Down", "error": str(e)}
|
40 |
+
|
41 |
+
# تحليل تحسين محركات البحث (SEO)
|
42 |
+
def analyze_seo_free(url):
|
43 |
+
try:
|
44 |
+
response = requests.get(url)
|
45 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
46 |
+
title = soup.title.string if soup.title else "No Title"
|
47 |
+
meta_description = soup.find("meta", attrs={"name": "description"})
|
48 |
+
keywords = soup.find("meta", attrs={"name": "keywords"})
|
49 |
+
|
50 |
+
return {
|
51 |
+
"title": title,
|
52 |
+
"meta_description": meta_description["content"] if meta_description else "No Description",
|
53 |
+
"keywords": keywords["content"] if keywords else "No Keywords",
|
54 |
+
}
|
55 |
+
except Exception as e:
|
56 |
+
return {"error": str(e)}
|
57 |
+
|
58 |
+
# تحليل الأثر البيئي
|
59 |
+
def analyze_carbon_free(url):
|
60 |
+
try:
|
61 |
+
response = requests.get(url)
|
62 |
+
page_size = len(response.content) / 1024 # بالكيلوبايت
|
63 |
+
co2_estimation = page_size * 0.02 # تقدير تقريبي لانبعاثات CO2
|
64 |
+
return {
|
65 |
+
"page_size_kb": round(page_size, 2),
|
66 |
+
"estimated_co2_g": round(co2_estimation, 2),
|
67 |
+
}
|
68 |
+
except Exception as e:
|
69 |
+
return {"error": str(e)}
|
70 |
+
|
71 |
+
# رسم الرسوم البيانية باستخدام matplotlib
|
72 |
+
def draw_bar_chart(data, title, xlabel, ylabel):
|
73 |
+
keys, values = list(data.keys()), list(data.values())
|
74 |
+
plt.figure(figsize=(8, 5))
|
75 |
+
plt.bar(keys, values, color='skyblue')
|
76 |
+
plt.title(title)
|
77 |
+
plt.xlabel(xlabel)
|
78 |
+
plt.ylabel(ylabel)
|
79 |
+
plt.tight_layout()
|
80 |
+
plt.savefig('chart.png')
|
81 |
+
plt.show()
|
82 |
+
|
83 |
+
# تصدير التقرير إلى PDF
|
84 |
+
def export_to_pdf_free(results, file_path):
|
85 |
+
c = canvas.Canvas(file_path, pagesize=letter)
|
86 |
+
c.drawString(30, 750, "Website Analysis Report")
|
87 |
+
c.drawString(30, 730, "=" * 50)
|
88 |
+
y = 700
|
89 |
+
for section, content in results.items():
|
90 |
+
c.drawString(30, y, f"{section}:")
|
91 |
+
y -= 20
|
92 |
+
for key, value in content.items():
|
93 |
+
c.drawString(50, y, f"- {key}: {value}")
|
94 |
+
y -= 20
|
95 |
+
y -= 20
|
96 |
+
c.save()
|
97 |
+
|
98 |
+
# واجهة المستخدم باستخدام Streamlit
|
99 |
+
st.title("أداة تحليل المواقع")
|
100 |
+
st.write("تحليل شامل للمواقع باستخدام أدوات مجانية")
|
101 |
+
|
102 |
+
# إدخال الرابط
|
103 |
+
url = st.text_input("أدخل رابط الموقع:", "https://example.com")
|
104 |
+
|
105 |
+
if url:
|
106 |
+
st.subheader("1. تحليل عنوان IP والموقع الجغرافي")
|
107 |
+
ip_data = analyze_ip_free(url)
|
108 |
+
if "error" in ip_data:
|
109 |
+
st.error(ip_data["error"])
|
110 |
+
else:
|
111 |
+
st.json(ip_data)
|
112 |
+
|
113 |
+
st.subheader("2. تحليل توافر الموقع")
|
114 |
+
uptime_data = analyze_uptime_free(url)
|
115 |
+
if "error" in uptime_data:
|
116 |
+
st.error(uptime_data["error"])
|
117 |
+
else:
|
118 |
+
st.json(uptime_data)
|
119 |
+
|
120 |
+
st.subheader("3. تحليل تحسين محركات البحث (SEO)")
|
121 |
+
seo_data = analyze_seo_free(url)
|
122 |
+
if "error" in seo_data:
|
123 |
+
st.error(seo_data["error"])
|
124 |
+
else:
|
125 |
+
st.json(seo_data)
|
126 |
+
|
127 |
+
st.subheader("4. تحليل الأثر البيئي")
|
128 |
+
carbon_data = analyze_carbon_free(url)
|
129 |
+
if "error" in carbon_data:
|
130 |
+
st.error(carbon_data["error"])
|
131 |
+
else:
|
132 |
+
st.json(carbon_data)
|
133 |
+
|
134 |
+
# رسم الرسم البياني
|
135 |
+
st.subheader("رسم بياني لتحليل الأثر البيئي")
|
136 |
+
co2_data = {"Page Size (KB)": carbon_data["page_size_kb"], "CO2 Emission (g)": carbon_data["estimated_co2_g"]}
|
137 |
+
draw_bar_chart(co2_data, "Carbon Analysis", "Category", "Value")
|
138 |
+
st.image("chart.png")
|
139 |
+
|
140 |
+
st.subheader("5. تصدير التقرير إلى PDF")
|
141 |
+
if st.button("تصدير التقرير"):
|
142 |
+
results = {
|
143 |
+
"IP Analysis": ip_data,
|
144 |
+
"Uptime Analysis": uptime_data,
|
145 |
+
"SEO Analysis": seo_data,
|
146 |
+
"Carbon Analysis": carbon_data,
|
147 |
+
}
|
148 |
+
file_path = "website_analysis_report.pdf"
|
149 |
+
export_to_pdf_free(results, file_path)
|
150 |
+
st.success(f"تم تصدير التقرير إلى {file_path}")
|
151 |
+
with open(file_path, "rb") as pdf_file:
|
152 |
+
st.download_button("تحميل التقرير", data=pdf_file, file_name="website_analysis_report.pdf")
|