FIX: Weather command now properly handles multiple-worded names, as well as unicode

pull/3/head
Kamal Curi 3 years ago
parent 1410f48b3a
commit 8ed458746a

@ -5,7 +5,7 @@
█ ▄ █ ▄▄▄█ ▄▄▄█ ▄ ██ █▄▄▄█ ▄▄▄█ █ ▄ █ ▄▄▄█ ▄▄▄█ ▄ ██ █▄▄▄█ ▄▄▄█
█ █ █ █ █▄▄▄█ █▄▄▄█ █▄█ █ █ █▄▄▄ █ █ █ █ █▄▄▄█ █▄▄▄█ █▄█ █ █ █▄▄▄
█▄█ █▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█ █▄█ █▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█
Neeble version: 2022.08.24a Neeble version: 2022.08.28
You can help the development of neeble in You can help the development of neeble in
https://github.com/KevinCaires/neeble https://github.com/KevinCaires/neeble
Thank you! Thank you!

@ -82,8 +82,7 @@ PERMISSIONS = {
OW_API_CONFIG = { OW_API_CONFIG = {
'api_id' : os.environ.get('OPENWEATHER_API_TOKEN', 'no'), 'api_id' : os.environ.get('OPENWEATHER_API_TOKEN', 'no'),
'gc_url' : 'http://api.openweathermap.org/geo/1.0/direct?q=<CITY>,<STATE>,<COUNTRY>&limit=1&appid=<API_ID>', 'wh_url' : 'https://api.openweathermap.org/data/2.5/weather?q=<CITY>&units=metric&appid=<API_ID>'
'wh_url' : 'https://api.openweathermap.org/data/2.5/weather?lat=<LAT>&lon=<LON>&units=metric&appid=<API_ID>'
} }
logger.dictConfig(LOGGING_CONFIG) logger.dictConfig(LOGGING_CONFIG)

@ -12,7 +12,7 @@ from utils.database import (count_quotes, get_by_id, get_quote_contains,
get_quotes, remove_quote, set_quote) get_quotes, remove_quote, set_quote)
from utils.machine_monitor import Monitor from utils.machine_monitor import Monitor
from utils.tools import kbytes_to_gbytes 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()) client = commands.Bot(command_prefix='--', intents=Intents.all())
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -191,38 +191,18 @@ async def weather(bot: object, *location: str) -> str:
""" """
Displays the weather information for a given place Displays the weather information for a given place
""" """
if OW_API_CONFIG['api_id'] == 'no': 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 \ 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") (The weather command hansn't been set up properly, make sure you have `OPENWEATHER_API_TOKEN` set up")
if location: if location:
location = str(location) location = ' '.join(location)
stripped = location.replace(" ", "") # Strips all whitespace location = location.encode('utf-8').decode('utf-8')
separated = stripped.split(',') # Splits between commas location = location.replace(" ", "+")
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 <city>, <state>, <ISO 3166 country code>`)")
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]
else: else:
city = "" location = "curitiba,paraná".encode('utf-8').decode('utf-8')
state = ""
country = ""
lat, lon = geocode(city, state, country) weatherdata = getweatherdata(location)
weatherdata = getweatherdata(lat, lon)
msg = displayweather(weatherdata) msg = displayweather(weatherdata)
default_msg = 'No data!' default_msg = 'No data!'
embed = Embed(type='rich') embed = Embed(type='rich')

@ -1,43 +1,22 @@
import json import json
import logging import logging
import urllib.request import requests
from collections import namedtuple from collections import namedtuple
from settings.config import OW_API_CONFIG from settings.config import OW_API_CONFIG
def getweatherdata(city:str):
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>', city)
ow_gc_url = ow_gc_url.replace('<STATE>', state)
ow_gc_url = ow_gc_url.replace('<COUNTRY', country)
ow_gc_url = ow_gc_url.replace('<API_ID>', 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):
""" """
Gets weather data based on coordinates Gets weather data based on coordinates
""" """
ow_wh_url = OW_API_CONFIG['wh_url'] ow_wh_url = OW_API_CONFIG['wh_url'].encode('utf-8').decode('utf-8')
ow_wh_url = ow_wh_url.replace('<LAT>', lat) ow_wh_url = ow_wh_url.replace('<CITY>', city)
ow_wh_url = ow_wh_url.replace('<LON>', lon)
ow_wh_url = ow_wh_url.replace('<API_ID>', OW_API_CONFIG['api_id']) ow_wh_url = ow_wh_url.replace('<API_ID>', 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 = requests.get(ow_wh_url).text
weatherdata = weatherdata.read().decode() weatherdata = weatherdata.encode('utf-8').decode('utf-8')
weatherdata = json.loads(str(weatherdata)) weatherdata = json.loads(weatherdata)
return weatherdata return weatherdata

Loading…
Cancel
Save