diff --git a/config.json b/config.json index a580efa..b819a1c 100644 --- a/config.json +++ b/config.json @@ -9,6 +9,13 @@ "api_hash": "", "bot_token": "" }, + "database": { + "user": null, + "password": null, + "host": "127.0.0.1", + "port": 27017, + "name": "tgposter" + }, "mode": { "post": true, "submit": true, diff --git a/modules/database.py b/modules/database.py new file mode 100644 index 0000000..49e0e58 --- /dev/null +++ b/modules/database.py @@ -0,0 +1,37 @@ +"""Module that provides all database columns and +creates geospatial index for col_applications""" + +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: + con_string = 'mongodb://{0}:{1}@{2}:{3}/{4}'.format( + db_config["user"], + db_config["password"], + db_config["host"], + db_config["port"], + db_config["name"] + ) +else: + con_string = 'mongodb://{0}:{1}/{2}'.format( + db_config["host"], + db_config["port"], + db_config["name"] + ) + +db_client = MongoClient(con_string) +db = db_client.get_database(name=db_config["name"]) + +collections = db.list_collection_names() + +for collection in ["sent", "banned", "submitted"]: + if not collection in collections: + db.create_collection(collection) + +col_sent = db.get_collection("sent") +col_banned = db.get_collection("banned") +col_submitted = db.get_collection("submitted") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 6e0d773..b3d20c8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ apscheduler~=3.10.0 pyrogram~=2.0.99 requests~=2.28.2 -psutil~=5.9.4 \ No newline at end of file +psutil~=5.9.4 +pymongo~=4.3.3 \ No newline at end of file