-
Notifications
You must be signed in to change notification settings - Fork 1
/
craigslist.py
211 lines (159 loc) · 4.76 KB
/
craigslist.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import requests
import datetime
from bs4 import BeautifulSoup
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource('dynamodb')
class CraigslistItem(object):
def __init__(self, href, tablename):
self.info = {
"link": href,
"postid": None,
"currentPrice": None,
"title": None,
"activeSince": None,
"description": None,
"priceHistory": list(),
}
self.tablename = tablename
self.GetNewData()
self.UpdateLocalData()
self.StoreData()
#self.ExportData()
def GetNewData(self):
r = requests.get(self.info["link"])
data = r.text
soup = BeautifulSoup(data)
try:
for child in soup.find('div', {"class": "postinginfos"}):
if "post id" in child.string:
postid = (child.string).split(" ")[-1]
print postid
break
except:
postid = "No postid"
try:
price = soup.find('span', {"class": "price"}).text
except:
price = "No price"
try:
title = soup.find('span', {"id": "titletextonly"}).text
except:
title = "No title"
try:
activeSince = soup.find('time', {"class":"date timeago"}).get('datetime').split("T")[0].replace("-",'')
except:
activeSince = "No posting data"
self._IntegrateNewData(postid, price, title, activeSince)
def ExportData(self):
#further develop this
return self.info
def UpdateLocalData(self):
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table(self.tablename)
try:
result = table.query(KeyConditionExpression=Key('postid').eq(self.info['postid']))
print "found history!"
print result
if result['Items'] and "priceHistory" in result['Items'][0] and result['Items'][0]['priceHistory'] != None:
self.info['priceHistory'] = result['Items'][0]['priceHistory']
if self.info['currentPrice'] != str(result['Items'][0]['priceHistory'][-1]):
print "appending new price"
self.info['priceHistory'].append(self.info['currentPrice'])
except:
print "ERROR"
def StoreData(self):
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table(self.tablename)
table.put_item(Item=self.info)
def _IntegrateNewData(self, postid, price, title, activeSince):
# further develop this
self.info["postid"] = postid
self.info["activeSince"] = activeSince
self.info["currentPrice"] = price
self.info["title"] = title
self.info['priceHistory'] = [self.info['currentPrice']]
class CraigslistBot(object):
def __init__(self, startpage, tablename):
self.startPage = startpage
self.work = []
self.emailData=''
self.items = []
self.tablename = tablename
def Run(self):
self.GetWork()
self.DoWork()
self.FormatItemsHtml()
self.SendData()
def GetWork(self):
r = requests.get(self.startPage)
data = r.text
soup = BeautifulSoup(data)
for link in soup.find_all('a', {"class": "result-image gallery"}):
self.work.append(link.get('href'))
def DoWork(self):
i = 0
for link in self.work:
data = CraigslistItem(link, self.tablename)
self.items.append(data.ExportData())
i+=1
print i
self.items = sorted(self.items, key=lambda k: k['activeSince'])
print self.items
def FormatItemsString(self):
for item in self.items:
self.emailData += ('Post ID: ' + item['postid'] + '\n')
self.emailData += ('Title: ' + item['title'] + '\n')
self.emailData += ('Price: ' + item['currentPrice'] + '\n')
self.emailData += ('Active Since: ' + item['activeSince'][:3] + "-" + item['activeSince'][4:6] + '-' + item['activeSince'][6:] + '\n')
self.emailData += '\n'
def FormatItemsHtml(self):
for item in self.items:
self.emailData += """
<ul>
<li>Title: {title}</li>
<li>PostID: {postid}</li>
<li>Price: {price}</li>
<li>History: {history}</li>
<li>Active Since: {active}</li>
</ul>""".format(postid=item['postid'], title=item['title'], price=item['currentPrice'], history=item['priceHistory'], active=str(item['activeSince']))
def SendData(self):
client = boto3.client('ses')
response = client.send_email(
Source='[email protected]',
Destination={
'ToAddresses': [
],
},
Message={
'Subject': {
'Data': 'craigslist',
'Charset': 'UTF-8'
},
'Body': {
'Text': {
'Data': self.emailData,
'Charset': 'UTF-8'
},
'Html': {
'Data':"""
<html>
<head></head>
<body>
<h1>Craigslist TVs</h1>
<p>{emailData}<p>
</body>
</html>
""".format(emailData=self.emailData),
'Charset': 'UTF-8'
}
}
},
)
print response
if __name__=="__main__":
bot = CraigslistBot("https://annarbor.craigslist.org/search/sss?query=tv&sort=rel", "craigslisttv")
bot.Run()
with open("lastrun.txt", "w'") as filename:
filename.write(str(datetime.datetime.now()))