-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseomi.py
142 lines (115 loc) · 4.91 KB
/
seomi.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
import os
import sys
import datetime
import psycopg2
import requests
from tqdm import tqdm
from bs4 import BeautifulSoup
import xml.etree.ElementTree as ET
from utilities import *
from termcolor import colored
timestamp = datetime.datetime.now()
def main(sitemap_url):
items, alert_names = [], []
response = requests.get(sitemap_url)
root = ET.fromstring(response.content)
if root.tag.endswith('sitemapindex'):
for sitemap in root.findall('.//{http://www.sitemaps.org/schemas/sitemap/0.9}sitemap'):
loc = sitemap.find('{http://www.sitemaps.org/schemas/sitemap/0.9}loc')
if loc is not None:
sitemap_items, sitemap_alert_names = process_sitemap(loc.text)
items.extend(sitemap_items)
alert_names.extend(sitemap_alert_names)
else:
items, alert_names = process_sitemap(sitemap_url)
save_all_to_database(items)
flat_alert_names = [alert for sublist in alert_names for alert in sublist]
print_summary_report(items, flat_alert_names)
def process_sitemap(sitemap_url):
response = requests.get(sitemap_url)
root = ET.fromstring(response.content)
urls = []
for loc in root.findall('.//{http://www.sitemaps.org/schemas/sitemap/0.9}loc'):
urls.append(loc.text)
for link in root.findall('.//xhtml:link[@rel="alternate"]', {'xhtml': 'http://www.w3.org/1999/xhtml'}):
urls.append(link.get('href'))
items = []
alert_names = []
for url in tqdm(urls, desc="Processing URLs", unit="URL", ncols=80):
item, alert_name = process_url(url)
items.append(item)
alert_names.append(alert_name)
return items, alert_names
def process_url(url):
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
item = {}
item['url'] = url
item['title'] = extract_title(soup)
item['meta_description'] = extract_meta_description(soup)
item['h1'] = extract_h1(soup)
item['h2'] = extract_h2(soup)
item['internal_links'] = extract_internal_links(soup, url)
item['external_links'] = extract_external_links(soup, url)
item['count_internal_links'] = count_internal_links(item['internal_links'])
item['count_external_links'] = count_external_links(item['external_links'])
item['url_length'] = extract_url_length(url)
item['title_length'] = extract_title_length(item['title'])
item['meta_description_length'] = extract_meta_description_length(item['meta_description'])
item['meta_keyword'] = extract_meta_keywords(soup)
item['h1_length'] = extract_h1_length(item['h1'])
item['h2_length'] = extract_h2_length(item['h2'])
item['count_paragraphs'] = extract_count_paragraphs(soup)
item['response_time'] = extract_response_time(response)
item['status'] = extract_status(response)
item['word_count'] = extract_word_count(soup)
item['page_size'] = extract_page_size(response)
item['text_ratio'] = extract_text_ratio(soup, response)
item['canonical_url'] = extract_canonical_url(soup)
item['meta_robots'] = extract_meta_robots(soup)
item['image_alt_attributes'] = extract_image_alt_attributes(soup)
item['structured_data'] = extract_structured_data(soup)
item['language_tags'] = extract_language_tags(soup)
item['timestamp'] = timestamp
alerts, alert_names = generate_seo_alerts(item)
item.update(alerts)
return item, alert_names
def save_all_to_database(items):
conn = psycopg2.connect(
dbname=os.environ['DB_NAME'],
user=os.environ['DB_USER'],
password=os.environ['DB_PASS'],
host=os.environ['DB_HOST'],
port=os.environ['DB_PORT']
)
cursor = conn.cursor()
for item in tqdm(items, desc="Saving to Database", unit="URL", ncols=80):
save_to_postgresql(item, cursor, conn)
cursor.close()
conn.close()
def save_to_postgresql(item, cursor, conn):
columns = ', '.join(item.keys())
placeholders = ', '.join(['%s'] * len(item))
query = f"""
INSERT INTO seo_data ({columns})
VALUES ({placeholders});
"""
data = tuple(item.values())
cursor.execute(query, data)
conn.commit()
def print_summary_report(items, alert_names):
total_urls = len(items)
urls_with_alerts = sum(1 for item in items if item['has_alert'])
urls_without_alerts = total_urls - urls_with_alerts
print(colored(f"\nTotal URLs: {total_urls}", "green"))
print(colored(f"URLs without alerts: {urls_without_alerts}", "blue"))
print(colored(f"URLs with alerts: {urls_with_alerts}", "red"))
print(colored("\nAlerts breakdown:", "cyan"))
for alert_type in set(alert_names):
if alert_type != 'has_alert':
alert_count = sum(1 for item in items if item.get(alert_type, False))
print(colored(f"\t{alert_type}: {alert_count}", "yellow"))
print(colored("\nSummary report completed.", "magenta"))
if __name__ == '__main__':
sitemap_url = sys.argv[1]
main(sitemap_url)