-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherGraph.py
146 lines (100 loc) · 4.57 KB
/
weatherGraph.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import requests
import datetime
import smtplib
from email.mime.text import MIMEText
import tkinter
mainwindow = tkinter.Tk()
mainwindow.title("Weather API")
mainwindow.resizable(0, 0)
mainwindow.configure(background='grey')
label = tkinter.Label(mainwindow, text="Enter City",background="orange",fg="Black")
label.grid(row=0, column=0,sticky= "NENSSESW")
cityValue = tkinter.StringVar()
e = tkinter.Entry(mainwindow,textvariable=cityValue)
e.grid(row=0, column=1,sticky= "NENSSESW")
button = tkinter.Button(mainwindow, text="Submit",background="Black",fg="White", command=lambda: weather_data())
button.grid(row=1, column=0, columnspan=2,sticky= "NENSSESW")
datalabel= tkinter.Label(mainwindow)
datalabel.grid(row=2, column=0,sticky= "NENSSESW",columnspan=2)
label_your_email=tkinter.Label(mainwindow,text="Enter your Email Id",background="Orange")
label_your_email.grid(row=3,column=0,sticky= "NENSSESW")
u_mail=tkinter.StringVar()
entry_your_email= tkinter.Entry(mainwindow,textvariable=u_mail)
entry_your_email.grid(row=3,column=1,sticky= "NENSSESW")
label_password=tkinter.Label(text="Enter your Password",background="orange")
label_password.grid(row=4,column=0,sticky= "NENSSESW")
u_pass= tkinter.StringVar()
enter_password=tkinter.Entry(mainwindow,textvariable=u_pass)
enter_password.grid(row=4,column=1,sticky= "NENSSESW")
label_sender_email=tkinter.Label(text="Enter Sendser Email:",background="orange")
label_sender_email.grid(row=5,column=0,sticky= "NENSSESW")
u_sendmail= tkinter.StringVar()
entry_sender_email=tkinter.Entry(mainwindow,textvariable=u_sendmail)
entry_sender_email.grid(row=5,column=1,sticky= "NENSSESW")
button_submit=tkinter.Button(mainwindow,background="Black",fg="White",text="Submit Report",command=lambda:email())
button_submit.grid(row=6,column=0,columnspan=2,sticky= "NENSSESW")
def weather_data():
city = str(e.get())
url = 'http://api.openweathermap.org/data/2.5/weather?appid=ce45a4d1079e68c410cd42a3054d00e1&q='
new_url = url + cityValue.get()
#print(new_url)
data = requests.get(new_url).json()
#print(data)
if(data['cod']=='404'or data['cod']=='400'):
print('City Not Found')
datalabel.configure(text='City Not Found')
return
longitude = data["coord"]["lon"]
latitude = data["coord"]["lat"]
weather_status = data["weather"][0]["description"]
wind_speed = data["wind"]["speed"]
sunrise_timestamp = data["sys"]["sunrise"]
sunrise_time = time_stamp(sunrise_timestamp)
sunset_timestamp = data["sys"]["sunset"]
sunset_time = time_stamp(sunset_timestamp)
pressure = data["main"]["pressure"]
date = data["dt"]
date_updated = time_stamp(date)
text = '''
weather Report :{1}
Latitude :{2}
longitude :{3}
Windspeed :{4}
SunRise :{5}
SunSet :{6}
Date :{7}
Pressure :{8}
Test Weather data :{9}
'''.format(city, weather_status, latitude, longitude, wind_speed, sunrise_time, sunset_time, date_updated, pressure,test("delhi"))
datalabel.configure(text = text)
return text
def test(cty):
url = 'http://api.openweathermap.org/data/2.5/weather?appid=ce45a4d1079e68c410cd42a3054d00e1&q='
new_url = url + cty
data = requests.get(new_url).json()
return data["weather"][0]["description"]
def time_stamp(number):
date_time = datetime.datetime.fromtimestamp(int(number)).strftime('%Y-%m-%d %H:%M:%S')
return date_time
def email():
u_mail=str(entry_your_email.get())
u_pass=str(enter_password.get())
u_sendmail=str(entry_sender_email.get())
# define content
recipients = u_sendmail #SENDER TO SEND
sender = u_mail # YOUR EMAIL
subject = "Weather update"
body = weather_data()
print("BODY is {}".format(body))
# make up message
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = ", ".join(recipients)
# sending
session = smtplib.SMTP('smtp.gmail.com', 587)
session.starttls()
session.login(u_mail, u_pass)
send_it = session.sendmail(sender, recipients, msg.as_string())
session.quit()
mainwindow.mainloop()