Skip to content

Commit

Permalink
new module database
Browse files Browse the repository at this point in the history
  • Loading branch information
socraticDevBlog committed Jun 14, 2023
1 parent 07ff939 commit 2bccf93
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
3 changes: 3 additions & 0 deletions apis/pastebin/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,6 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# pastebin project stuff
*.db
83 changes: 83 additions & 0 deletions apis/pastebin/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import sqlite3
from sqlite3 import Connection
import logging

import base64, hashlib
import datetime, time

logging.basicConfig(format="%(process)d-%(levelname)s-%(message)s", level=logging.DEBUG)


class Paste:
def __init__(self, paste, badge: str = ""):
self.id = self._hash(paste)
self.paste = paste
self.badge = badge
self.insert_timestamp = time.mktime(datetime.datetime.now().timetuple())

def _hash(self, val):
encoded_val = val.encode("utf-8")
h = hashlib.md5(encoded_val)
ret = base64.urlsafe_b64encode(h.digest()[:6])
return bytes.decode(ret, "utf-8")


class DB:
def __init__(self, db_path: str = "default.db") -> None:
self._database = db_path
self._conn = self._create_connection(db_path)
self._create_pastebin_table()

def __del__(self):
if self._conn:
self._conn.close()

def insert_paste(self, paste: Paste) -> str:
sql = """ INSERT INTO pastebin(id,paste,insert_timestamp,badge)
VALUES(?,?,?,?) """
try:
self._conn.cursor()
self._conn.cursor().execute(
sql, (paste.id, paste.paste, paste.insert_timestamp, paste.badge)
)
self._conn.commit()
except sqlite3.Error as e:
logging.error(
"unable to insert new paste in sqlite3 database", exc_info=True
)

return paste.id

def _create_pastebin_table(self) -> None:
sql_create_pastebin_table = """ CREATE TABLE IF NOT EXISTS pastebin (
id text PRIMARY KEY,
paste BLOB,
insert_timestamp REAL,
badge TEXT
); """

try:
c = self._conn.cursor()
c.execute(sql_create_pastebin_table)
except sqlite3.Error as e:
logging.error("Error creating sqlite3 pastebin table", exc_info=True)

def _create_connection(self, db_file) -> Connection:
"""returns a database connection to a SQLite database"""
conn = None
try:
conn = sqlite3.connect(db_file)
print(sqlite3.version)
except sqlite3.Error as e:
logging.error("Error creating sqlite3 database connection", exc_info=True)

return conn


# very temporary: used to test the module locally
#
# if __name__ == "__main__":
# db = DB(r"pastebin.db")
# p = Paste(paste="zoom zoom", badge="mon ami")
# r = db.insert_paste(paste=p)
# print(r)
4 changes: 4 additions & 0 deletions apis/pastebin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

from fastapi import FastAPI

import logging

logging.basicConfig(format="%(process)d-%(levelname)s-%(message)s", level=logging.debug)

app = FastAPI()


Expand Down
3 changes: 2 additions & 1 deletion apis/pastebin/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
black
fastapi
uvicorn[standard]
uvicorn[standard]
db-sqlite3

0 comments on commit 2bccf93

Please sign in to comment.