File size: 5,604 Bytes
b43f4c5 |
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 |
import socket
import requests
from urllib.parse import urlparse
from bs4 import BeautifulSoup
import streamlit as st
import matplotlib.pyplot as plt
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
# تحليل عنوان IP والموقع الجغرافي باستخدام مكتبة GeoLite2
import geoip2.database
def analyze_ip_free(url):
try:
domain = urlparse(url).netloc
ip = socket.gethostbyname(domain)
with geoip2.database.Reader('GeoLite2-City.mmdb') as reader:
response = reader.city(ip)
return {
"ip": ip,
"city": response.city.name or "Unknown",
"region": response.subdivisions.most_specific.name or "Unknown",
"country": response.country.name or "Unknown",
"latitude": response.location.latitude or "Unknown",
"longitude": response.location.longitude or "Unknown",
}
except Exception as e:
return {"error": str(e)}
# تحليل توفر الموقع باستخدام requests
def analyze_uptime_free(url):
try:
response = requests.get(url, timeout=5)
return {
"status": "Up" if response.status_code == 200 else "Down",
"status_code": response.status_code,
}
except requests.exceptions.RequestException as e:
return {"status": "Down", "error": str(e)}
# تحليل تحسين محركات البحث (SEO)
def analyze_seo_free(url):
try:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.title.string if soup.title else "No Title"
meta_description = soup.find("meta", attrs={"name": "description"})
keywords = soup.find("meta", attrs={"name": "keywords"})
return {
"title": title,
"meta_description": meta_description["content"] if meta_description else "No Description",
"keywords": keywords["content"] if keywords else "No Keywords",
}
except Exception as e:
return {"error": str(e)}
# تحليل الأثر البيئي
def analyze_carbon_free(url):
try:
response = requests.get(url)
page_size = len(response.content) / 1024 # بالكيلوبايت
co2_estimation = page_size * 0.02 # تقدير تقريبي لانبعاثات CO2
return {
"page_size_kb": round(page_size, 2),
"estimated_co2_g": round(co2_estimation, 2),
}
except Exception as e:
return {"error": str(e)}
# رسم الرسوم البيانية باستخدام matplotlib
def draw_bar_chart(data, title, xlabel, ylabel):
keys, values = list(data.keys()), list(data.values())
plt.figure(figsize=(8, 5))
plt.bar(keys, values, color='skyblue')
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.tight_layout()
plt.savefig('chart.png')
plt.show()
# تصدير التقرير إلى PDF
def export_to_pdf_free(results, file_path):
c = canvas.Canvas(file_path, pagesize=letter)
c.drawString(30, 750, "Website Analysis Report")
c.drawString(30, 730, "=" * 50)
y = 700
for section, content in results.items():
c.drawString(30, y, f"{section}:")
y -= 20
for key, value in content.items():
c.drawString(50, y, f"- {key}: {value}")
y -= 20
y -= 20
c.save()
# واجهة المستخدم باستخدام Streamlit
st.title("أداة تحليل المواقع")
st.write("تحليل شامل للمواقع باستخدام أدوات مجانية")
# إدخال الرابط
url = st.text_input("أدخل رابط الموقع:", "https://example.com")
if url:
st.subheader("1. تحليل عنوان IP والموقع الجغرافي")
ip_data = analyze_ip_free(url)
if "error" in ip_data:
st.error(ip_data["error"])
else:
st.json(ip_data)
st.subheader("2. تحليل توافر الموقع")
uptime_data = analyze_uptime_free(url)
if "error" in uptime_data:
st.error(uptime_data["error"])
else:
st.json(uptime_data)
st.subheader("3. تحليل تحسين محركات البحث (SEO)")
seo_data = analyze_seo_free(url)
if "error" in seo_data:
st.error(seo_data["error"])
else:
st.json(seo_data)
st.subheader("4. تحليل الأثر البيئي")
carbon_data = analyze_carbon_free(url)
if "error" in carbon_data:
st.error(carbon_data["error"])
else:
st.json(carbon_data)
# رسم الرسم البياني
st.subheader("رسم بياني لتحليل الأثر البيئي")
co2_data = {"Page Size (KB)": carbon_data["page_size_kb"], "CO2 Emission (g)": carbon_data["estimated_co2_g"]}
draw_bar_chart(co2_data, "Carbon Analysis", "Category", "Value")
st.image("chart.png")
st.subheader("5. تصدير التقرير إلى PDF")
if st.button("تصدير التقرير"):
results = {
"IP Analysis": ip_data,
"Uptime Analysis": uptime_data,
"SEO Analysis": seo_data,
"Carbon Analysis": carbon_data,
}
file_path = "website_analysis_report.pdf"
export_to_pdf_free(results, file_path)
st.success(f"تم تصدير التقرير إلى {file_path}")
with open(file_path, "rb") as pdf_file:
st.download_button("تحميل التقرير", data=pdf_file, file_name="website_analysis_report.pdf")
|