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

60 lines
1.7 KiB
Python
Raw Permalink Normal View History

2023-01-03 16:12:46 +02:00
"""Module that provides all database columns and
creates geospatial index for col_applications"""
2022-12-31 00:29:17 +02:00
from pymongo import MongoClient, GEOSPHERE
2022-12-10 12:37:15 +02:00
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 17:25:06 +02:00
con_string = "mongodb://{0}:{1}@{2}:{3}/{4}".format(
2022-12-10 12:37:15 +02:00
db_config["user"],
db_config["password"],
db_config["host"],
db_config["port"],
2023-03-09 17:25:06 +02:00
db_config["name"],
2022-12-10 12:37:15 +02:00
)
else:
2023-03-09 17:25:06 +02:00
con_string = "mongodb://{0}:{1}/{2}".format(
db_config["host"], db_config["port"], db_config["name"]
2022-12-10 12:37:15 +02:00
)
db_client = MongoClient(con_string)
db = db_client.get_database(name=db_config["name"])
collections = db.list_collection_names()
2023-03-09 17:25:06 +02:00
for collection in [
"tmp",
"bans",
"users",
"context",
"youtube",
"spoilers",
"messages",
"warnings",
"applications",
"sponsorships",
2023-04-06 17:08:33 +03:00
"analytics_group",
"analytics_users"
2023-03-09 17:25:06 +02:00
]:
2022-12-10 12:37:15 +02:00
if not collection in collections:
db.create_collection(collection)
2022-12-14 14:57:29 +02:00
col_tmp = db.get_collection("tmp")
2023-01-30 12:01:52 +02:00
col_bans = db.get_collection("bans")
2022-12-10 12:37:15 +02:00
col_users = db.get_collection("users")
col_context = db.get_collection("context")
col_youtube = db.get_collection("youtube")
2023-01-04 20:58:54 +02:00
col_spoilers = db.get_collection("spoilers")
2022-12-10 12:37:15 +02:00
col_messages = db.get_collection("messages")
col_warnings = db.get_collection("warnings")
2022-12-10 16:52:07 +02:00
col_applications = db.get_collection("applications")
2022-12-28 19:56:13 +02:00
col_sponsorships = db.get_collection("sponsorships")
2023-04-06 17:08:33 +03:00
col_analytics_group = db.get_collection("analytics_group")
col_analytics_users = db.get_collection("analytics_users")
2022-12-28 19:56:13 +02:00
2023-03-09 17:25:06 +02:00
col_applications.create_index([("application.3.location", GEOSPHERE)])