Geo update WIP
This commit is contained in:
@@ -4,25 +4,35 @@ from pyrogram.types import Message
|
||||
from pyrogram.client import Client
|
||||
from classes.holo_user import HoloUser
|
||||
from modules import custom_filters
|
||||
from modules.utils import configGet, should_quote
|
||||
from modules.utils import configGet, locale, should_quote, find_location
|
||||
from modules.database import col_applications
|
||||
from classes.errors.geo import PlaceNotFoundError
|
||||
|
||||
# Nearby command ===============================================================================================================
|
||||
@app.on_message(~ filters.scheduled & (custom_filters.allowed | custom_filters.admin) & (filters.private | (filters.chat(configGet("admin_group")) | filters.chat(configGet("destination_group")))) & filters.command(["nearby"], prefixes=["/"]))
|
||||
async def cmd_nearby(app: Client, msg: Message):
|
||||
|
||||
if len(msg.command) < 1:
|
||||
holo_user = HoloUser(msg.from_user)
|
||||
|
||||
if len(msg.command) < 2:
|
||||
application = col_applications.find_one({"user": msg.from_user})
|
||||
if application is None:
|
||||
await msg.reply_text("You have no application")
|
||||
await msg.reply_text(locale("nearby_user_empty", "message", locale=holo_user.locale))
|
||||
return
|
||||
location = application["application"]["3"]["loc"][0], application["application"]["3"]["loc"][1]
|
||||
else:
|
||||
# find location
|
||||
location = "result"
|
||||
try:
|
||||
location_coordinates = find_location(" ".join(msg.command[2:]))
|
||||
location = location_coordinates["lng"], location_coordinates["lat"]
|
||||
except PlaceNotFoundError:
|
||||
await msg.reply_text(locale("nearby_invalid", "message", locale=holo_user.locale), quote=should_quote(msg))
|
||||
return
|
||||
except Exception as exp:
|
||||
await msg.reply_text(locale("nearby_error", "message", locale=holo_user.locale), quote=should_quote(msg))
|
||||
return
|
||||
|
||||
output = []
|
||||
users_nearby = col_applications.find( {"application": {"loc": { "$within": { "$center": [[location], 5] } }}} )
|
||||
users_nearby = col_applications.find( {"application.loc": {"$near": { "$geometry": { "type": "Point", "coordinates": location }, "$maxDistance": 30000 }} } )
|
||||
|
||||
for user in users_nearby:
|
||||
output.append(user)
|
||||
|
@@ -1,4 +1,4 @@
|
||||
from pymongo import MongoClient
|
||||
from pymongo import MongoClient, GEO2D
|
||||
from ujson import loads
|
||||
|
||||
with open("config.json", "r", encoding="utf-8") as f:
|
||||
@@ -35,4 +35,6 @@ col_context = db.get_collection("context")
|
||||
col_messages = db.get_collection("messages")
|
||||
col_warnings = db.get_collection("warnings")
|
||||
col_applications = db.get_collection("applications")
|
||||
col_sponsorships = db.get_collection("sponsorships")
|
||||
col_sponsorships = db.get_collection("sponsorships")
|
||||
|
||||
col_applications.create_index([("application.3.loc", GEO2D)])
|
@@ -1,4 +1,5 @@
|
||||
from typing import Any, Union
|
||||
from requests import get
|
||||
from pyrogram.enums.chat_type import ChatType
|
||||
from pyrogram.types import User
|
||||
from pyrogram.client import Client
|
||||
@@ -10,6 +11,7 @@ from sys import exit
|
||||
from os import kill, listdir, sep
|
||||
from os import name as osname
|
||||
from traceback import print_exc
|
||||
from classes.errors.geo import PlaceNotFoundError
|
||||
|
||||
from modules.logging import logWrite
|
||||
|
||||
@@ -171,6 +173,24 @@ def all_locales(key: str, *args: str) -> list:
|
||||
|
||||
return output
|
||||
|
||||
def find_location(query: str) -> dict:
|
||||
"""Find location on geonames.org by query. Search is made with feature classes A and P.
|
||||
|
||||
### Args:
|
||||
* query (`str`): Some city/village/state name
|
||||
|
||||
### Raises:
|
||||
* PlaceNotFoundError: Exception is raised when API result is empty
|
||||
|
||||
### Returns:
|
||||
* `dict`: One instance of geonames response
|
||||
"""
|
||||
try:
|
||||
result = (get(f"http://api.geonames.org/searchJSON?q={query}&maxRows=1&countryBias=UA&lang=uk&orderby=relevance&featureClass=P&featureClass=A&username={configGet('username', 'geocoding')}")).json()
|
||||
return result["geonames"][0]
|
||||
except (ValueError, KeyError, IndexError):
|
||||
raise PlaceNotFoundError(query)
|
||||
|
||||
try:
|
||||
from psutil import Process
|
||||
except ModuleNotFoundError:
|
||||
|
Reference in New Issue
Block a user