-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
152 lines (127 loc) · 4.72 KB
/
app.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
import customtkinter as tk
import requests
from bs4 import BeautifulSoup
keywords = []
def fetchPage(url):
"""fetchPage fetches the page from the given URL
Args:
url (_type_): url of the page to fetch
Returns:
page object
"""
try:
page = requests.get(url)
page.raise_for_status()
return page
except requests.HTTPError as e:
print(f"HTTP Error: {e}")
except Exception as e:
print(f"Error: {e}")
def analyzePage(url, keywords, resultLabel):
"""analyzePage analyzes the page
Args:
string: page url
Returns:
seo check results
"""
html = fetchPage(url)
if html:
soup = BeautifulSoup(html.text, 'html.parser')
# SEO checks
titleCheckResult = checkTitle(soup, keywords)
metaCheckResult = checkMetaDescription(soup, keywords)
headingCheckResult = checkHeadings(soup, keywords)
# print the results
print(titleCheckResult)
print(metaCheckResult)
print(headingCheckResult)
#show the results in the GUI
resultLabel.configure(text=f"{titleCheckResult}\n{metaCheckResult}\n{headingCheckResult}")
def checkTitle(soup, keywords):
"""checkTitle checks the title of the page
Args:
soup (soup object): soup object of the page
keywords (list): list of keywords to check for in the title (from the user input)
Returns:
string: is title too short? too long? or just right?
"""
title = soup.find('title')
titleLength = len(title.string) if title else 0
output = "Title:"
if titleLength < 40:
output += " Too Short"
elif titleLength > 60:
output += " Too Long"
titleWords = title.string.upper().split()
includedKeywords = 0
for keyword in keywords:
if keyword in titleWords:
includedKeywords += 1
if includedKeywords < len(keywords) // 2:
output += " Doesn't include enough keywords"
return output
def checkMetaDescription(soup, keywords):
"""checkMetaDescription checks the meta description of the page
Args:
soup (soup object): soup object of the page
Returns:
string: is meta description too short? too long? or just right?
"""
metaDescription = soup.find('meta', attrs={'name': 'description'})
if metaDescription is None:
return "Meta Description: Not Found"
metaDescriptionLength = len(metaDescription['content'])
metaDescriptionWords = metaDescription['content'].upper().split()
output = "Meta Description:"
if metaDescriptionLength < 70:
output += " Too Short"
elif metaDescriptionLength > 160:
output += " Too Long"
for keyword in keywords:
if keyword not in metaDescriptionWords:
output += " Doesn't include all keywords"
return output
return output
def checkHeadings(soup, keywords):
"""checkHeadings checks the headings of the page
Args:
soup (soup object): soup object of the page
keywords (list): list of keywords to check for in the headings (from the user input)
Returns:
string: do headings contain keywords?
"""
f_keywords = keywords
headings = soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
for heading in headings:
if heading.string is not None:
for keyword in f_keywords:
if keyword in heading.string.upper():
keywords.remove(keyword)
if not f_keywords:
return "Headings: Contain all keywords"
return "Headings: Do not contain all keywords"
def main():
# GUI
tk.set_appearance_mode("system")
tk.set_default_color_theme("dark-blue")
root = tk.CTk()
root.title("SEO Analyzer")
root.geometry("500x600")
frame = tk.CTkFrame(master=root)
frame.pack(pady=20, padx=60, fill="both", expand=True)
mainLabel = tk.CTkLabel(master=frame, text="SEO Analyzer", font=("Roboto", 20))
mainLabel.pack(pady=12, padx=10)
urlBox = tk.CTkEntry(master=frame, placeholder_text="Enter URL", font=("Roboto", 12))
urlBox.pack(pady=12, padx=10)
#CoPilot & GPT4 Assisted Start
keywordBox = tk.CTkTextbox(master=frame, height=5, font=("Roboto", 12))
keywordBox.pack(pady=12, padx=10, fill="both", expand=True)
analyzeButton = tk.CTkButton(master=frame, text="Analyze", font=("Roboto", 12), command=lambda: \
analyzePage("https://" + urlBox.get(), keywordBox.get("1.0", "end-1c").upper().split("\n"), resultLabel))
analyzeButton.pack(pady=12, padx=10)
#CoPilot & GPT4 Assisted End
resultLabel = tk.CTkLabel(master=frame, text="", font=("Roboto", 12))
resultLabel.pack(pady=12, padx=10)
root.mainloop()
if __name__ == "__main__":
main()