initial
This commit is contained in:
41
mongo.py
Normal file
41
mongo.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import datetime
|
||||
import pymongo
|
||||
import os
|
||||
|
||||
MONGO_USER = os.getenv("MONGO_USER", "mongo")
|
||||
MONGO_PASSWORD = os.getenv("MONGO_PASSWORD", "password")
|
||||
MONGO_HOST = os.getenv("MONGO_HOST", "localhost")
|
||||
|
||||
|
||||
class Mongo:
|
||||
def __init__(self):
|
||||
url = f"mongodb://{MONGO_USER}:{MONGO_PASSWORD}@{MONGO_HOST}:27017/"
|
||||
self.client: pymongo.MongoClient = pymongo.MongoClient(url)
|
||||
self.database = self.client.get_database("certupdater")
|
||||
self.hosts_collection.create_index([
|
||||
("host", 1)
|
||||
])
|
||||
|
||||
def __getitem__(self, item):
|
||||
return self.database.get_collection(item)
|
||||
|
||||
@property
|
||||
def hosts_collection(self):
|
||||
return self["hosts"]
|
||||
|
||||
@property
|
||||
def hosts(self):
|
||||
hosts = {}
|
||||
for host in self.hosts_collection.find({}):
|
||||
hosts[host["host"]] = host
|
||||
return hosts
|
||||
|
||||
def update_date(self, host: str):
|
||||
now = datetime.datetime.now()
|
||||
if self.hosts_collection.find_one({"host": host}):
|
||||
self.hosts_collection.update_one({"host": host}, {"$set": {"update_time": now, "expire_time": now + datetime.timedelta(days=90)}})
|
||||
else:
|
||||
self.hosts_collection.insert_one({"host": host, "update_time": now, "expire_time": now + datetime.timedelta(days=90)})
|
||||
|
||||
|
||||
mongo = Mongo()
|
||||
Reference in New Issue
Block a user