-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenWeatherScraper.py
72 lines (61 loc) · 2.5 KB
/
openWeatherScraper.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
import dbinfo
import requests
import json
from sqlalchemy import create_engine, text, MetaData, Table, Column, String
import traceback
from datetime import datetime
engine = create_engine("mysql+mysqldb://{}:{}@{}:{}/{}".format(dbinfo.USER, dbinfo.PASSWORD, dbinfo.DB_URI, dbinfo.PORT, dbinfo.DB_NAME), echo=True)
with engine.begin() as connection:
connection.execute(text("USE dublinbikes"))
createCurrentWeatherTable = """CREATE TABLE IF NOT EXISTS current_weather (
dt BIGINT,
main_weather VARCHAR(256),
weather_desc VARCHAR(256),
icon_code VARCHAR(256),
sunrise BIGINT,
sunset BIGINT,
temp FLOAT,
visibility INT,
wind_speed FLOAT,
wind_deg INT,
timestamp BIGINT
)
"""
try:
connection.execute(text(createCurrentWeatherTable))
except Exception as e:
print(e)
def main():
try:
now = datetime.now()
timestamp = datetime.timestamp(now)
r = requests.get(dbinfo.WEATHER_URI, params={"lat":dbinfo.LAT, "lon":dbinfo.LON, "exclude":dbinfo.EXCLUDE, "appid":dbinfo.APP_ID})
api_to_db(r.text, timestamp)
except:
print(traceback.format_exc())
if connection is None:
return
def api_to_db(apiData, timestamp):
weather = json.loads(apiData)
with engine.begin() as connection:
weather_info = (
weather.get('current').get('dt'),
weather.get('current').get('weather')[0].get('main'),
weather.get('current').get('weather')[0].get('description'),
weather.get('current').get('weather')[0].get('icon'),
weather.get('current').get('sunrise'),
weather.get('current').get('sunset'),
weather.get('current').get('temp'),
weather.get('current').get('visibility'),
weather.get('current').get('wind_speed'),
weather.get('current').get('wind_deg'),
timestamp
)
try:
weather_insert_row = """INSERT INTO current_weather VALUES("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")"""
weather_insert_row = weather_insert_row % weather_info
connection.execute(text(weather_insert_row))
except Exception as e:
print(e)
return
main()