This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathremove_rate_widget.py
61 lines (44 loc) · 1.68 KB
/
remove_rate_widget.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
#!/usr/bin/env python3.7
from bs4 import BeautifulSoup
import shutil
import pathlib
import sys
def main(html_path):
# Convert the path to a pathlib and break to parts
html_path = pathlib.Path(html_path)
html_dir = html_path.parent
html_name = html_path.name
html_backup = pathlib.Path(html_dir / (html_name + '.bk'))
try:
# First make a backup of the file
shutil.copy(html_path, html_backup)
# Open and read in the html file
html_file = open(html_path,'r')
html_doc = html_file.read()
html_file.close()
# Parse the html file using beautiful soup
soup = BeautifulSoup(html_doc, 'html.parser')
# Remove the rating menu itself
rating_menu = soup.find("div", {"id": "rating-menu"})
rating_menu.replace_with("")
# Remove the button that toggles the rating menu (just to avoid confusion)
rating_toggler = soup.find('a', {'id': 'rating-toggler'})
rating_toggler.replace_with("")
# Remove the old html file
html_path.unlink()
html = soup.prettify("utf-8")
with open(html_path, "wb") as file:
file.write(html)
# If this all worked fine, remove the backup
html_backup.unlink()
except Exception as e:
# Error processing file...just restore the old one
html_path.unlink()
shutil.copy(html_backup, html_path)
html_backup.unlink()
raise e
if __name__ == '__main__':
if len(sys.argv) != 2:
print('Requires a single html file as input')
else:
main(sys.argv[1])