1
0

Initial commit

This commit is contained in:
2022-01-05 14:04:46 +01:00
commit 27e5342e82
12 changed files with 421 additions and 0 deletions

2
s9ywriter/__init__.py Normal file
View File

@@ -0,0 +1,2 @@
from .s9ywriter import S9YWriter
from .s9yentry import S9YEntry

14
s9ywriter/s9yentry.py Normal file
View File

@@ -0,0 +1,14 @@
class S9YEntry():
def __init__(self):
self.db_keys = ["id", "title", "timestamp", "body", "comments", "trackbacks", "extended", "exflag", "author", "authorid", "isdraft", "allow_comments", "last_modified", "moderate_comments"]
self.categories = []
self.tags = []
def get_db_insert_values(self):
values = []
for k in self.db_keys:
if hasattr(self, k):
values.append(getattr(self, k))
else:
values.append(None)
return values

75
s9ywriter/s9ywriter.py Normal file
View File

@@ -0,0 +1,75 @@
import sqlite3
from .s9yentry import S9YEntry
class S9YWriter():
def __init__(self, db_file: str):
self.db_file = db_file
self.db = sqlite3.connect(self.db_file)
def __del__(self):
self.db.close()
def add_entry(self, entry: S9YEntry):
keys = entry.db_keys[1:]
placeholders = ["?"] * len(keys)
data = entry.get_db_insert_values()[1:]
sql = "INSERT INTO entries (" + ", ".join(keys) + ") VALUES (" + ", ".join(placeholders) + ")"
#print(sql)
result = self.db.execute(sql, data)
entry_id = result.lastrowid
print(repr(result.lastrowid))
print(entry.title)
# Add permission
sql = "INSERT INTO entryproperties VALUES (?, ?, ?)"
self.db.execute(sql, [entry_id, "ep_access", "public"])
# TODO: Add permalink
# permalinks (permalink, entry_id, type, data)
# permalink = archives/YYYY-MM-DD-%title%.html
# %title% = sanitised, no Unicode, ü --> ue, etc.
# WORKAROUND: Edit your blog settings and change the permalink to let S9Y regenerate them
# then change back to desired value
for category in list(set(entry.categories)):
self.add_category(entry_id, category)
for tag in list(set(entry.tags)):
self.add_tag(entry_id, tag)
def commit(self):
self.db.commit()
def add_category(self, entry_id: int, category_name: str):
# Tables: category, entrycat, access
# Category: categoryid, category_name, "", "", 0, 0, 0, parentid, NULL, NULL
# entrycat: entryid, categoryid
print(f"{entry_id} - {category_name}")
sql = "SELECT categoryid FROM category WHERE category_name = ?"
result = self.db.execute(sql, [category_name])
cat = result.fetchone()
if cat:
category_id = cat[0]
else:
# Category does not yet exist, add it
sql = "INSERT INTO category (category_name) VALUES (?)"
result = self.db.execute(sql, [category_name])
category_id = result.lastrowid
# Add access permissions
# access: 0, category_id, "category", read
# access: 0, category_id, "category", write
sql = "INSERT INTO access VALUES (?, ?, ?, ?, ?)"
self.db.execute(sql, [0, category_id, "category", "read", ""])
self.db.execute(sql, [0, category_id, "category", "write", ""])
# Add permalink
sql = "INSERT INTO permalinks VALUES (?, ?, ?, ?)"
self.db.execute(sql, [f"categories/{category_name}", category_id, "category", None])
sql = "INSERT INTO entrycat VALUES (?, ?)"
self.db.execute(sql, [entry_id, category_id])
def add_tag(self, entry_id: int, tag: str):
# Table: entrytags (entryid, tag)
sql = "INSERT INTO entrytags VALUES (?, ?)"
self.db.execute(sql, (entry_id, tag))