Generate initial zasoby.csv file

This commit is contained in:
Marta Sienkiewicz 2026-01-11 17:40:57 +01:00
parent 1d8e18247d
commit 1b2ca8eb87
2 changed files with 31 additions and 6 deletions

View file

@ -3,6 +3,7 @@ Class to generate a csv file based on data fetched via Discourse REST API
''' '''
import requests import requests
import json import json
import csv
class Discourse(): class Discourse():
def __init__(self, url): def __init__(self, url):
@ -20,13 +21,17 @@ class Discourse():
res_json = json.loads(res.text) res_json = json.loads(res.text)
return res_json return res_json
def get_category_topics(self, category_id: str) -> requests.Response: def category_topics_csv(self, category_id: str) -> requests.Response:
"""Get topics from a Discourse category""" """Save category topics to a csv file"""
cat_data = self.get_category_data(category_id) cat_data = self.get_category_data(category_id)
columns = ["id", "title", "tags", "url"]
with open('zasoby.csv', 'w', encoding='UTF8') as f:
write = csv.writer(f)
write.writerow(columns)
for topic in cat_data["topic_list"]["topics"]: for topic in cat_data["topic_list"]["topics"]:
print(f'{topic["id"]}, {topic["title"]}, {topic["tags"]}, {self.url}t/{topic["id"]}') write.writerow([topic["id"], topic["title"], topic["tags"], f'{self.url}t/{topic["id"]}'])
if __name__=="__main__": if __name__=="__main__":
dis = Discourse("https://kb.hs3.pl/") dis = Discourse("https://kb.hs3.pl/")
dis.get_category_topics(9) dis.category_topics_csv(9)

View file

@ -0,0 +1,20 @@
id,title,tags,url
45,Jak stworzyć nowy wpis do bazy zasobów Hackerspace Trójmiasto?,[],https://kb.hs3.pl/t/45
20,O kategorii: Baza Wiedzy Hackerspace'u,[],https://kb.hs3.pl/t/20
108,PC Engines APU2 Router Box,"['cow-work', 'networking']",https://kb.hs3.pl/t/108
61,HS3 BOFH,"['cow-work', 'garage', 'events', 'bofh']",https://kb.hs3.pl/t/61
93,King Bob,['cow-work'],https://kb.hs3.pl/t/93
92,Drukarka 3D Creality K1 Max,"['tools', 'workshop', '3d-print']",https://kb.hs3.pl/t/92
91,Drukarka 3D Creality Ender 3,"['tools', 'workshop', '3d-print']",https://kb.hs3.pl/t/91
90,Chciejlista,[],https://kb.hs3.pl/t/90
85,Komu powinien służyć Spejs,[],https://kb.hs3.pl/t/85
84,Budżet,[],https://kb.hs3.pl/t/84
83,Hackerspace Dragon Dreaming,[],https://kb.hs3.pl/t/83
82,Biblioteka,"['cow-work', 'books']",https://kb.hs3.pl/t/82
66,Apteczki,"['cow-work', 'garage', 'bhp']",https://kb.hs3.pl/t/66
44,Brayton Power,"['garage', 'projects']",https://kb.hs3.pl/t/44
52,Evil Submarine,"['cow-work', 'projects']",https://kb.hs3.pl/t/52
50,Infinity mirror (duże),"['garage', 'projects']",https://kb.hs3.pl/t/50
47,Cricut Maker 3 ploter tnący,"['tools', 'workshop']",https://kb.hs3.pl/t/47
41,Wiertarka PSB 500 RE BOSCH,"['garage', 'tools']",https://kb.hs3.pl/t/41
46,What the Duck,"['cow-work', 'wled']",https://kb.hs3.pl/t/46
1 id title tags url
1 id title tags url
2 45 Jak stworzyć nowy wpis do bazy zasobów Hackerspace Trójmiasto? [] https://kb.hs3.pl/t/45
3 20 O kategorii: Baza Wiedzy Hackerspace'u [] https://kb.hs3.pl/t/20
4 108 PC Engines APU2 Router Box ['cow-work', 'networking'] https://kb.hs3.pl/t/108
5 61 HS3 BOFH ['cow-work', 'garage', 'events', 'bofh'] https://kb.hs3.pl/t/61
6 93 King Bob ['cow-work'] https://kb.hs3.pl/t/93
7 92 Drukarka 3D Creality K1 Max ['tools', 'workshop', '3d-print'] https://kb.hs3.pl/t/92
8 91 Drukarka 3D Creality Ender 3 ['tools', 'workshop', '3d-print'] https://kb.hs3.pl/t/91
9 90 Chciejlista [] https://kb.hs3.pl/t/90
10 85 Komu powinien służyć Spejs [] https://kb.hs3.pl/t/85
11 84 Budżet [] https://kb.hs3.pl/t/84
12 83 Hackerspace Dragon Dreaming [] https://kb.hs3.pl/t/83
13 82 Biblioteka ['cow-work', 'books'] https://kb.hs3.pl/t/82
14 66 Apteczki ['cow-work', 'garage', 'bhp'] https://kb.hs3.pl/t/66
15 44 Brayton Power ['garage', 'projects'] https://kb.hs3.pl/t/44
16 52 Evil Submarine ['cow-work', 'projects'] https://kb.hs3.pl/t/52
17 50 Infinity mirror (duże) ['garage', 'projects'] https://kb.hs3.pl/t/50
18 47 Cricut Maker 3 ploter tnący ['tools', 'workshop'] https://kb.hs3.pl/t/47
19 41 Wiertarka PSB 500 RE BOSCH ['garage', 'tools'] https://kb.hs3.pl/t/41
20 46 What the Duck ['cow-work', 'wled'] https://kb.hs3.pl/t/46