Skip to content

Commit

Permalink
[sync] add sitemap generator
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler authored Jun 2, 2024
1 parent 9faa182 commit 33706ce
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
18 changes: 16 additions & 2 deletions .github/workflows/sync_build_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ jobs:
- name: Install dependencies
run: pip install -r util/requirements.txt

# New added modules
- name: Add module
if: ${{ (github.event.inputs.repo_user != '') && (github.event.inputs.repo_name != '') }}
run: |
Expand All @@ -69,11 +70,10 @@ jobs:
- name: Commit module
if: ${{ (github.event.inputs.repo_user != '') && (github.event.inputs.repo_name != '') }}
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add modules
git commit -sm "[MODULE] ${{ github.event.inputs.repo_name }}" || true
git push || true
####

- name: Sync
if: ${{ env.IS_SYNC == 'true' }}
Expand Down Expand Up @@ -102,6 +102,20 @@ jobs:
run: |
python util/cli.py index --push
# Sitemap generator
- name: Generate sitemap
if: ${{ env.IS_SYNC == 'true' }}
run: |
python generate_sitemap.py
- name: Commit sitemap
if: ${{ env.IS_SYNC == 'true' }}
run: |
git add json/sitemap.xml
git commit -sm "[SITEMAP] Update sitemap.xml" || true
git push || true
####

- name: Upload logs
uses: actions/upload-artifact@v3
with:
Expand Down
45 changes: 45 additions & 0 deletions generate_sitemap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import json
import xml.etree.ElementTree as ET
from xml.dom import minidom
from datetime import datetime

# Open modules.json
with open('json/modules.json') as json_data:
data = json.load(json_data)

# Access the "modules" object
modules = data.get("modules", [])

# Create the root element for the sitemap
urlset = ET.Element("urlset", xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")

# Base URL for the modules
base_url = "http://mmrl.dergoogler.com/?module="

# Add URLs to the sitemap
for module in modules:

# Convert Unix timestamp to datetime object
dt_object = datetime.utcfromtimestamp(module["timestamp"])

# Format the datetime object as 'YYYY-MM-DD'
formatted_date = dt_object.strftime('%Y-%m-%d')

module_id = module["id"]
url = base_url + module_id
url_element = ET.SubElement(urlset, "url")
loc_element = ET.SubElement(url_element, "loc")
lastmod_element = ET.SubElement(url_element, "lastmod")
lastmod_element.text = formatted_date
loc_element.text = url

# Convert the ElementTree to a string
sitemap = ET.tostring(urlset, encoding="utf-8", method="xml")

xmlstr = minidom.parseString(sitemap).toprettyxml(indent=" ")

# Write to a file
with open("json/sitemap.xml", "w") as f:
f.write(xmlstr)

print("Sitemap generated successfully.")

0 comments on commit 33706ce

Please sign in to comment.