-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_knowledgebase.py
More file actions
104 lines (83 loc) · 3.34 KB
/
Copy pathsync_knowledgebase.py
File metadata and controls
104 lines (83 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import urllib.request
import json
import ssl
import os
import re
import sys
from markdownify import markdownify as md
# Setup SSL context to ignore cert errors (as seen in existing scripts)
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
TOKEN = os.getenv("ZAMMAD_API_TOKEN")
if not TOKEN:
print("Error: ZAMMAD_API_TOKEN environment variable is not set.")
sys.exit(1)
BASE_URL = "https://support.10bitworks.org/api/v1"
OUTPUT_DIR = "./knowledgebase"
headers = {
"Authorization": f"Token token={TOKEN}",
"Content-Type": "application/json"
}
def fetch_json(url):
req = urllib.request.Request(url, headers=headers)
with urllib.request.urlopen(req, context=ctx) as response:
return json.loads(response.read().decode('utf-8'))
def sanitize_filename(filename):
return re.sub(r'[^A-Za-z0-9._-]', '_', filename)
def main():
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
print("Fetching Knowledge Base 1...")
kb_url = f"{BASE_URL}/knowledge_bases/1?include_contents=1&full=1&expand=true"
try:
kb_data = fetch_json(kb_url)
except Exception as e:
print(f"Error fetching KB: {e}")
return
answer_ids = kb_data.get('answer_ids', [])
if not answer_ids:
print("No answer IDs found.")
return
for aid in answer_ids:
print(f"Fetching Answer ID: {aid}...")
answer_url = f"{BASE_URL}/knowledge_bases/1/answers/{aid}?include_contents={aid}"
try:
answer_data = fetch_json(answer_url)
assets = answer_data.get('assets', {})
# Extract Title
translations = assets.get('KnowledgeBaseAnswerTranslation', {})
title = "Unknown_Title"
if translations:
first_key = list(translations.keys())[0]
title = translations[first_key].get('title', "Unknown_Title")
# Extract Category
categories = assets.get('KnowledgeBaseCategoryTranslation', {})
category = "Uncategorized"
if categories:
first_key = list(categories.keys())[0]
category = categories[first_key].get('title', "Uncategorized")
# Extract Body
contents = assets.get('KnowledgeBaseAnswerTranslationContent', {})
body_html = ""
if contents:
first_key = list(contents.keys())[0]
body_html = contents[first_key].get('body', "")
# Convert HTML to Markdown
body_md = md(body_html, heading_style="ATX")
safe_title = sanitize_filename(title)
if not safe_title or safe_title == "_":
safe_title = f"Answer_{aid}"
file_path = os.path.join(OUTPUT_DIR, f"{safe_title}.md")
with open(file_path, "w", encoding="utf-8") as f:
f.write("---\n")
f.write(f'title: "{title}"\n')
f.write(f'category: "{category}"\n')
f.write(f'answer_id: {aid}\n')
f.write("---\n\n")
f.write(body_md)
print(f"Saved {file_path}")
except Exception as e:
print(f"Error fetching answer {aid}: {e}")
if __name__ == "__main__":
main()