mirror of
https://github.com/NovaOSS/nova-api.git
synced 2024-11-25 18:33:57 +01:00
Update keys.py
This commit is contained in:
parent
5bf7d9fc5d
commit
6ca84147f2
44
api/keys.py
44
api/keys.py
|
@ -1,39 +1,34 @@
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import sqlite3
|
from pymongo import MongoClient
|
||||||
|
|
||||||
with open('./config.json', 'r') as file:
|
with open('./config.json', 'r') as file:
|
||||||
config = json.load(file)
|
config = json.load(file)
|
||||||
|
|
||||||
class Keys:
|
class Keys:
|
||||||
# --- START OF CONFIG ---
|
# --- START OF CONFIG ---
|
||||||
|
MONGO_URI = os.getenv('MONGO_URI') or config.get('MONGO_URI')
|
||||||
DB_PATH = os.getenv('DB_PATH') or config.get('DB_PATH')
|
|
||||||
|
|
||||||
# --- END OF CONFIG ---
|
# --- END OF CONFIG ---
|
||||||
|
|
||||||
locked_keys = set()
|
locked_keys = set()
|
||||||
cache = set()
|
cache = set()
|
||||||
|
|
||||||
|
# Initialize MongoDB
|
||||||
|
client = MongoClient(MONGO_URI)
|
||||||
|
db = client.get_database('keys_db')
|
||||||
|
collection = db['keys']
|
||||||
|
|
||||||
def __init__(self, key: str):
|
def __init__(self, key: str):
|
||||||
self.key = key
|
self.key = key
|
||||||
if not Keys.cache:
|
if not Keys.cache:
|
||||||
self._load_keys()
|
self._load_keys()
|
||||||
|
|
||||||
def _connect(self):
|
|
||||||
conn = sqlite3.connect(self.DB_PATH)
|
|
||||||
return conn
|
|
||||||
|
|
||||||
def _load_keys(self) -> None:
|
def _load_keys(self) -> None:
|
||||||
conn = self._connect()
|
cursor = self.collection.find({}, {'_id': 0, 'key_value': 1})
|
||||||
cursor = conn.cursor()
|
for doc in cursor:
|
||||||
cursor.execute("SELECT key_value FROM keys")
|
key_value = doc['key_value']
|
||||||
rows = cursor.fetchall()
|
|
||||||
for row in rows:
|
|
||||||
key_value = row[0]
|
|
||||||
self.cache.add(key_value)
|
self.cache.add(key_value)
|
||||||
conn.commit()
|
|
||||||
|
|
||||||
def lock(self) -> None:
|
def lock(self) -> None:
|
||||||
self.locked_keys.add(self.key)
|
self.locked_keys.add(self.key)
|
||||||
|
@ -56,13 +51,9 @@ class Keys:
|
||||||
return key.key
|
return key.key
|
||||||
|
|
||||||
print("[WARN] No unlocked keys found in get keys request!")
|
print("[WARN] No unlocked keys found in get keys request!")
|
||||||
|
|
||||||
def delete(self) -> None:
|
|
||||||
conn = self._connect()
|
|
||||||
cursor = conn.cursor()
|
|
||||||
|
|
||||||
cursor.execute("DELETE FROM keys WHERE key_value=?", (self.key,))
|
def delete(self) -> None:
|
||||||
conn.commit()
|
self.collection.delete_one({'key_value': self.key})
|
||||||
# Update cache
|
# Update cache
|
||||||
try:
|
try:
|
||||||
Keys.cache.remove(self.key)
|
Keys.cache.remove(self.key)
|
||||||
|
@ -70,17 +61,12 @@ class Keys:
|
||||||
print("[WARN] Tried to remove a key from cache which was not present: " + self.key)
|
print("[WARN] Tried to remove a key from cache which was not present: " + self.key)
|
||||||
|
|
||||||
def save(self) -> None:
|
def save(self) -> None:
|
||||||
conn = self._connect()
|
self.collection.insert_one({'key_value': self.key})
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute("INSERT INTO keys (key_value) VALUES (?)", (self.key,))
|
|
||||||
conn.commit()
|
|
||||||
# Update cache
|
# Update cache
|
||||||
Keys.cache.add(self.key)
|
Keys.cache.add(self.key)
|
||||||
|
|
||||||
|
|
||||||
# Usage example:
|
# Usage example:
|
||||||
# os.environ['DB_PATH'] = "keys.db"
|
# os.environ['MONGO_URI'] = "mongodb://localhost:27017"
|
||||||
# key_instance = Keys("example_ky")
|
# key_instance = Keys("example_key")
|
||||||
# key_instance.save()
|
# key_instance.save()
|
||||||
# key_value = Keys.get()
|
# key_value = Keys.get()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue