-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
134 lines (100 loc) · 3.62 KB
/
app.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
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
133
134
import os
from yaml import load as yaml_load, FullLoader
from time import sleep
from pytz import timezone
from redis import Redis
from craigslist import CraigslistHousing
from twilio.rest import Client as TwilioClient
from attrdict import AttrDict
from datetime import datetime, timedelta
config = open("config.yml", "r")
config = yaml_load(config, Loader=FullLoader)
config = AttrDict(config)
redisClient = Redis(
host=os.environ.get("REDIS_HOST", config.redis.host),
port=os.environ.get("REDIS_PORT", config.redis.port),
db=0,
)
clistClient = CraigslistHousing(
site=config.craigslist.site,
area=config.craigslist.area,
filters=config.craigslist.filters,
)
twilioClient = TwilioClient(config.twilio.sid, config.twilio.token)
def getApartmentOptions():
results = []
try:
results = clistClient.get_results(
sort_by=config.craigslist.search.sort_by,
limit=config.craigslist.search.limit,
)
except:
print("Failed to fetch posts from Craigslist.")
return results
def parseApartment(apartment):
if redisClient.sismember(config.redis.key_names.seen, apartment["id"]):
return [None, None]
post_id = apartment["id"]
post_name = apartment["name"]
post_cost = apartment["price"]
post_loc = apartment["where"]
post_time = apartment["datetime"]
post_url = apartment["url"]
message = "-"
message += "\n\n" + post_name + "\n\n"
message += "price: %s\n\n" % post_cost
message += "location: %s\n\n" % post_loc
message += "posted: %s\n\n" % post_time
message += post_url
return [post_id, message]
def sendSms(to_number, apartment_id, message_body):
if not to_number or not apartment_id:
return
try:
print("Sending SMS to %s for ID: %s" % (to_number, apartment_id))
twilioClient.messages.create(
to=to_number, from_=config.twilio.from_number, body=message_body
)
redisClient.sadd(config.redis.key_names.seen, apartment_id)
except:
print("SMS delivery failed to %s for ID: %s" % (to_number, apartment_id))
def getCurrentTime():
tz = timezone(config.timing.timezone)
return datetime.now(tz)
def getCurrentHour():
return getCurrentTime().hour
def isBlackoutHours():
blackout_start, blackout_end = (
config.timing.blackout_start,
config.timing.blackout_end,
)
currentHour = getCurrentHour()
return currentHour >= blackout_start or currentHour < blackout_end
def hoursToSleep():
HOURS_IN_DAY = 24
currentHour = getCurrentHour()
if currentHour >= config.timing.blackout_start:
return HOURS_IN_DAY - currentHour + config.timing.blackout_end
else:
return config.timing.blackout_end - currentHour
def waitIntervalTime():
sleep_time_in_seconds = config.timing.interval * 60 * 60
sleeping_til = getCurrentTime() + timedelta(0, sleep_time_in_seconds)
print("sleeping til %s..." % sleeping_til.strftime("%H:%M:%S"))
sleep(sleep_time_in_seconds)
def main():
while True:
if isBlackoutHours():
print("We are in a blackour hour!")
print("Sleeping for %d hours..." % hoursToSleep())
sleep(hoursToSleep() * 60 * 60) # seconds
continue
apartments = getApartmentOptions()
for apartment in apartments:
apartment_id, message_body = parseApartment(apartment)
contacts = config.twilio.contacts
for contact in config.twilio.contacts:
number = config.twilio.contacts[contact]
sendSms(number, apartment_id, message_body)
waitIntervalTime()
main()