Skip to content

Commit efcfc8c

Browse files
authored
Create rain_alert_notification.py
1 parent 5044487 commit efcfc8c

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import json
2+
import requests
3+
import smtplib
4+
5+
input_city = str(input("enter city name : "))
6+
sender_email = str(input("enter sender's email ID : "))
7+
sender_pwd = str(input("enter sender's email password : "))
8+
receiver_email = str(input("enter receiver's email ID : "))
9+
API_key = str(input("enter the API key : "))
10+
11+
input_city = input_city.lower()
12+
input_city_list = list(input_city)
13+
input_city_list[0] = input_city_list[0].upper()
14+
for i in range(1, len(input_city_list)):
15+
if(input_city_list[i-1]==' '):
16+
input_city_list[i] = input_city_list[i].upper()
17+
18+
input_city = ''.join(input_city_list)
19+
20+
json1_file = open('city_list.json', encoding="utf8")
21+
json1_str = json1_file.read()
22+
json1_data = json.loads(json1_str)
23+
24+
coord_city_list = []
25+
count_cities = 0
26+
message = ""
27+
for i in range(len(json1_data)):
28+
if(json1_data[i]['name']==input_city):
29+
count_cities = count_cities + 1
30+
coord_dict = json1_data[i]['coord']
31+
coord_list = []
32+
coord_list.append(coord_dict['lon'])
33+
coord_list.append(coord_dict['lat'])
34+
coord_city_list.append(coord_list)
35+
lat = coord_list[1]
36+
lon = coord_list[0]
37+
url = "https://api.openweathermap.org/data/2.5/onecall?lat=" + str(lat) + "&lon=" + str(lon) + "&exclude=current,minutely,daily,alerts&appid=" + API_key
38+
req = requests.get(url)
39+
req_text = req.text
40+
c = 0
41+
start_idx = 0
42+
while(c!=12):
43+
find_idx = req_text.find("main", start_idx)
44+
start_idx = find_idx+1
45+
c = c+1
46+
find_idx = req_text.find("main", start_idx)
47+
find_idx = find_idx+7
48+
weather_condition = ""
49+
while(req_text[find_idx]!=","):
50+
weather_condition = weather_condition + req_text[find_idx]
51+
find_idx = find_idx + 1
52+
weather_condition = weather_condition[0:len(weather_condition)-1]
53+
message = message + "the 12 hour weather forecast of city " + input_city + " with country initials " + json1_data[i]['country'] + " is : " + weather_condition.upper() + "." + "\n"
54+
55+
if(count_cities==0):
56+
message = "unfortunately we did not find your entered city name in our database. Please check for any spelling errors."
57+
58+
elif(count_cities==1):
59+
message = "we found " + str(count_cities) + " city with name " + input_city + "." + "\n" + message
60+
61+
elif(count_cities>1):
62+
message = "we found " + str(count_cities) + " cities with name " + input_city + "." + "\n" + message
63+
64+
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
65+
server.login(sender_email, sender_pwd)
66+
server.sendmail(sender_email, receiver_email, message)
67+
server.quit()
68+

0 commit comments

Comments
 (0)