diff --git a/discourse.py b/discourse.py
index 1e5c73e..0f7957a 100644
--- a/discourse.py
+++ b/discourse.py
@@ -5,29 +5,45 @@ import requests
import json
import csv
-class Discourse():
- def __init__(self, url):
- self.url = url
- def get_category_data(self, category_id: str) -> requests.Response:
- """Get data related to a Discourse category"""
+DISCOURSE_URL = "https://kb.hs3.pl" # Database is hosted here
+CATEGORY_ID = 9 # Database category ID
+
+class DiscourseDatabase():
+ def __init__(self):
+ data = self.get_category_data()
+ self.category_topics_csv(data)
+
+ def get_category_data(self) -> requests.Response:
+ """Get data from a Discourse category"""
headers = {
"content-type": "application/json",
}
- url = f"{self.url}/c/{category_id}.json"
+ url = f"{DISCOURSE_URL}/c/{CATEGORY_ID}.json"
+ print(f"Fetching data from {url}")
res = requests.get(url, headers)
res.raise_for_status()
-
res_json = json.loads(res.text)
return res_json
- def category_topics_csv(self, category_id: str) -> requests.Response:
+ def category_topics_csv(self, category_data) -> None:
"""Save category topics to a csv file"""
- cat_data = self.get_category_data(category_id)
- columns = ["id", "title", "tags"]
+ columns = ["id", "title", "place", "tags"]
+ records = category_data["topic_list"]["topics"]
with open('zasoby.csv', 'w', encoding='UTF8') as f:
write = csv.writer(f)
write.writerow(columns)
- for topic in cat_data["topic_list"]["topics"]:
- html_url = f'{topic["title"]}'
- write.writerow([topic["id"], html_url, topic["tags"]])
+ for topic in records:
+ html_url = f'{topic["title"]}'
+ place = self.get_place(topic)
+ write.writerow([topic["id"], html_url, place, topic["tags"]])
+ print(f"New zasoby.csv generated with {len(records)} records")
+
+ def get_place(self, topic):
+ """Get place of a topic"""
+ places = ["cow-work", "garage", "workshop"]
+ for place in places:
+ if place in topic["tags"]:
+ return f'{place}'
+ return "unknown"
+
diff --git a/docs/index.html b/docs/index.html
index ce05618..5a8a454 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -29,6 +29,8 @@
title |
+ place |
+
tags |
@@ -41,6 +43,8 @@
Jak stworzyć nowy wpis do bazy zasobów Hackerspace Trójmiasto? |
+ unknown |
+
[] |
@@ -51,6 +55,8 @@
O kategorii: Baza Wiedzy Hackerspace'u |
+ unknown |
+
[] |
@@ -61,6 +67,8 @@
PC Engines APU2 Router Box |
+ cow-work |
+
['cow-work', 'networking'] |
@@ -71,6 +79,8 @@
HS3 BOFH |
+ cow-work |
+
['cow-work', 'garage', 'events', 'bofh'] |
@@ -81,6 +91,8 @@
King Bob |
+ cow-work |
+
['cow-work'] |
@@ -91,6 +103,8 @@
Drukarka 3D Creality K1 Max |
+ workshop |
+
['tools', 'workshop', '3d-print'] |
@@ -101,6 +115,8 @@
Drukarka 3D Creality Ender 3 |
+ workshop |
+
['tools', 'workshop', '3d-print'] |
@@ -111,6 +127,8 @@
Chciejlista |
+ unknown |
+
[] |
@@ -121,6 +139,8 @@
Komu powinien służyć Spejs |
+ unknown |
+
[] |
@@ -131,6 +151,8 @@
Budżet |
+ unknown |
+
[] |
@@ -141,6 +163,8 @@
Hackerspace Dragon Dreaming |
+ unknown |
+
[] |
@@ -151,6 +175,8 @@
Biblioteka |
+ cow-work |
+
['cow-work', 'books'] |
@@ -161,6 +187,8 @@
Apteczki |
+ cow-work |
+
['cow-work', 'garage', 'bhp'] |
@@ -171,6 +199,8 @@
Brayton Power |
+ garage |
+
['garage', 'projects'] |
@@ -181,6 +211,8 @@
Evil Submarine |
+ cow-work |
+
['cow-work', 'projects'] |
@@ -191,6 +223,8 @@
Infinity mirror (duże) |
+ garage |
+
['garage', 'projects'] |
@@ -201,6 +235,8 @@
Cricut Maker 3 ploter tnący |
+ workshop |
+
['tools', 'workshop'] |
@@ -211,6 +247,8 @@
Wiertarka PSB 500 RE BOSCH |
+ garage |
+
['garage', 'tools'] |
@@ -221,6 +259,8 @@
What the Duck |
+ cow-work |
+
['cow-work', 'wled'] |
diff --git a/main.py b/main.py
index 892352f..f851c57 100644
--- a/main.py
+++ b/main.py
@@ -1,23 +1,19 @@
-DISCOURSE_URL = "https://kb.hs3.pl/" # Database is hosted here
-DISCOURSE_CATEGORY = 9 # Database is stored in this Discourse category
-
import os, shutil
from jinja2 import Environment, FileSystemLoader
import pandas as pd
-
-from discourse import Discourse
+from discourse import DiscourseDatabase
def generate_dashboard():
"""Generate dashboard from zasoby.csv file"""
+ print("Generating HTML dashboard")
website_folder = "docs"
data = pd.read_csv("zasoby.csv")
env = Environment(loader=FileSystemLoader("template"))
-
+ print("Removing old website files")
shutil.rmtree(f"./{website_folder}")
os.mkdir(f"./{website_folder}")
+ print("Creating a new website")
shutil.copytree("template/static", f"{website_folder}/static")
-
- print("Creating page to static file.")
template = env.get_template("_main_layout.html")
with open(f"{website_folder}/index.html", "w+", encoding="utf-8") as file:
header_row = data.columns.values.tolist()
@@ -27,10 +23,6 @@ def generate_dashboard():
if __name__ == "__main__":
- print(f"Discourse database: {DISCOURSE_URL}{DISCOURSE_CATEGORY}")
- print("Fetching database data to zasoby.csv")
- dis = Discourse(DISCOURSE_URL)
- dis.category_topics_csv(DISCOURSE_CATEGORY)
- print("Generating HTML dashboard")
+ DiscourseDatabase()
generate_dashboard()
print("Done!")
\ No newline at end of file
diff --git a/template/_base_template.html b/template/_base_template.html
index 10b53c2..6f050ac 100644
--- a/template/_base_template.html
+++ b/template/_base_template.html
@@ -18,7 +18,8 @@
{% block body %}{% endblock body %}