Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
WeatherBot/Process.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
292 lines (233 sloc)
10.1 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import WeatherAPI #Used for calling function from API | |
from datetime import * | |
import discord | |
from discord.ext import commands | |
bot = commands.Bot(command_prefix="!") | |
from dotenv import load_dotenv | |
load_dotenv() | |
#get the message from the bot and the city detect rain cloud and | |
def getweather(location, keyword, day): #Takes arguments from discord message and uses in function | |
if day != "none": | |
getForecast(day, location, keyword) | |
call = WeatherAPI.Weather.callWeatherAPI(location) #Shortens the call for easier writing | |
print(call) #Debug purposes see the whole call | |
clouds = call['weather'][0]['description'] #Focuses on the section of the json file pertaining to clouds | |
temp = call['main']['temp'] #Focuses on the section of the json pertaining to temperature | |
temp = temp - 273.15 #Temperature rounding as default from call is kelvin | |
temp = round(temp,1) | |
fahr = (temp*(9/5))+32 | |
fahr = round(fahr,1) | |
temp = str(temp) | |
fahr = str(fahr) | |
visibility = str(call['visibility']) | |
humidity = str(call['main']['humidity']) | |
pressure = str(call['main']['pressure']) | |
wind = call['wind']['speed'] #Focuses on speciic section for wind | |
if keyword == "weather": #Returns specific information based on what the user requested | |
toReturn = "the temperature is **"+temp+" degrees celcius** in "+location+" and **"+clouds+" detected**" #variable used for formatting purposes | |
return(toReturn) | |
if keyword == "rain": | |
if call['weather'][0]['main'] == 'Rain': #uses this method to assure the string always makes sense | |
toReturn = "**it is raining** in "+location #If there is no rain the train doesnt exist in the json making this if statement work | |
return(toReturn) | |
else: | |
toReturn = "**it is not raining** in "+location | |
return(toReturn) | |
if keyword == "cloud": #If any mentions of the cloud or cloudy are picked up it will run this | |
toReturn = "**"+clouds+" detected** in "+location | |
return(toReturn) | |
if keyword == "forecast": | |
toReturn = "the temperature is **"+temp+" degrees celcius** in "+location | |
return(toReturn) | |
if keyword == "snow": | |
if "snow" in call: | |
toReturn = "**it is snowing** in "+ location | |
return(toReturn) | |
else: | |
toReturn = "**it is not snowing** in "+ location | |
return(toReturn) | |
if keyword == "sun": | |
toReturn = "**"+ clouds+ " detected** in "+ location | |
return(toReturn) | |
if keyword == "wind" or keyword == "storm": | |
if wind < 1: | |
toReturn = "there is minimal wind in "+ location | |
return(toReturn) | |
if wind >= 1 and wind < 3: #Wind checks speeds to respond slightly differently based on how windy it is | |
toReturn = "**there is light air** in " + location | |
return(toReturn) | |
if wind >=3 and wind < 7: | |
toReturn = "**there is a light breeze** in "+ location | |
return(toReturn) | |
if wind >=7 and wind < 24: | |
toReturn = "**there is a Moderate breeze** in"+ location | |
return(toReturn) | |
if wind >= 24: | |
toReturn = "**there are strong winds** in "+location | |
return(toReturn) | |
if keyword == "temp" or keyword=="hot" or keyword=="cold" or keyword=="warm": | |
toReturn = "the temperature is **"+temp+" degrees celcius**, or **"+fahr+" degrees fahrenheit** in "+location | |
return(toReturn) | |
if keyword == "humid": | |
toReturn = "the humidity percentage is **"+humidity+ "%**" | |
return(toReturn) | |
if keyword == "pressure": | |
toReturn = "the pressure is **"+pressure+"hPa**" | |
return(toReturn) | |
if keyword == "visibility": | |
toReturn = "The visibility is **"+visibility+"m**" | |
return(toReturn) | |
#-----RETRIEVE COMMAND----- | |
@bot.command(name="retrieve") #sets the name for the command | |
async def retrieveCommand(ctx): | |
if toReturn == "": | |
await ctx.author.send("There have been no weather requests for me to send to you! Please type out your request in the main channel and try again.") #if toReturn is empty, it sends the user this message. | |
else: | |
await ctx.author.send(toReturn) #takes the last forecast sent to the channel and sends it to the requester in a private message | |
# List of time keywords: | |
# ['today','morning','afternoon','evening','tonight','monday','tuesday','wednesday','thursday','friday','saturday','sunday','now'] | |
def getForecast(day,location,keyword): | |
#print(keyword) | |
# Code Done by Team member Amir Khan | |
call = WeatherAPI.Weather.callWeatherAPI(location) | |
lat = call['coord']['lat'] | |
lon = call['coord']['lon'] | |
callforcast = WeatherAPI.Weather.callForecastAPI(lat,lon) | |
# End of Code Done by Team member Amir Khan | |
# predictionNight = callforcast['daily'][0]['temp']['night'] | |
# predictionMorn = callforcast['daily'][0]['temp']['morn'] | |
# predictionEve = callforcast['daily'][0]['temp']['eve'] | |
# print(predictionNight) | |
# print(predictionMorn) | |
# print(predictionEve) | |
#try: | |
# points of the day | |
if day.lower() == "monday": | |
#predictionNight = callforcast['daily'][0][day]['temp']['night'] # This line throws an error | |
unixTemp = int(dateconversion(day)) # for getting rid of the 0 at the end | |
unixDate = str(unixTemp) | |
x = 0 | |
y = False | |
print(callforcast.values()) | |
newlist = list(callforcast.values()) | |
search = str(newlist) | |
print(newlist[5]) | |
for counter, x in enumerate(newlist): | |
print(counter, x) | |
print("working") | |
print(x) | |
print(unixDate) | |
return #predictionNight | |
# points of the day | |
elif keyword.lower() == "morning": | |
predictionMorn = callforcast['daily'][0]['temp']['morn'] | |
return predictionMorn | |
# points of the day | |
elif keyword.lower() == "evening" or keyword.lower() == "afternoon": | |
predictionEve = callforcast['daily'][0]['temp']['eve'] | |
return predictionEve | |
else: | |
print("Your request could not be processed") | |
print("Please use one of the following keywords: tonight, morning, evening/afternoon.") | |
#except: | |
print("Sorry an Error occurred.") | |
# forecast based on the Day | |
def getForecastDay(day,location,keyWord): | |
if day.lower() == "monday": | |
unixDate = dateconversion(day) | |
outForecast = getForecast(unixDate,location,keyWord) | |
return outForecast | |
elif day.lower() == "tuesday": | |
unixDate = dateconversion(day) | |
outForecast = getForecast(unixDate,location, keyWord) | |
return outForecast | |
elif day.lower() == "wednesday": | |
unixDate = dateconversion(day) | |
outForecast = getForecast(unixDate,location, keyWord) | |
return outForecast | |
elif day.lower() == "thursday": | |
unixDate = dateconversion(day) | |
outForecast = getForecast(unixDate,location, keyWord) | |
return outForecast | |
elif day.lower() == "friday": | |
unixDate = dateconversion(day) | |
outForecast = getForecast(unixDate,location, keyWord) | |
return outForecast | |
elif day.lower() == "saturday": | |
unixDate = dateconversion(day) | |
outForecast = getForecast(unixDate,location, keyWord) | |
return outForecast | |
elif day.lower() == "sunday": | |
unixDate = dateconversion(day) | |
outForecast = getForecast(unixDate,location, keyWord) | |
return outForecast | |
else: | |
print("Please use a valid Day") | |
# https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes | |
# datetime documentation | |
#from datetime import * | |
# https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes | |
# datetime documentation | |
def dateconversion(day): | |
if day == "monday": | |
day = 0 | |
elif day == 'tuesday': | |
day = 1 | |
elif day == 'wednesday': | |
day = 2 | |
elif day == 'thursday': | |
day = 3 | |
elif day == 'friday': | |
day = 4 | |
elif day == 'saturday': | |
day = 5 | |
elif day == 'sunday': | |
day = 6 | |
today = date.today()#Defines today | |
current = date.weekday(today) | |
count = 0 | |
while current != day: #figures out what day of the weak they are asking for and how many days that is in the future | |
current = current+1 | |
if current == 7: | |
current = 0 | |
count = count + 1 | |
daydate = int((datetime.strftime(date.today(), "%d"))) #gets day | |
month = int((datetime.strftime(date.today(), "%m"))) #gets month | |
year = int((datetime.strftime(date.today(), "%y"))) #gets year | |
realday = int(daydate) + count # adds on current day to what day user wants | |
months31 = [0,2,4,6,7,9,11] # list of months with 31 days | |
months30 = [3,5,8,10] # list of months with 30 days | |
check = year/4 #returns a value that could give a decimal | |
if check == int(year/4): #accounts for leap years hopefully | |
if month == 2: | |
if realday > 29: | |
realday = realday - 29 | |
month = month + 1 | |
if month in months30: #accounts for change of month | |
if realday > 30: | |
realday = realday - 30 | |
month = month + 1 | |
if month in months31: #accounts for change of month with 31 days | |
if realday > 31: | |
realday = realday - 30 | |
month = month + 1 | |
if month == 11: | |
year = year+1 | |
month = 1 | |
year = year + 2000 #converts year from last two digits to full year | |
dt = datetime(year, month, realday) # copied from https://www.tutorialspoint.com/How-to-convert-Python-date-to-Unix-timestamp#:~:text=You%20can%20use%20the%20datetime,epoch%20for%20that%20datetime%20object. | |
unixdate = dt.replace(tzinfo=timezone.utc).timestamp() | |
print(year, month, realday) | |
unixdate = unixdate + 61200 | |
return(unixdate) # end of copy | |
# Testing Lines | |
#outResult = getForecastDay("monday","Birmingham","tonight") | |
getweather("birmingham","forecast","monday") | |
#outResult = getForecast("london","evening") | |
#print(outResult) | |
#x = "Birmingham" | |
#y = "temperature" | |
#getweather(x, y) | |
#x = input("Where is the place") | |
#y = input("say keyword") | |
#getweather(x, y) |