joermd commited on
Commit
da60d2c
·
verified ·
1 Parent(s): 4072976

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -57
app.py CHANGED
@@ -6,11 +6,19 @@ 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)
@@ -27,8 +35,16 @@ def analyze_ip_free(url):
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 {
@@ -38,8 +54,16 @@ def analyze_uptime_free(url):
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')
@@ -55,12 +79,20 @@ def analyze_seo_free(url):
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),
@@ -68,8 +100,16 @@ def analyze_carbon_free(url):
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')
@@ -80,8 +120,14 @@ def draw_bar_chart(data, title, xlabel, ylabel):
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)
@@ -95,58 +141,72 @@ def export_to_pdf_free(results, file_path):
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")
 
 
 
 
 
6
  import matplotlib.pyplot as plt
7
  from reportlab.lib.pagesizes import letter
8
  from reportlab.pdfgen import canvas
 
 
9
  import geoip2.database
10
 
11
  def analyze_ip_free(url):
12
+ """
13
+ Analyze IP address and geolocation of a given URL
14
+ Uses GeoLite2 database to retrieve location information
15
+
16
+ Args:
17
+ url (str): Website URL to analyze
18
+
19
+ Returns:
20
+ dict: IP and location details or error information
21
+ """
22
  try:
23
  domain = urlparse(url).netloc
24
  ip = socket.gethostbyname(domain)
 
35
  except Exception as e:
36
  return {"error": str(e)}
37
 
 
38
  def analyze_uptime_free(url):
39
+ """
40
+ Check website availability and response status
41
+
42
+ Args:
43
+ url (str): Website URL to check
44
+
45
+ Returns:
46
+ dict: Uptime status and status code
47
+ """
48
  try:
49
  response = requests.get(url, timeout=5)
50
  return {
 
54
  except requests.exceptions.RequestException as e:
55
  return {"status": "Down", "error": str(e)}
56
 
 
57
  def analyze_seo_free(url):
58
+ """
59
+ Extract basic SEO information from the website
60
+
61
+ Args:
62
+ url (str): Website URL to analyze
63
+
64
+ Returns:
65
+ dict: SEO-related metadata
66
+ """
67
  try:
68
  response = requests.get(url)
69
  soup = BeautifulSoup(response.text, 'html.parser')
 
79
  except Exception as e:
80
  return {"error": str(e)}
81
 
 
82
  def analyze_carbon_free(url):
83
+ """
84
+ Estimate website's carbon footprint based on page size
85
+
86
+ Args:
87
+ url (str): Website URL to analyze
88
+
89
+ Returns:
90
+ dict: Page size and estimated CO2 emissions
91
+ """
92
  try:
93
  response = requests.get(url)
94
+ page_size = len(response.content) / 1024 # in kilobytes
95
+ co2_estimation = page_size * 0.02 # rough CO2 emission estimate
96
  return {
97
  "page_size_kb": round(page_size, 2),
98
  "estimated_co2_g": round(co2_estimation, 2),
 
100
  except Exception as e:
101
  return {"error": str(e)}
102
 
 
103
  def draw_bar_chart(data, title, xlabel, ylabel):
104
+ """
105
+ Create a bar chart visualization
106
+
107
+ Args:
108
+ data (dict): Data to visualize
109
+ title (str): Chart title
110
+ xlabel (str): X-axis label
111
+ ylabel (str): Y-axis label
112
+ """
113
  keys, values = list(data.keys()), list(data.values())
114
  plt.figure(figsize=(8, 5))
115
  plt.bar(keys, values, color='skyblue')
 
120
  plt.savefig('chart.png')
121
  plt.show()
122
 
 
123
  def export_to_pdf_free(results, file_path):
124
+ """
125
+ Export analysis results to a PDF report
126
+
127
+ Args:
128
+ results (dict): Analysis results
129
+ file_path (str): Path to save PDF
130
+ """
131
  c = canvas.Canvas(file_path, pagesize=letter)
132
  c.drawString(30, 750, "Website Analysis Report")
133
  c.drawString(30, 730, "=" * 50)
 
141
  y -= 20
142
  c.save()
143
 
144
+ def main():
145
+ """
146
+ Main Streamlit application for website analysis
147
+ """
148
+ st.title("أداة تحليل المواقع")
149
+ st.write("تحليل شامل للمواقع باستخدام أدوات مجانية")
150
 
151
+ # URL input
152
+ url = st.text_input("أدخل رابط الموقع:", "https://example.com")
153
 
154
+ if url:
155
+ # IP Analysis
156
+ st.subheader("1. تحليل عنوان IP والموقع الجغرافي")
157
+ ip_data = analyze_ip_free(url)
158
+ if "error" in ip_data:
159
+ st.error(ip_data["error"])
160
+ else:
161
+ st.json(ip_data)
162
 
163
+ # Uptime Analysis
164
+ st.subheader("2. تحليل توافر الموقع")
165
+ uptime_data = analyze_uptime_free(url)
166
+ if "error" in uptime_data:
167
+ st.error(uptime_data["error"])
168
+ else:
169
+ st.json(uptime_data)
170
 
171
+ # SEO Analysis
172
+ st.subheader("3. تحليل تحسين محركات البحث (SEO)")
173
+ seo_data = analyze_seo_free(url)
174
+ if "error" in seo_data:
175
+ st.error(seo_data["error"])
176
+ else:
177
+ st.json(seo_data)
178
 
179
+ # Carbon Analysis
180
+ st.subheader("4. تحليل الأثر البيئي")
181
+ carbon_data = analyze_carbon_free(url)
182
+ if "error" in carbon_data:
183
+ st.error(carbon_data["error"])
184
+ else:
185
+ st.json(carbon_data)
186
 
187
+ # Carbon Analysis Chart
188
+ st.subheader("رسم بياني لتحليل الأثر البيئي")
189
+ co2_data = {
190
+ "Page Size (KB)": carbon_data["page_size_kb"],
191
+ "CO2 Emission (g)": carbon_data["estimated_co2_g"]
192
+ }
193
+ draw_bar_chart(co2_data, "Carbon Analysis", "Category", "Value")
194
+ st.image("chart.png")
195
 
196
+ # PDF Export
197
+ st.subheader("5. تصدير التقرير إلى PDF")
198
+ if st.button("تصدير التقرير"):
199
+ results = {
200
+ "IP Analysis": ip_data,
201
+ "Uptime Analysis": uptime_data,
202
+ "SEO Analysis": seo_data,
203
+ "Carbon Analysis": carbon_data,
204
+ }
205
+ file_path = "website_analysis_report.pdf"
206
+ export_to_pdf_free(results, file_path)
207
+ st.success(f"تم تصدير التقرير إلى {file_path}")
208
+ with open(file_path, "rb") as pdf_file:
209
+ st.download_button("تحميل التقرير", data=pdf_file, file_name="website_analysis_report.pdf")
210
+
211
+ if __name__ == "__main__":
212
+ main()