forked from malakazzz0/urls-mthrfckr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmrl.py
89 lines (70 loc) · 2.55 KB
/
htmrl.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
from bs4 import BeautifulSoup
import requests
import logging
import os
# Initialize logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s [%(levelname)s] %(message)s',
handlers=[
logging.FileHandler('bookmark_processing.log'),
logging.StreamHandler()
]
)
# Initialize statistics counters
total_entries = 0
duplicates_removed = 0
descriptions_fetched = 0
# Function to normalize URLs
def normalize_url(url):
url = url.lower().replace('http://', '').replace('https://', '')
return url.rstrip('/')
# Prompt user for file location
file_location = input("Enter the path to the HTML bookmark file: ")
try:
# Read HTML file
with open(file_location, 'r', encoding='utf-8') as file:
soup = BeautifulSoup(file.read(), 'lxml')
# Extract all links
links = soup.find_all('a')
total_entries = len(links)
logging.info(f"Found {total_entries} bookmarks in {file_location}")
# Remove duplicate URLs
unique_urls = {}
new_links = []
for link in links:
url = link.get('href', '').strip()
normalized_url = normalize_url(url)
if normalized_url and normalized_url not in unique_urls:
unique_urls[normalized_url] = True
new_links.append(link)
else:
duplicates_removed += 1
link.extract()
logging.info(
f"Removed {duplicates_removed} duplicates. {len(new_links)} unique bookmarks remaining.")
# Fetch missing descriptions
for link in new_links:
url = link.get('href', '').strip()
description = link.string if link.string else ''
if url and not description:
try:
response = requests.get(url, timeout=5)
if response.status_code == 200:
parsed_url = urlparse(url)
link.string = f"Description for {parsed_url.netloc}"
descriptions_fetched += 1
except Exception as e:
logging.error(
f"Failed to fetch description for URL {url}: {e}")
# Write the updated HTML back to a new file
new_file_location = f"updated_{file_location}"
with open(new_file_location, 'w', encoding='utf-8') as file:
file.write(str(soup))
logging.info(f"Saved updated bookmarks to {new_file_location}")
# Show statistics
print(f"Total entries: {total_entries}")
print(f"Duplicates removed: {duplicates_removed}")
print(f"Descriptions fetched: {descriptions_fetched}")
except Exception as e:
logging.error(f"An error occurred: {e}")