Skip to content

Commit 6f8fb86

Browse files
committed
2 parents f1b0b6c + f9be1b2 commit 6f8fb86

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

app/__init__.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Imported packages
1010
from sendgrid.helpers.mail import Mail
1111
from pymongo import MongoClient
12-
from apscheduler.schedulers.blocking import BlockingScheduler
12+
from apscheduler.schedulers.background import BackgroundScheduler
1313
from sendgrid import SendGridAPIClient
1414

1515
# Custom modules
@@ -37,21 +37,16 @@
3737
#get sendgrid key
3838
key = Credential.get_sg_key()
3939

40-
'''
40+
4141
sg_key = Credential.get_sg_key()
4242

4343
def cron_job():
4444
# Query each user, find out if anyone else has lodged complaints for thier BBL - if so, email them.
45-
emailNeeded = True
46-
if(emailNeeded):
47-
print("sending...")
48-
49-
content = "Hi"
50-
send_mail(sg_key, to, content)
51-
52-
scheduler = BlockingScheduler()
45+
MongoHelper.iterate_all_users(db, col)
46+
47+
scheduler = BackgroundScheduler()
5348
scheduler.add_job(cron_job, 'interval', hours=24)
54-
scheduler.start()'''
49+
scheduler.start()
5550
#Ref: https://stackoverflow.com/questions/22715086/scheduling-python-script-to-run-every-hour-accurately
5651

5752

@@ -192,3 +187,4 @@ def getURL():
192187
myKey = Credential.get_places_key()
193188
apiString = "https://maps.googleapis.com/maps/api/js?v=3.exp&key={keyVal}&sensor=false&libraries=places".format(keyVal=myKey)
194189
return apiString
190+

app/modules/MongoHelper.py

+32-9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ def init_Mongo():
3636
#get sendgrid key
3737
key = Credential.get_sg_key()
3838

39+
def iterate_all_users(db, col):
40+
41+
returnData = {}
42+
43+
for x in col.find():
44+
45+
complaints = getUIDNewComplaints(db, col, x)
46+
if complaints:
47+
open_complaints = complaints[0]
48+
closed_complaints = complaints[1]
49+
number = complaints[2]
50+
51+
emaillist = list(x['email'])
52+
53+
returnData["open_complaints"] = open_complaints
54+
returnData["closed_complaints"] = closed_complaints
55+
returnData["number"] = number
56+
57+
if returnData:
58+
Communications.send_email(key, emaillist, '311 Notification – New Complaint(s) Filed', returnData)
59+
60+
3961
#remove user from database (only for unit testing purposes)
4062
def DB_remove_user(col, email):
4163
cursor = col.find({'email': email})
@@ -110,17 +132,18 @@ def getUIDComplaints(db, col, user_id):
110132
return "No Complaints Found"
111133

112134
#get complaints for a given user based on the current date
113-
def getUIDNewComplaints(db, col, user_id):
114-
cursor = col.find({'id' : user_id})
135+
def getUIDNewComplaints(db, col, doc):
115136
start_date = datetime.datetime.now()
116137
end_date = datetime.datetime.now() + datetime.timedelta(1)
117-
start_date = start_date.strftime("%m/%d/%Y")
118-
end_date = end_date.strftime("%m/%d/%Y")
119-
for x in cursor:
120-
bbl = x['bbl']
121-
result = NYCDBWrapper.findNewComplaints(bbl, start_date, end_date)
122-
return result
123-
return "No Complaints Found"
138+
#start_date = start_date.strftime("%m/%d/%Y")
139+
#end_date = end_date.strftime("%m/%d/%Y")
140+
141+
bbl = str(doc['bbl'])
142+
print(bbl)
143+
result = NYCDBWrapper.findDailyComplaints(bbl)
144+
return result
145+
146+
#return "No Complaints Found"
124147

125148
#get address for a given user ID
126149
def getAddress(db, col, UID):

app/modules/NYCDBWrapper.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ def findDailyComplaints(bbl):
4444
findNewComplaints(bbl, start_date, end_date)
4545

4646
def findNewComplaints(bbl, start_date, end_date):
47-
token = Credential.get_nycdb_token()
48-
url = "https://data.cityofnewyork.us/resource/erm2-nwe9.json?$$app_token={}&&$where=created_date%20between%20%27{}%27%20and%20%27{}%27&&bbl={}".format(token,start_date, end_date, bbl)
49-
with urllib.request.urlopen(url) as r:
50-
data = json.loads(r.read().decode(r.info().get_param('charset') or 'utf-8'))
51-
return cleanComplaints(data)
47+
token = Credential.get_nycdb_token()
48+
url = "https://data.cityofnewyork.us/resource/erm2-nwe9.json?$$app_token={}&&$where=created_date%20between%20%27{}%27%20and%20%27{}%27&&bbl={}".format(token,start_date, end_date, bbl)
49+
print(url)
50+
with urllib.request.urlopen(url) as r:
51+
data = json.loads(r.read().decode(r.info().get_param('charset') or 'utf-8'))
52+
53+
return cleanComplaints(data)
5254

5355
def getBBL(building, street, borough):
5456
appID = Credential.get_nyc_appID()

0 commit comments

Comments
 (0)