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?
ChatBot/attempt2weather
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
254 lines (206 sloc)
7.76 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 discord | |
import json | |
import urllib.request | |
from discord.ext import commands | |
bot = commands.Bot(command_prefix="$") | |
token = 'NTA2NDY5NDY5MDE5OTYzNDIy.DrimBg.YhS-b8QTmk_DWUmW8qsit_oYREM' | |
#First API for google wikipedia | |
#this function is for the API for wikipedia google search | |
def google (input): | |
url = 'https://en.wikipedia.org/wiki' | |
search_string = '/' | |
use_word = input | |
final1_string = url + search_string + use_word | |
return final1_string | |
#this comand uses the funcion above and it's suppose to give to the user the information for the the word that the user types | |
@bot.command() | |
async def google_sea(arg): | |
use_word = arg | |
location = arg | |
string22 = 'There is your result about ' + location + ' ' | |
try: | |
var1 = google(use_word) | |
finast = string22 + str(var1) | |
await bot.say(finast) | |
except: | |
await bot.say("error!Type again") | |
#Combination of the two API's | |
#the command below it takes the user's input and output the temperature of the town or city that the user types and also some extra inforamtion about the input | |
@bot.command() | |
async def google_temp(arg): | |
use_word = arg | |
location = arg | |
string22 = 'Also some information about ' + location + ' ' | |
try: | |
var1 = google(use_word) | |
finast = string22 + str(var1) | |
var3 = temp_url(location) | |
var4 = current_weather(var3) | |
string2 = str('The temperature in ' + location + ' is ' + str(var4)) | |
await bot.say(string2) | |
await bot.say(finast) | |
except: | |
await bot.say("error!Type again") | |
#the command below gives to the user the temperature in Fahrenheit and also some extra information for the user's input | |
@bot.command() | |
async def google_tempf(arg): | |
use_word = arg | |
location = arg | |
string22 = 'Also some information about ' + location + ' ' | |
try: | |
var1 = google(use_word) | |
finast = string22 + str(var1) | |
var3 = temp_url(location) | |
var4 = tempf(var3) | |
string2 = str('The temperature in Fahrenheit in ' + location + ' is ' + str(var4)) | |
await bot.say(string2) | |
await bot.say(finast) | |
except: | |
await bot.say("error!Type again") | |
#The command below gives the uv index and also some information for the user's input | |
@bot.command() | |
async def google_uv(arg): | |
use_word = arg | |
location = arg | |
string22 = 'Also some information about ' + location + ' ' | |
try: | |
var1 = google(use_word) | |
finast = string22 + str(var1) | |
var3 = temp_url(location) | |
var4 =uv_index_func(var3) | |
var5 = uv_warning(var4) | |
string2 = str('The uv index in ' + location + ' is ' + str(var4) + str(var5)) | |
await bot.say(string2) | |
await bot.say(finast) | |
except: | |
await bot.say("error!Type again") | |
#the command below gives to the user the wind and also some information about the user's input | |
@bot.command() | |
async def google_wind(arg): | |
use_word = arg | |
location = arg | |
string22 = 'Also some information about ' + location + ' ' | |
try: | |
var1 = google(use_word) | |
finast = string22 + str(var1) | |
var3 = temp_url(location) | |
var4 =wind_di(var3) | |
string2 = str('The wind in ' + location + ' is ' + str(var4) ) | |
await bot.say(string2) | |
await bot.say(finast) | |
except: | |
await bot.say("error!Type again") | |
#second API | |
#the function below it is suppose to take the the city/town that the user input's and gives the main url that the information will come from so that the bot can show | |
def temp_url(input: object) -> object: | |
main_url = 'http://api.apixu.com/v1/current.json' | |
api_key = '?key=dc42d220696a4172b65174631181611' | |
search_string = '&q=' | |
location = input | |
final_string = main_url + api_key + search_string + location | |
return final_string | |
#This function gives you the proper messages if the uv index is hight how you can protect yourself | |
#It takes the input and with an if statement it sees how hight is it and prints the message | |
def uv_warning(arg): | |
values = ' ' | |
UV_index = arg | |
uv3 = 'You need sunglasses' | |
uv5 = uv3 + ' and a hat and you need to stay in shade' | |
uv11 = uv5 + '. Stay indoors between 10am - 4am' | |
if UV_index >= 3 and UV_index < 5: | |
values = uv3 | |
if UV_index >= 5 and UV_index < 11: | |
values = uv5 | |
if UV_index >= 11: | |
values = uv11 | |
return values | |
@bot.command() | |
async def uv(arg): | |
location = arg | |
string22 = 'The UV index in ' + location + " is " | |
#in this part of the function the 'var1' takes the ciy name and puts it in the url | |
#after the 'var2' calls the function 'uv_index_func' with input the 'var1' so that it can collects the proper information from the url to print it to the user | |
#the same is happening for the above 'async' functions | |
try: | |
var1 = temp_url(location) | |
var2 = uv_index_func(var1) | |
var3 = uv_warning(var2) | |
final_string = string22 + str(var2) + var3 | |
await bot.say(final_string) | |
except: | |
await bot.say("This city doesn't exist!Try again") | |
#the two functions below they take the city/town/country that the user input and they take from the API dictionary the exact information that we want | |
def current_weather(input): | |
url = input | |
response = urllib.request.urlopen(url).read() | |
json_obj = str(response, 'utf-8') | |
data = json.loads(json_obj) | |
#if you want the entire dictonary | |
#final = data | |
final = (data['current']['temp_c']) | |
return final | |
def uv_index_func(input): | |
url = input | |
response = urllib.request.urlopen(url).read() | |
json_obj = str(response, 'utf-8') | |
data = json.loads(json_obj) | |
#if you want the entire dictonary | |
#final = data | |
final = (data['current']['uv']) | |
return final | |
#gives the temperature of the location that the user want's | |
@bot.command() | |
async def temp(arg): | |
location = arg | |
string22 = 'The temperature in ' + location + " is " | |
try: | |
var1 = temp_url(location) | |
var2 = current_weather(var1) | |
final_string = string22 + str(var2) | |
await bot.say(final_string) | |
#if the location that the user input does not exist it will come up with an error | |
except: | |
await bot.say("This city doesn't exist!Try again") | |
#gives the temperature in Fahrenheit of the location that the user want's | |
@bot.command() | |
async def ftemp(arg): | |
location = arg | |
string22 = 'The temperature in Fahrenheit in ' + location + " is " | |
try: | |
var1 = temp_url(location) | |
var2 = tempf(var1) | |
final_string = string22 + str(var2) | |
await bot.say(final_string) | |
# if the location that the user input does not exist it will come up with an error | |
except: | |
await bot.say("This city doesn't exist!Try again") | |
def tempf(input): | |
url = input | |
responce = urllib.request.urlopen(url).read() | |
json_obj = str(responce,'utf-8') | |
data = json.loads(json_obj) | |
final = (data['current']['temp_f']) | |
return final | |
#gives the wind of the location that the user want's | |
@bot.command() | |
async def wind(arg): | |
location = arg | |
string22 = 'The wind in ' + location + " is " | |
try: | |
var1 = temp_url(location) | |
var2 = wind_di(var1) | |
final_string = string22 + str(var2) | |
await bot.say(final_string) | |
except: | |
await bot.say("This city doesn't exist!Try again") | |
def wind_di(input): | |
url= input | |
responce = urllib.request.urlopen(url).read() | |
json_obj = str(responce, 'utf-8') | |
data = json.loads(json_obj) | |
final = (data['current']['wind_mph']) | |
return final | |
@bot.event | |
async def on_ready(): | |
print('ready') | |
bot.run(token) |