From 8ed458746ad4ef6957e922d6571bd01125de531d Mon Sep 17 00:00:00 2001 From: Kamal Curi Date: Sun, 28 Aug 2022 15:22:21 -0300 Subject: [PATCH] FIX: Weather command now properly handles multiple-worded names, as well as unicode --- motd | 2 +- settings/config.py | 3 +-- utils/commands.py | 36 ++++++++---------------------------- utils/weather.py | 37 ++++++++----------------------------- 4 files changed, 18 insertions(+), 60 deletions(-) diff --git a/motd b/motd index 8b34c6d..6238be2 100644 --- a/motd +++ b/motd @@ -5,7 +5,7 @@ █ ▄ █ ▄▄▄█ ▄▄▄█ ▄ ██ █▄▄▄█ ▄▄▄█ █ █ █ █ █▄▄▄█ █▄▄▄█ █▄█ █ █ █▄▄▄ █▄█ █▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█ -Neeble version: 2022.08.24a +Neeble version: 2022.08.28 You can help the development of neeble in https://github.com/KevinCaires/neeble Thank you! \ No newline at end of file diff --git a/settings/config.py b/settings/config.py index 849eb3f..55fe648 100644 --- a/settings/config.py +++ b/settings/config.py @@ -82,8 +82,7 @@ PERMISSIONS = { OW_API_CONFIG = { 'api_id' : os.environ.get('OPENWEATHER_API_TOKEN', 'no'), - 'gc_url' : 'http://api.openweathermap.org/geo/1.0/direct?q=,,&limit=1&appid=', - 'wh_url' : 'https://api.openweathermap.org/data/2.5/weather?lat=&lon=&units=metric&appid=' + 'wh_url' : 'https://api.openweathermap.org/data/2.5/weather?q=&units=metric&appid=' } logger.dictConfig(LOGGING_CONFIG) diff --git a/utils/commands.py b/utils/commands.py index 989df66..ca07fd0 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -12,7 +12,7 @@ from utils.database import (count_quotes, get_by_id, get_quote_contains, get_quotes, remove_quote, set_quote) from utils.machine_monitor import Monitor from utils.tools import kbytes_to_gbytes -from utils.weather import displayweather, geocode, getweatherdata +from utils.weather import displayweather, getweatherdata client = commands.Bot(command_prefix='--', intents=Intents.all()) logger = logging.getLogger(__name__) @@ -191,38 +191,18 @@ async def weather(bot: object, *location: str) -> str: """ Displays the weather information for a given place """ + if OW_API_CONFIG['api_id'] == 'no': return await bot.send("You haven't set up an API key! Make an user and set up an API key in https://openweathermap.org/\n \ (The weather command hansn't been set up properly, make sure you have `OPENWEATHER_API_TOKEN` set up") if location: - location = str(location) - stripped = location.replace(" ", "") # Strips all whitespace - separated = stripped.split(',') # Splits between commas - separated.pop(-1) - separated[0] = separated[0][1:len(separated[0])] # These two commands clean up input for the parser - - if len(separated) > 3: - return await bot.send("This command takes 3 parameters at most!\n \ - (Syntax: `--w , , `)") - if len(separated) == 1: - city = separated[0] - state = "" - country = "" - elif len(separated) == 2: - city = separated[0] - state = separated[1] - country = "" - else: - city = separated[0] - state = separated[1] - country = separated[2] + location = ' '.join(location) + location = location.encode('utf-8').decode('utf-8') + location = location.replace(" ", "+") else: - city = "" - state = "" - country = "" - - lat, lon = geocode(city, state, country) - weatherdata = getweatherdata(lat, lon) + location = "curitiba,paraná".encode('utf-8').decode('utf-8') + + weatherdata = getweatherdata(location) msg = displayweather(weatherdata) default_msg = 'No data!' embed = Embed(type='rich') diff --git a/utils/weather.py b/utils/weather.py index 779c1ca..d197cde 100644 --- a/utils/weather.py +++ b/utils/weather.py @@ -1,43 +1,22 @@ import json import logging -import urllib.request +import requests from collections import namedtuple from settings.config import OW_API_CONFIG - -def geocode(city = 'curitiba', state = 'parana', country = 'BR') -> str: - """ - Converts city, state and country parameters into their coordinates - """ - if city == "" and state == "" and country == "": - city = 'curitiba' - state = 'parana' - country = 'BR' - ow_gc_url = OW_API_CONFIG['gc_url'] - ow_gc_url = ow_gc_url.replace('', city) - ow_gc_url = ow_gc_url.replace('', state) - ow_gc_url = ow_gc_url.replace('', OW_API_CONFIG['api_id']) - - placedata = urllib.request.urlopen(ow_gc_url) - placedata = placedata.read().decode() - placedata = json.loads(str(placedata)) - - return str(placedata[0]['lat']), str(placedata[0]['lon']) - -def getweatherdata(lat, lon): +def getweatherdata(city:str): """ Gets weather data based on coordinates """ - ow_wh_url = OW_API_CONFIG['wh_url'] - ow_wh_url = ow_wh_url.replace('', lat) - ow_wh_url = ow_wh_url.replace('', lon) + ow_wh_url = OW_API_CONFIG['wh_url'].encode('utf-8').decode('utf-8') + ow_wh_url = ow_wh_url.replace('', city) ow_wh_url = ow_wh_url.replace('', OW_API_CONFIG['api_id']) + ow_wh_url = ow_wh_url.encode('utf-8').decode('utf-8') - weatherdata = urllib.request.urlopen(ow_wh_url) - weatherdata = weatherdata.read().decode() - weatherdata = json.loads(str(weatherdata)) + weatherdata = requests.get(ow_wh_url).text + weatherdata = weatherdata.encode('utf-8').decode('utf-8') + weatherdata = json.loads(weatherdata) return weatherdata