This repository has been archived on 2024-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
TelegramPoster/modules/database.py

36 lines
1.0 KiB
Python
Raw Normal View History

2023-02-14 17:25:56 +02:00
"""Module that provides all database columns"""
2023-02-14 15:45:52 +02:00
from pymongo import MongoClient
from ujson import loads
with open("config.json", "r", encoding="utf-8") as f:
db_config = loads(f.read())["database"]
f.close()
if db_config["user"] is not None and db_config["password"] is not None:
2023-03-09 12:33:02 +02:00
con_string = "mongodb://{0}:{1}@{2}:{3}/{4}".format(
2023-02-14 15:45:52 +02:00
db_config["user"],
db_config["password"],
db_config["host"],
db_config["port"],
2023-03-09 12:33:02 +02:00
db_config["name"],
2023-02-14 15:45:52 +02:00
)
else:
2023-03-09 12:33:02 +02:00
con_string = "mongodb://{0}:{1}/{2}".format(
db_config["host"], db_config["port"], db_config["name"]
2023-02-14 15:45:52 +02:00
)
db_client = MongoClient(con_string)
db = db_client.get_database(name=db_config["name"])
collections = db.list_collection_names()
2023-02-15 15:06:06 +02:00
for collection in ["sent", "users", "banned", "submitted"]:
2023-02-14 15:45:52 +02:00
if not collection in collections:
db.create_collection(collection)
col_sent = db.get_collection("sent")
2023-02-15 15:06:06 +02:00
col_users = db.get_collection("users")
2023-02-14 15:45:52 +02:00
col_banned = db.get_collection("banned")
2023-03-09 12:33:02 +02:00
col_submitted = db.get_collection("submitted")