Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate sitemap #128

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions example/example_site.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
# name of the folder that the site will be generated in
name = "Notion Test Site"

# Sitemap configuration
# Remove if you don't want to generate sitemap.xml
protocol = "https"
domain = "example.com"
remove_html_extension = true

# the notion.so page to being parsing from. This page will become the index.html
# of the generated site, and loconotation will parse all sub-pages present on the page
page = "https://www.notion.so/Loconotion-Example-Page-03c403f4fdc94cc1b315b9469a8950ef"
Expand Down
21 changes: 21 additions & 0 deletions loconotion/modules/notionparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,21 @@ def parse_subpages(self, subpages):
if sub_page not in self.processed_pages.keys():
self.parse_page(sub_page)

def export_sitemap(self, protocol:str, domain:str, processed_pages:set, remove_html_extension:bool):
# Open file in dist/sitemap.xml to write sitemap
with open(self.dist_folder / "sitemap.xml", "w") as f:
# Write XML header
f.write('<?xml version="1.0" encoding="UTF-8"?>\r')
# Write sitemap index opening tag
f.write('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\r')
# Write the sitemap from domain and processed pages
for page in processed_pages:
if remove_html_extension:
page = page.replace(".html", "")
f.write(f'\t<url><loc>{protocol}://{domain}/{page}</loc></url>\r')
# Write sitemap index closing tag
f.write("</urlset>")

def load(self, url):
self.driver.get(url)
WebDriverWait(self.driver, 60).until(notion_page_loaded())
Expand All @@ -762,6 +777,12 @@ def run(self):
start_time = time.time()
self.processed_pages = {}
self.parse_page(self.starting_url)
if self.config.get("domain",None):
self.export_sitemap(
self.config.get("protocol", "https"),
self.config.get("domain"),
set(self.processed_pages.values()),
self.config.get("remove_html_extension", False))
elapsed_time = time.time() - start_time
formatted_time = "{:02d}:{:02d}:{:02d}".format(
int(elapsed_time // 3600),
Expand Down