-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.py
More file actions
executable file
·132 lines (103 loc) · 4.25 KB
/
weather.py
File metadata and controls
executable file
·132 lines (103 loc) · 4.25 KB
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
import urllib2
import json
import pandas as pd
import datetime as dt
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from subprocess import call
import smtplib
import os
import glob
import sys
cities_interests = ['NY/New_York','DC/Washington','UK/Liverpool','GA/Alpharetta','NJ/Toms_River','AU/Sydney']
daily_reads = []
for city in cities_interests:
f = urllib2.urlopen('http://api.wunderground.com/api/ac91bb3fd50992d4/forecast10day/q/'+city+'.json')
json_string = f.read()
parsed_json = json.loads(json_string)
for day in parsed_json['forecast']['simpleforecast']['forecastday']:
daily_reads.append({'date':str(day['date']['year'])+'-'+str(day['date']['month']).zfill(2)+'-'+str(day['date']['day']).zfill(2),
'high_f': day['high']['fahrenheit'],
'high_c': day['high']['celsius'],
'low_f': day['low']['fahrenheit'],
'low_c': day['low']['celsius'],
'rain_in' : day['qpf_allday']['in'],
'rain_mm' : day['qpf_allday']['mm'],
'As_of_date':dt.datetime.now().strftime('%Y-%m-%d'),
'city': city.split('/')[1]})
dailyread_df = pd.DataFrame(daily_reads)
message = 'City '.ljust(10)+' High '+'Low '+ 'Rain '+'\n'
for i in range(0,len(dailyread_df)):
if dailyread_df['date'][i] == dt.datetime.now().strftime('%Y-%m-%d'):
message += dailyread_df['city'][i].ljust(10)+' '+dailyread_df['high_f'][i].ljust(6)+dailyread_df['low_f'][i].ljust(4)+str(dailyread_df['rain_in'][i])+'\n'+'\n'
message += '\n'+'--------- Metric-------'+'\n'
message += 'City '.ljust(10)+' High '+'Low '+ 'Rain '+'\n'
for i in range(0,len(dailyread_df)):
if dailyread_df['date'][i] == dt.datetime.now().strftime('%Y-%m-%d'):
message += dailyread_df['city'][i].ljust(10)+' '+dailyread_df['high_c'][i].ljust(6)+dailyread_df['low_c'][i].ljust(4)+str(dailyread_df['rain_mm'][i])+'\n'+'\n'
# Run the External Plotting Portion
dailyread_df.to_csv('weather_daily.csv',headers=False)
os.system('cat weather_daily.csv >> vintage_weather.csv')
os.system('R CMD BATCH weather_plots.R')
pics = glob.glob('*.png')
fromaddr = str(sys.argv[1])+'gmail.com'
toaddr = str(sys.argv[3])
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = dt.datetime.now().strftime('%d-%m-%Y')+' Weather'
body = message
msg.attach(MIMEText(body, 'plain'))
msg.attach(MIMEImage(file(pics[0]).read()))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(str(sys.argv[1]), str(sys.argv[2]))
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
os.remove(pics[0])
### Now adding the data to the database
import psycopg2
for city in daily_read:
con = psycopg2.connect(database='weather', user='root')
cur = con.cursor()
city['id'] = city['date']+city['As_of_date']+city['city']
fields = ', '.join(city.keys())
values = []
for s in range(0,len(city.keys())):
values.append('%s')
query = 'INSERT INTO weather (%s) VALUES (' % (fields)+' ,'.join(values)+');'
try:
cur.execute(query,tuple(city.values()))
except:
pass
con.commit()
con.close()
### Now create the .json for the webapp
import pandas as pd
from datetime import datetime as dt
weather = pd.read_csv('weather_daily.csv')
weather = pd.read_csv('weather_daily.csv')
weather = weather[['city','date','high_f']].drop_duplicates()
nycd = {}
cities = weather['city'].drop_duplicates()
for city in cities:
nycd[city] = list(weather['high_f'][weather['city']==city])
nycd['dates'] = []
for date in list(weather['date'].drop_duplicates()[0:10]):
nycd['dates'].append(dt.strptime(date, "%Y-%m-%d"))
nycd_df = pd.DataFrame(nycd)
nycd_df.index = nycd_df.dates
nycd_df = nycd_df.drop('dates',1)
import vincent
#vincent.core.initialize_notebook()
line = vincent.Line(nycd_df)
line.axis_titles(x='Date', y='Tempature F')
line.legend(title='Cities')
line.scales[1].domain_max = max(nycd_df.max())+10
line.scales[1].domain_min = min(nycd_df.min())-10
line.scales[1].grammar()
line.scales[1].zero = False
line.to_json('/var/www/html/weather.json')