Update bookmarks to reflect permanent redirects upon exiting.

This commit is contained in:
Solderpunk 2023-11-18 14:23:56 +01:00
parent de7e5dc254
commit 247f01e3e7
1 changed files with 34 additions and 0 deletions

34
av98.py
View File

@ -775,6 +775,38 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
self.log["ipv6_requests"] += 1
self.log["ipv6_bytes_recvd"] += size
def _maintain_bookmarks(self):
# Nothing to do if no bookmarks exist!
bm_file = os.path.join(self.config_dir, "bookmarks.gmi")
if not os.path.exists(bm_file):
return
# Backup bookmark file
backup_file = tempfile.NamedTemporaryFile(delete=False)
backup_file.close()
backup_file = backup_file.name
shutil.copyfile(bm_file, backup_file)
# Attempt maintenance, restore backup if anything fails
try:
with open(backup_file, "r") as fp_old, open(bm_file, "w") as fp_new:
for line in fp_old:
if not line.startswith("=>"):
fp_new.write(line)
continue
old_url = line.split()[1]
url = old_url
while url in self.permanent_redirects:
url = self.permanent_redirects[url]
if url != old_url:
ui_out.debug("Updating old bookmark url {} to {} based on permanent redirect.".format(old_url, url))
fp_new.write(line.replace(old_url, url))
except Exception as err:
shutil.copyfile(backup_file, bm_file)
ui_out.debug(traceback.format_exc())
finally:
os.unlink(backup_file)
# Cmd implementation follows
def default(self, line):
@ -1266,6 +1298,8 @@ current gemini browsing session."""
os.unlink(self.raw_file_buffer)
os.unlink(self.rendered_file_buffer)
self.client_cert_manager.cleanup()
# Apply permanent redirects to bookmarks
self._maintain_bookmarks()
# Say goodbye
print()
print("Thank you for flying AV-98!")