-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrequest.py
90 lines (76 loc) · 2.27 KB
/
request.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
import pandas as pd
import numpy as np
import datetime as dt
from bs4 import BeautifulSoup
import datetime
import requests
import json
#Flask API URL
url = 'http://127.0.0.1:5000'
#For debugging purposes
date = datetime.datetime.now()
#Simplify Date
year = date.year
month = date.month
day = date.day
#Get avg_temp automatically
response = requests.get("https://api.wunderground.com/history/airport/KSFO/" + str(year) + "/" + str(month) + "/" + str(day) + "/DailyHistory.html")
if(response.ok):
data = response.text
soup = BeautifulSoup(data, features='lxml')
values = soup.find_all("span", class_="wx-value")
values = [x.get_text() for x in values]
avg_temp = values[0]
#Get day of week automatically
wd = date.weekday()
week = [0,0,0,0,0,0]
if wd == 1:
week[0] = 1
elif wd == 2:
week[1] = 1
elif wd == 3:
week[2] = 1
elif wd == 4:
week[3] = 1
elif wd == 5:
week[4] = 1
elif wd == 6:
week[5] = 1
#Loop for all PdDistricts
#Get NFL game? automatically
#Data
data = { 'avg_temp': int(avg_temp),
'Day': int(day),
'Month': int(month),
'Year': int(year),
'PdDistrict_CENTRAL': 0,
'PdDistrict_INGLESIDE': 0,
'PdDistrict_MISSION': 0,
'PdDistrict_NORTHERN': 0,
'PdDistrict_PARK': 0,
'PdDistrict_RICHMOND': 0,
'PdDistrict_SOUTHERN': 0,
'PdDistrict_TARAVAL': 0,
'PdDistrict_TENDERLOIN': 0,
'DayOfWeek_Monday': week[0],
'DayOfWeek_Saturday': week[4],
'DayOfWeek_Sunday': week[5],
'DayOfWeek_Thursday': week[3],
'DayOfWeek_Tuesday': week[1],
'DayOfWeek_Wednesday': week[2],
'NFL_Game_Day_1': 0,
}
#Preprocess Data
#data = pd.DataFrame(data, index=[0])
#data['Dates'] = pd.to_datetime(data['Dates'])
#data['Day'] = data['Dates'].dt.day
#data['Month'] = data['Dates'].dt.month
#data['Year'] = data['Dates'].dt.year
#data.drop(columns=['Dates'], inplace=True)
#data = pd.get_dummies(data, columns=["PdDistrict", "DayOfWeek", "NFL_Game_Day"], drop_first=True)
#data = data[['Day']]
#data = data.to_json()
data = json.dumps(data)
send_request = requests.post(url, data)
print(send_request)
print(send_request.json())