This repository has been archived on 2024-10-10. You can view files and clone it, but cannot push or open issues or pull requests.
TelegramBot/classes/location.py
2023-08-27 22:43:16 +02:00

59 lines
1.4 KiB
Python

from dataclasses import dataclass
from bson import ObjectId
from classes.point import Point
from modules.database import col_locations
@dataclass
class Location:
__slots__ = (
"_id",
"id",
"name",
"location",
"country",
"timezone",
)
_id: ObjectId
id: int
name: str
location: Point
country: int
timezone: str
@classmethod
async def get(cls, id: int):
db_entry = await col_locations.find_one({"id": id})
if db_entry is None:
raise ValueError(f"No location with ID {id} found.")
db_entry["location"] = Point(*db_entry["location"]) # type: ignore
return cls(**db_entry)
@classmethod
async def find(cls, name: str):
db_entry = await col_locations.find_one({"name": {"$regex": name}})
if db_entry is None:
raise ValueError(f"No location with name {name} found.")
db_entry["location"] = Point(*db_entry["location"]) # type: ignore
return cls(**db_entry)
@classmethod
async def nearby(cls, lat: float, lon: float):
db_entry = await col_locations.find_one({"location": {"$near": [lon, lat]}})
if db_entry is None:
raise ValueError(f"No location near {lat}, {lon} found.")
db_entry["location"] = Point(*db_entry["location"]) # type: ignore
return cls(**db_entry)