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?
Python-Chatbot/weather_realtime.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
54 lines (47 sloc)
2.27 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 time | |
import requests | |
async def weather(client,message): | |
'''Code extracted and editted from: https://pypi.org/project/weather-api/ ''' | |
''' weather-api 1.0.6 ''' | |
'''Return real time weather to users. Need Internet access!!! ''' | |
#version1 | |
from weather import Weather, Unit | |
weather = Weather(unit=Unit.CELSIUS) | |
lookup = weather.lookup(560743) | |
condition = lookup.condition | |
msg="It is "+((condition.text)).lower()+" today." | |
await client.send_message(message.channel, msg) | |
await client.send_message(message.channel, "Specific weather details: ") | |
#version2 | |
#weather = Weather(unit=Unit.CELSIUS) | |
#location = weather.lookup_by_location('dublin') | |
#condition = location.condition | |
#print(condition.text) | |
while True: | |
try: | |
weather = Weather(unit=Unit.CELSIUS) | |
async client.send_message(message.channel,"Which place's weather do you want me to tell you? ") | |
@client.event | |
place=message.content | |
location = weather.lookup_by_location(place.strip()) | |
forecasts = location.forecast | |
except: | |
await client.send_message(message.channel, "Invalid location!") | |
else: | |
for forecast in forecasts: | |
msg=forecast.date+" will be "+((forecast.text).lower()).strip(),"." | |
await client.send_message(message.channel, msg) | |
msg=forecast.date+":"+forecast.low+"℃ to "+forecast.high+"℃." | |
await client.send_message(message.channel, msg) | |
msg="On this day in "+place.strip()+", you should \t" | |
await client.send_message(message.channel,msg) | |
if float(forecast.low)>25 or float(forecast.high)>30: | |
msg=msg+"drink more water and beware of heat stroke." | |
elif float(forecast.low)<0 or float(forecast.high)<13: | |
msg=msg+"wear more clothes before outing. Beware of cold temperature otherwise you'll get a cold." | |
else: | |
msg=msg+"do nothing but enjoy your day, because this is a normal day." | |
await client.edit_message(message.channel,msg) | |
break | |
'''Extracted code ends.''' |