-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeather_forecast_application.py
75 lines (59 loc) · 3.04 KB
/
Weather_forecast_application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#Weather Forecast Application - Python
#Necessary Modules
from prettytable import PrettyTable
import requests
import bs4 as bs
import datetime
import sys
import Balloon_tip as Bt
#Dictionary to store the Weather Details. Date as Keys and details as the value stored as a list.
weather_details = {}
countries = {1: 'India', 2: 'Japan', 3: 'USA', 4: 'UK', 5: 'Russia', 6: 'Japan', 7: 'France'}
#Current Date
curr_date = datetime.datetime.now().strftime("%d-%b-%Y")
date_1 = datetime.datetime.strptime(curr_date, "%d-%b-%Y")
#Using a browser Agent
headers = {"User-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"}
for i in countries:
print('{}. {}'.format(i, countries[i]))
#Selecting the Countries
while True:
nation = int(input('\nChoose any one of the above Countries: \n'))
if nation in countries.keys():
break
else:
print('\n**** PLEASE CHOOSE A VALID COUNTRY. ****')
for i in countries:
print('{}. {}'.format(i, countries[i]))
nation = countries[nation]
area = input('Enter a Location in {}: '.format(nation)).strip().lower()
time_period = int(input('Enter the No. of Days [1-14]: '))
time_period = 3 if time_period <= 0 or time_period > 14 else time_period
#Getting the webpage of the URL
url = 'https://www.timeanddate.com/weather/' + nation + '/' + area + '/ext'
response = requests.get(url, headers = headers)
html = response.text
#Extracting the necessary information
soup = bs.BeautifulSoup(html, 'lxml')
temp = soup.find('tbody')
#Looping and extracting the Information about weather.
for i in range(time_period):
end_date = date_1 + datetime.timedelta(days=i)
try:
temperature = temp.select_one("tr:nth-of-type(" + str(i+1) + ")").select_one("td:nth-of-type(2)").text
weather_details[end_date.strftime("%d-%b-%Y")] = [temperature]
weather_details[end_date.strftime("%d-%b-%Y")].append(temp.select_one("tr:nth-of-type(" + str(i+1) + ")").select_one("td:nth-of-type(3)").text)
weather_details[end_date.strftime("%d-%b-%Y")].append(temp.select_one("tr:nth-of-type(" + str(i+1) + ")").select_one("td:nth-of-type(4)").text)
except AttributeError:
print('Weather Forecast NOT AVAILABLE for this Location. Please Enter a popular location.')
sys.exit(8)
#Putting the data into a PrettyTable table
print('\n{}-Day Weather Forecast for {}, {}:'.format(time_period, area.title(), nation))
table = PrettyTable(['Date', 'Max/Min Temp.', 'Weather Conditions', 'Feels Like'])
for i in weather_details:
table.add_row([i, *weather_details[i]])
#Printing the final table
print(table)
#This Part of code is used to show a desktop notification. It only shows the current day weather for the choosen location.
Bt.balloon_tip('Today\'s Weather Update - ' + area.title() + ', ' + nation,'Min/Max Temp: {}\nWeather: {}\nFeels Like: {}'
.format(weather_details[curr_date][0], weather_details[curr_date][1],weather_details[curr_date][2]))