Skip to content

Commit f9be1b2

Browse files
authored
Merge pull request #41 from cseg-tech/cron
cron job functionality
2 parents e205741 + 88415b9 commit f9be1b2

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

app/__init__.py

Lines changed: 7 additions & 11 deletions
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

Lines changed: 32 additions & 9 deletions
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})
@@ -109,17 +131,18 @@ def getUIDComplaints(db, col, user_id):
109131
return "No Complaints Found"
110132

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

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

app/modules/NYCDBWrapper.py

Lines changed: 7 additions & 5 deletions
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)