-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestingproduction02.py
374 lines (298 loc) · 14.8 KB
/
testingproduction02.py
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
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QProgressBar, QPlainTextEdit, QFrame, QScrollArea
from PyQt5.QtGui import QFont, QPixmap, QTextCursor, QPalette, QColor
from PyQt5.QtCore import QThread, pyqtSignal, Qt, QTimer
import requests
from bs4 import BeautifulSoup
import nltk
from nltk.corpus import stopwords
import spacy
from sklearn.feature_extraction.text import CountVectorizer
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from io import BytesIO
from collections import Counter
stop_words = set(stopwords.words('english')).union(set(stopwords.words('indonesian')))
nlp = spacy.load("en_core_web_sm")
class AnalysisThread(QThread):
analysis_complete = pyqtSignal(str, list, list, list, list, bytes, list, int, dict) # Tambahkan dict untuk analisis tambahan
def __init__(self, url):
super(AnalysisThread, self).__init__()
self.url = url
def run(self):
good = []
bad = []
title_message = ""
description_message = ""
image_count = 0
additional_analysis = {}
try:
response = requests.get(self.url, timeout=10)
response.raise_for_status()
except requests.exceptions.Timeout:
self.analysis_complete.emit("Error: Permintaan waktu habis (timeout). Silakan coba lagi.", [], [], [], [], b'', [], 0, {})
return
except requests.exceptions.RequestException as e:
self.analysis_complete.emit(f"Error: {str(e)}", [], [], [], [], b'', [], 0, {})
return
if response.status_code != 200:
self.analysis_complete.emit("Error: Tidak bisa mengakses website untuk analisa.", [], [], [], [], b'', [], 0, {})
return
soup = BeautifulSoup(response.content, 'html.parser')
title = soup.find('title').get_text() if soup.find('title') else None
description = soup.find('meta', attrs={'name': 'description'})['content'] if soup.find('meta', attrs={'name': 'description'}) else None
if title:
title_message = f"{title}"
else:
title_message = "Judul Web tidak ditemukan, tolong berikan judul laman nya"
if description:
description_message = f"{description}"
else:
description_message = "Deskripsi Web tidak ditemukan, tolong minta administrator / pengembang untuk menambahkan deskripsi meta nya"
hs = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
h_tags = []
for h in soup.find_all(hs):
good.append(f"{h.name}-->{h.text.strip()}")
h_tags.append(h.name)
if 'h1' not in h_tags:
bad.append("Tidak ditemukan header H1!")
for img in soup.find_all('img'):
image_count += 1
if not img.get('alt'):
bad.append(f"Image tanpa alt: {img}")
additional_analysis['heading_tags'] = {tag: h_tags.count(tag) for tag in hs}
additional_analysis['meta_description_length'] = len(description) if description else 0
keywords = self.extract_keywords(soup)
additional_chart_data = self.additional_chart_analysis(soup)
ner_entities = self.ner_analysis(soup)
self.analysis_complete.emit(
f"Analisa Komplit, Website yang dianalisa : {self.url}",
[title_message, description_message],
keywords,
good,
bad,
additional_chart_data,
ner_entities,
image_count,
additional_analysis
)
def extract_keywords(self, soup):
paragraphs = soup.find_all('p')
text_content = ' '.join([paragraph.text for paragraph in paragraphs])
words = nltk.word_tokenize(text_content)
filtered_words = [word.lower() for word in words if word.isalpha() and word.lower() not in stop_words]
freq = nltk.FreqDist(filtered_words)
keywords = freq.most_common(10)
return keywords
def additional_chart_analysis(self, soup):
paragraphs = soup.find_all('p')
text_content = ' '.join([paragraph.text for paragraph in paragraphs])
vectorizer = CountVectorizer(stop_words=list(stop_words))
try:
word_frequencies = vectorizer.fit_transform([text_content])
if word_frequencies.shape[1] == 0:
raise ValueError("Vocabulary kosong, mungkin semua kata hanya berupa stop words.")
feature_names = vectorizer.get_feature_names_out()
total_word_frequencies = word_frequencies.sum(axis=0).A1
word_freq_pairs = list(zip(feature_names, total_word_frequencies))
word_freq_pairs = sorted(word_freq_pairs, key=lambda x: x[1], reverse=True)[:30]
top_words, top_frequencies = zip(*word_freq_pairs)
except ValueError as e:
print(f"Error: {str(e)}")
return b''
screen_size = QApplication.primaryScreen().size()
width = int(screen_size.width() * 0.75)
height = int(screen_size.height() * 0.75)
plt.figure(figsize=(width / 100, height / 100))
plt.bar(top_words, top_frequencies)
plt.xticks(rotation='vertical')
plt.xlabel('Kata Kunci')
plt.ylabel('Frekuensi Kata Kunci')
plt.title('Kata Kunci Terbanyak Dalam Website (Top 30)')
plt.tight_layout()
chart_image_bytes = BytesIO()
plt.savefig(chart_image_bytes, format='png')
plt.close()
return chart_image_bytes.getvalue()
def ner_analysis(self, soup):
text = ' '.join([paragraph.text for paragraph in soup.find_all('p')])
doc = nlp(text)
entities = [(ent.text, ent.label_) for ent in doc.ents]
return entities
class SEOAnalyzerApp(QWidget):
def __init__(self):
super(SEOAnalyzerApp, self).__init__()
self.init_ui()
def init_ui(self):
main_layout = QVBoxLayout()
url_label = QLabel("Masukkan URL Website :")
self.url_input = QLineEdit(self)
self.url_input.setPlaceholderText("https://contoh.com")
self.url_input.setFont(QFont("Arial", 12))
main_layout.addWidget(url_label)
main_layout.addWidget(self.url_input)
analyze_button = QPushButton("Mulai Analisa", self)
analyze_button.setFont(QFont("Arial", 12))
analyze_button.clicked.connect(self.start_analysis)
main_layout.addWidget(analyze_button)
self.progress_bar = QProgressBar(self)
main_layout.addWidget(self.progress_bar)
analysis_boxes_layout = QHBoxLayout()
self.first_analysis_box = QPlainTextEdit(self)
self.first_analysis_box.setReadOnly(True)
self.first_analysis_box.setFont(QFont("Courier New", 10))
analysis_boxes_layout.addWidget(self.first_analysis_box)
self.second_analysis_box = QPlainTextEdit(self)
self.second_analysis_box.setReadOnly(True)
self.second_analysis_box.setFont(QFont("Courier New", 10))
analysis_boxes_layout.addWidget(self.second_analysis_box)
main_layout.addLayout(analysis_boxes_layout)
self.third_analysis_box = QPlainTextEdit(self)
self.third_analysis_box.setReadOnly(True)
self.third_analysis_box.setFont(QFont("Courier New", 12))
main_layout.addWidget(self.third_analysis_box)
show_chart_button = QPushButton("Tampilkan Analisa Diagram", self)
show_chart_button.setFont(QFont("Arial", 12))
show_chart_button.clicked.connect(self.show_chart_window)
main_layout.addWidget(show_chart_button)
show_ner_analysis_button = QPushButton("Tampilkan Analisa Lebih Spesifik", self)
show_ner_analysis_button.setFont(QFont("Arial", 12))
show_ner_analysis_button.clicked.connect(self.show_ner_analysis_window)
main_layout.addWidget(show_ner_analysis_button)
self.setLayout(main_layout)
self.setWindowTitle("Aplikasi Analisis SEO")
screen_size = QApplication.primaryScreen().size()
width = int(screen_size.width() * 0.90)
height = int(screen_size.height() * 0.90)
self.resize(width, height)
self.analysis_thread = None
self.chart_window = None
self.ner_analysis_window = None
def start_analysis(self):
self.first_analysis_box.clear()
self.second_analysis_box.clear()
self.third_analysis_box.clear()
url = self.url_input.text()
self.analysis_thread = AnalysisThread(url)
self.analysis_thread.analysis_complete.connect(self.display_results)
self.analysis_thread.start()
self.progress_bar.setRange(0, 0)
self.timer = QTimer(self)
self.timer.timeout.connect(self.on_timeout)
self.timer.start(10000)
def on_timeout(self):
if self.analysis_thread.isRunning():
self.analysis_thread.terminate()
self.display_results("Error: Analisis waktu habis. Silakan coba lagi.", [], [], [], [], b'', [])
def add_horizontal_line(self, widget):
width = widget.viewport().width()
font_metrics = widget.fontMetrics()
char_width = font_metrics.horizontalAdvance('-')
num_chars = width // char_width
if num_chars > 0:
num_chars -= 3
widget.appendPlainText("\n" + "-" * num_chars + "\n")
def display_results(self, message, title_description, keywords, good, bad, chart_image_bytes, ner_entities, image_count, additional_analysis):
self.first_analysis_box.clear()
self.first_analysis_box.appendPlainText(message)
if title_description:
title = title_description[0] if len(title_description) > 0 else None
description = title_description[1] if len(title_description) > 1 else None
if title:
self.first_analysis_box.appendPlainText(f"Judul Web : {title}")
else:
self.first_analysis_box.appendPlainText("Judul Web tidak ditemukan, tolong berikan judul laman nya")
if description:
self.first_analysis_box.appendPlainText(f"Deskripsi Web : {description}")
else:
self.first_analysis_box.appendPlainText("Deskripsi Web tidak ditemukan, tolong tambah deskripsi meta nya")
else:
self.first_analysis_box.appendPlainText("Tidak ada informasi judul dan deskripsi yang ditemukan.")
self.first_analysis_box.moveCursor(QTextCursor.Start)
self.second_analysis_box.clear()
self.second_analysis_box.appendPlainText("Kata Kunci Utama (Top 10):\n")
for keyword, freq in keywords:
self.second_analysis_box.appendPlainText(f"{keyword}: {freq}")
self.second_analysis_box.moveCursor(QTextCursor.Start)
self.third_analysis_box.clear()
self.third_analysis_box.appendPlainText("Analisis Tambahan :")
self.add_horizontal_line(self.third_analysis_box)
if 'meta_description_length' in additional_analysis:
self.third_analysis_box.appendPlainText(f"Panjang Meta Description: {additional_analysis['meta_description_length']} karakter")
self.add_horizontal_line(self.third_analysis_box)
if 'heading_tags' in additional_analysis:
self.third_analysis_box.appendPlainText("Jumlah Heading Tags:\n")
for tag, count in additional_analysis['heading_tags'].items():
self.third_analysis_box.appendPlainText(f"{tag}: {count}")
self.add_horizontal_line(self.third_analysis_box)
self.third_analysis_box.appendPlainText("Judul Header Yang Ditemukan :\n")
for item in good:
self.third_analysis_box.appendPlainText(f"{item}")
self.add_horizontal_line(self.third_analysis_box)
self.third_analysis_box.appendPlainText(f"Total jumlah gambar yang terdeteksi pada laman ini : {image_count}\n")
self.third_analysis_box.appendPlainText("Perbaikan pada tag 'Alt' gambar yang diperlukan:\n")
for item in bad:
self.third_analysis_box.appendPlainText(f"- {item}")
self.third_analysis_box.moveCursor(QTextCursor.Start)
self.chart_image_bytes = chart_image_bytes
self.ner_entities = ner_entities
self.progress_bar.setRange(0, 1)
self.progress_bar.setValue(1)
self.timer.stop()
def show_chart_window(self):
if not self.chart_image_bytes:
return
if self.chart_window:
self.chart_window.close()
self.chart_window = QWidget()
self.chart_window.setWindowTitle("Analisa Diagram")
layout = QVBoxLayout()
chart_label = QLabel(self.chart_window)
pixmap = QPixmap()
pixmap.loadFromData(self.chart_image_bytes)
chart_label.setPixmap(pixmap)
layout.addWidget(chart_label)
self.chart_window.setLayout(layout)
self.chart_window.show()
def show_ner_analysis_window(self):
if not self.ner_entities:
return
if self.ner_analysis_window:
self.ner_analysis_window.close()
self.ner_analysis_window = NERAnalysisWindow(self.ner_entities)
self.ner_analysis_window.setWindowTitle("Analisis Lebih Spesifik")
self.ner_analysis_window.show()
class NERAnalysisWindow(QWidget):
def __init__(self, ner_entities):
super().__init__()
self.init_ui()
self.display_ner_analysis(ner_entities)
def init_ui(self):
layout = QVBoxLayout()
self.setLayout(layout)
self.ner_label = QLabel(self)
layout.addWidget(self.ner_label)
def display_ner_analysis(self, ner_entities):
entity_counter = Counter([entity[0] for entity in ner_entities])
entities, counts = zip(*entity_counter.items())
if len(entities) > 20:
entities = entities[:20]
counts = counts[:20]
fig, ax = plt.subplots()
ax.barh(entities, counts, color='skyblue')
ax.set_xlabel('Jumlah Kemunculan')
ax.set_ylabel('Entitas')
ax.set_title('Analisis NER (Top 20)')
ax.xaxis.set_major_locator(ticker.MaxNLocator(integer=True))
plt.tight_layout()
chart_image_bytes = BytesIO()
plt.savefig(chart_image_bytes, format='png')
plt.close()
chart_pixmap = QPixmap()
chart_pixmap.loadFromData(chart_image_bytes.getvalue())
self.ner_label.setPixmap(chart_pixmap)
if __name__ == "__main__":
app = QApplication(sys.argv)
main_app = SEOAnalyzerApp()
main_app.showFullScreen()
sys.exit(app.exec_())