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?
ChatBotGroup/weatherFunctions.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
79 lines (66 sloc)
2.81 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 urllib.request | |
import urllib.parse | |
import re | |
import asyncio | |
weatherList = ["cold", "hot", "weather", "typhoon", "forecast", "wind", "temperature"] | |
yes = ["yes"] | |
no = ["no"] | |
def weatherForecast(city): | |
try: | |
url = 'https://www.weather-forecast.com/locations/' + city + '/forecasts/latest' | |
headers = {} | |
headers['User-Agent'] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " \ | |
"Chrome/125.190.23.18 Safari/537.36" | |
req = urllib.request.Request(url, headers=headers) | |
resp = urllib.request.urlopen(req) | |
respData = resp.read() | |
temperature = re.findall(r'class="temp">\S+<', str(respData)) | |
weather_text = re.findall(r'"b-metar-table__weather-text">\D+<', str(respData)) | |
n = 0 | |
while n < 4: | |
t = temperature[n] | |
wt = weather_text[n] | |
if "Dry" in wt: | |
weather = "Dry" | |
elif "clowdy" or "Clowdy" in wt: | |
weather = "Clowdy" | |
elif "snow" in wt: | |
weather = "snowy" | |
elif "rain" or "Rain" in wt: | |
weather = "Rainy" | |
elif "Fog" or "fog" in wt: | |
weather = "Foggy" | |
elif "No report." in wt: | |
weather = "Something..." | |
if n == 0: | |
sentence0 = "The temperature and weather in different spots of " + city + " today will be: " | |
sentence1 = t[13:len(t) - 21] + "ºC, and " + weather | |
if n == 1: | |
sentence2 = t[13:len(t) - 21] + "ºC, and " + weather | |
elif n == 2: | |
sentence3 = t[13:len(t) - 21] + "ºC, and " + weather | |
elif n == 3: | |
sentence4 = t[13:len(t) - 21] + "ºC, and " + weather | |
return sentence0 + "\n" + sentence1 + "\n" + sentence2 + "\n" + sentence3 + "\n" + sentence4 | |
n = n + 1 | |
except Exception as error: | |
return "Sorry, but im afraid I don't know that city." | |
def weatherAnswer(text): | |
try: | |
if any(word in text for word in weatherList): | |
gV.flagWeather = 1 | |
return "Do you want to talk about the weather?" | |
elif gV.flagWeather == 1 and gV.flagWeatherA == 1: | |
gV.flagWeather = 0 | |
gV.flagWeatherA = 0 | |
return weatherForecast(text) | |
elif any(word in text for word in yes) and gV.flagWeather == 1: | |
gV.flagWeatherA = 1 | |
return "You want the weather forecast of which city?" | |
elif any(word in text for word in no) and gV.flagWeather == 1: | |
gV.flagWeather = 0 | |
return "Ok then." | |
else: | |
return "I didn't understand." | |
except: | |
return "Something went wrong." |