-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_craigslist_posts_(hourly).py
82 lines (68 loc) · 2.18 KB
/
email_craigslist_posts_(hourly).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
__author__ = 'jonathan_allen'
import platform
import urllib2
import cookielib
import urllib
import requests
import mechanize
import smtplib
import sys
from bs4 import BeautifulSoup
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def main():
#declare var
smtpurl = 'localhost'
me = 'sender@email.com'
you = 'reciever@email.com'
if len(sys.argv) > 1:
URL = sys.argv[1]
else:
URL = 'http://saltlakecity.craigslist.org/search/apa?postedToday=1&maxAsk=1000&bedrooms=2'
errorCheck = 0
baseURL = 'http://saltlakecity.craigslist.org'
#init browser and open login URL
browser = mechanize.Browser()
htmlResponse = browser.open(URL)
#if error (500) then it is down
#try:
# htmlResponse = browser.open(URL + '/admin/users/new')
#except urllib2.HTTPError, e:
# errorCheck = 0
soup = BeautifulSoup(htmlResponse)
rows = soup.findAll( 'time', attrs={ "datetime": True })
links = soup.findAll( 'a', attrs={ "class" : 'hdrlnk' })
s_rows = [ x["datetime"] for x in rows ]
s_links = [ y["href"] for y in links ]
#print the results
r_rows = []
for item in s_rows:
datetime_obj = datetime.strptime(item, '%Y-%m-%d %H:%M')
r_rows.append(datetime_obj)
num = 0
currTime = str(datetime.now())
s_currTime = datetime.strptime(currTime, '%Y-%m-%d %H:%M:%S.%f')
final_rows = []
for item1 in r_rows:
temp1 = s_currTime - item1
diff_in_min = temp1.total_seconds()/60
final_rows.append(diff_in_min)
emailString = ''
while num < len(rows):
if final_rows[num] < 60:
emailString = emailString + baseURL + s_links[num] + '\n'
num = num + 1
msg = MIMEMultipart('alternative')
msg['Subject'] = "New Pads To Look At"
msg['From'] = me
msg['To'] = you
part1 = MIMEText(emailString, 'plain')
msg.attach(part1)
s = smtplib.SMTP(smtpurl)
s.sendmail(me, you, msg.as_string())
s.quit()
print "sent mail"
browser.close()
if __name__ == "__main__":
main()