-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsn.py
187 lines (137 loc) · 3.69 KB
/
csn.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
import os
from os import system
import time
import shutil
import feedparser
from random import randrange
import urllib2
from bs4 import BeautifulSoup
import re
import glob
import threading
#to do:
#3 - clean up any lingering HTML in the description
home_path = os.getcwd()
def openPage(url):
opener = urllib2.build_opener()
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
page = opener.open(url)
soup = BeautifulSoup(page, from_encoding="utf-8")
opener.close()
return soup
def getImages(soup):
counter = 0
images = soup.find_all("img")
if len(images) > 1:
path = 'csn_processing/tempStorage/'
os.chdir(path)
files = glob.glob('*.jpg')
for f in files:
os.unlink(f)
for i in images:
if images.index(i) != 1:
link = str(i.get("src"))
link = link.replace("50x50c.jpg", "600x450.jpg")
print link
opener = urllib2.build_opener()
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
photo = opener.open(link)
with open(str(counter) + '.jpg', 'wb') as file_:
file_.write(photo.read())
file_.close()
opener.close()
counter += 1
time.sleep(15)
print "moving on"
return len(images)
else:
print "no images found"
return 0
os.chdir(home_path)
def getFullDescription(soup):
text = soup.find_all(id="postingbody")
desc = str(text[0]).replace('<section id="postingbody">', '').replace('</section>', '').replace('<br/>', '').replace('<br>', '').replace('"', ' inches').replace('&', '&')
contactLink = re.findall(r'<a(.*)a>', desc)
ampersand = re.findall(r'&(.*);', desc)
if len(contactLink) > 0:
desc = desc.replace(contactLink[0], '').replace('<a', '').replace('a>', '')
if len(ampersand) > 0:
for a in ampersand:
desc = desc.replace(a, '')
return desc
def getContactInfo(_id):
link = "http://newyork.craigslist.org/reply/nyc/for/" + _id
soup = openPage(link)
try:
email = soup.a.contents
return email[0]
except AttributeError:
print "no email"
return "None"
def collectEntry():
os.chdir(home_path)
print os.getcwd()
f = feedparser.parse("https://newyork.craigslist.org/search/sss?format=rss")
select = randrange(len(f.entries))
selection = f.entries[select]
# print selection
title = unicode.encode(selection.title, "utf-8")
link = unicode.encode(selection.link, "utf-8")
title = title.split("$")
if len(title) == 1:
title.append("???")
title[0] = title[0].replace('&', '&')
# summary = summary.lower()
r = re.findall(r'/[^/]*$', link)
_id = r[0].replace('/', '').replace('.html', '')
print _id
email = getContactInfo(_id)
print email
if email == "None":
print "starting over"
os.chdir(home_path)
print os.getcwd()
startTimer(30.0)
else:
print(selection.link)
raw = openPage(selection.link)
photos = getImages(raw)
summary = getFullDescription(raw)
print title
# print summary
if photos == 0:
print "starting over"
os.chdir(home_path)
print os.getcwd()
startTimer(30.0)
else:
tempPath = home_path + '/csn_processing/tempStorage/'
realPath = home_path + '/csn_processing/data/'
os.chdir(realPath)
oldFiles = glob.glob('*.jpg')
for o in oldFiles:
os.unlink(o)
os.chdir(tempPath)
files = glob.glob('*.jpg')
for f in files:
shutil.copy2(f, realPath + f)
os.chdir(realPath)
with open('data.txt', 'wb') as file_:
file_.write(title[0])
file_.write('\n')
file_.write(title[1])
file_.write('\n')
file_.write(link)
file_.write('\n')
file_.write(email)
file_.write('\n')
file_.write(summary)
file_.close()
os.chdir(home_path)
print os.getcwd()
startTimer(180.0)
def startTimer(time):
print "starting timer..."
t = threading.Timer(time, collectEntry)
t.start()
collectEntry()