-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcrawl.py
218 lines (192 loc) · 6.08 KB
/
crawl.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
212
213
214
215
216
217
218
import hashlib
import re
import requests
from bs4 import BeautifulSoup
def strip_all_but_numbers(num):
return re.compile(r'[^\d.]+').sub('', num)
def parse_listings(drug, total_pages):
data = []
for i in range(1, int(total_pages) + 1):
data = data + get_listings(drug=drug, page=i)
return data
def get_listings(drug, page):
"""
Given a large string of HTML code from a page on silkroad's site,
return a json structure representing the listings.
"""
file_number = (page-1) * 25
html = open('%s_data/%s.html' % (drug, file_number)).read()
soup = BeautifulSoup(html, "lxml")
container = soup.findAll("div", id="cat_item")
items = []
for item in container:
title_elem = item.find(class_="h2")
title = title_elem.text
if "lottery" in title.lower():
# ignore lottery listings because they mess up the average price
continue
try:
quantity = guess_quantity(title)
except:
print "failed:", title
if not quantity:
continue
url = title_elem.attrs['href']
price = item.find(class_="price_big").text[1:] # remove bitcoin glyph
price = float(price.replace(',', ''))
seller = item.find("a", href=re.compile("/silkroad/user/")).text
thumb_e = item.find("img", src=re.compile("^data:"))
thumb = (thumb_e and thumb_e.attrs['src']) or ""
ships_from = [x for x in item.find(id="cat_item_description").children][8]
country = ships_from[12:]
items.append({
'title': title,
"seller": seller,
"price": price,
"quantity": quantity,
"url": url,
"country": silkroad_country_to_iso(country),
"thumb": uri_to_file(thumb),
})
print "parsed page: %s (%s.html)" % (page, file_number)
return items
def uri_to_file(uri):
"""
Convert a data URI to an image file. Save to disk, use the
md5 of the data as the filename.
"""
if not uri:
return
header, data = uri.split(',')
md5_hash = hashlib.md5(data).hexdigest()
with open("static/images/%s.jpg" % md5_hash, 'wb') as f:
f.write(data.decode('base64'))
return md5_hash
def silkroad_country_to_iso(sr_country):
if sr_country == 'Saipan':
return "MP"
if sr_country == 'United States of America':
return "US"
if sr_country == 'Canada':
return "CA"
if sr_country == 'Germany':
return "DE"
if sr_country == 'South Africa':
return "ZA"
if sr_country == 'Germany':
return "DE"
if sr_country == 'Netherlands':
return "NL"
if sr_country == 'Sweden':
return "SE"
if sr_country == 'United Kingdom':
return "GB"
if sr_country == 'undeclared':
return ""
if sr_country == 'France':
return "FR"
if sr_country == 'Czech Republic':
return "CZ"
if sr_country == 'Poland':
return "PL"
if sr_country == 'Italy':
return "IT"
if sr_country == 'Australia':
return "AU"
if sr_country == 'Spain':
return "ES"
if sr_country == 'Denmark':
return "DK"
if sr_country == 'Finland':
return "FI"
if sr_country == 'India':
return "IN"
if sr_country == 'Belgium':
return "BE"
if sr_country == 'Thailand':
return "TH"
if sr_country == 'Ukraine':
return "UA"
if sr_country == 'Ghana':
return "GH"
if sr_country == 'Austria':
return "AT"
if sr_country == 'China':
return "CN"
if sr_country == 'Portugal':
return 'PT'
if sr_country == 'Ireland':
return 'IE'
if sr_country == 'Switzerland':
return 'CH'
if sr_country == 'Philippines':
return 'PH'
raise Exception("New country: %s" % sr_country)
def guess_quantity(title):
"""
In comes a title of a silkroad listing.
Returned is a best guess on the quantity of that listing.
Works by doing a fuzzy search. All return values are in ounces.
"""
title = title.lower()
regex = "(half|quarter|\d/\d|\d+\.*\d+|\d+)[\s-]*(%s|%s)"
grams = re.search(regex % ('g|gr', 'gram'), title)
if grams:
numeric = grams.groups()[0]
if numeric == 'half':
return 0.5 / 28.35
if numeric == 'quarter':
return 0.25 / 28.35
if '/' in numeric:
num, denom = numeric.split('/')
return float(num) / float(strip_all_but_numbers(denom))
return float(numeric) / 28.35
ounces = re.search(regex % ('oz', 'ounce'), title)
if ounces:
numeric = ounces.groups()[0] # just the numeric value
if numeric == 'half':
return 0.5
if numeric == 'quarter':
return 0.25
if '/' in numeric:
num, denom = numeric.split('/')
return float(num) / float(strip_all_but_numbers(denom))
return float(numeric)
pounds = re.search(regex % ('lb|lbs', 'pound'), title)
if pounds:
numeric = pounds.groups()[0] # just the numeric value
if numeric == 'half':
return 8
if numeric == 'quarter':
return 4
if numeric == 'eighth':
return 2
if '/' in numeric:
num, denom = numeric.split('/')
return float(num) / float(strip_all_but_numbers(denom))
return float(numeric) * 16
if "1/8" in title:
return 0.123456
if "1/4" in title:
return 0.25
if "1/2" in title:
return 0.5
if "pound" in title:
return 16
if "qp" in title:
return 4
if "hp" in title:
return 8
if "oz" in title:
return 1
def test():
cases = (
("Half pound blackberry kush", 8),
("Very High Grade Commercial 1 OZ", 1),
("Quarter oz Dark Purple Grape Ape", 0.25),
("1 pound og kush", 16),
("29-GRAMS-BORDER-BLASTER", 1.02294),
)
for title, oz in cases:
result = guess_quantity(title)
assert result == oz, (title, oz, result)