/
stats.py
executable file
·295 lines (246 loc) · 9.16 KB
/
stats.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pymongo
import numpy as np
import matplotlib.pyplot as plt
import simplekml
import sys
import time
from datetime import datetime
from pygeocoder import Geocoder, GeocoderError
#Clean prices
def clean_prices(poshmark):
for p in poshmark.find({"price": {"$exists": True}}):
price = int(p["price"][1:])
p["price"] = price
poshmark.save(p)
#Clean profile numbers
def clean_profiles(poshmark):
for p in poshmark.find({"user_name": {"$exists": True}}):
get_num = lambda num: int(num.replace(',', ''))
listings = get_num(p["listings"])
followers = get_num(p["followers"])
following = get_num(p["following"])
p["listings"] = listings
p["followers"] = followers
p["following"] = following
poshmark.save(p)
def print_percentage(title, count, tot):
print title + str(percentage(count, tot))
def percentage(count, tot):
return (float(count)/tot) * 100
def chart_brands(brands, values):
fig, ax = plt.subplots()
ax.set_xticklabels(brands, rotation=-90)
x_pos = np.arange(len(brands))
plt.bar(x_pos, values, align='center')
plt.xticks(x_pos, brands)
plt.xlabel('Brands')
plt.ylabel('No. Listings')
plt.title('Top brands')
plt.show()
def chart_dates(occurance_list):
month_list = [t.month for t in occurance_list]
numbers=[x for x in xrange(1,13)]
labels=map(lambda x: str(x), numbers)
plt.xticks(numbers, labels)
plt.xlim(0,24)
plt.hist(month_list)
plt.show()
def chart_prices(prices):
chart(prices, 'Prices', 'Listings', 'Prices Histogram')
def chart(vals, xlabel="", ylabel="", title = "", buckets=10):
fig, ax = plt.subplots()
counts, bins, patches = plt.hist(vals, buckets)
# Set the ticks to be at the edges of the bins.
ax.set_xticks(bins)
# Label the raw counts and the percentages below the x-axis...
bin_centers = 0.5 * np.diff(bins) + bins[:-1]
for count, x in zip(counts, bin_centers):
# Label the raw counts
ax.annotate(str(count), xy=(x, 0), xycoords=('data', 'axes fraction'),
xytext=(0, -18), textcoords='offset points', va='top', ha='center')
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
# Give ourselves some more room at the bottom of the plot
plt.subplots_adjust(bottom=0.15)
plt.show()
def create_map(profiles):
options = ("no", "don't", "not", u"❌", "sorry", u"🚫", u"✋", "paypal", "pp",
"negociable", "negotiable", "negoatiating", "negotiate",
"offer", "deal", "lower")
kml = simplekml.Kml()
coords_map = {}
g = Geocoder()
for p in profiles:
location = p["location"]
# Check if it's just a message and not a location
if any(o in location for o in options):
continue
# We can't hit the API too hard
time.sleep(6)
try:
coords = coords_map[location]
except KeyError:
try:
results = g.geocode(location)
lat, lng = results.coordinates
coords = [(lng, lat)]
coords_map[location] = coords
pnt = kml.newpoint()
pnt.style.labelstyle.scale = 2 # Make the text twice as big
pnt.name = p["user_name"]
pnt.coords = coords
# Save the KML
kml.save("poshmark.kml")
except GeocoderError as e:
print e
def process_comments(comments):
found = False
trade_mentions = 0
dates = []
get_time_obj = lambda x: datetime.strptime(x, time_string)
for comment_list in comments:
username, comment, date = comment_list.values()
time_string = "%b %d %I:%M%p"
if "trade" in comment:
trade_mentions += 1
if not found:
found = True
try:
dates.append(get_time_obj(date))
except:
pass
return trade_mentions, dates
# Access poshmark collection from our db
client = pymongo.MongoClient("localhost", 27017)
db = client.scrapy
poshmark = db.items
# Lists
atlantians = []
georgians = []
prices = []
listings_count = []
followers_count = []
following_count = []
# Dictionaries
brands = {}
sold_brands = {}
# Counts
listing_trade_mentions = 0
total_trade_mentions = 0
total_comments = 0
trade_listings = 0
no_trade_listings = 0
bundle_listings = 0
paypal_listings = 0
negotiable_listings = 0
# Listings that cost over $200 are usually not real
listings = poshmark.find({"price": {"$lt": 200}})
tot_listings = poshmark.find({"price": {"$exists": True}})
profiles = poshmark.find(
{"user_name": {"$exists": True},
"followers": {"$lt": 1000},
"following": {"$lt": 1000},
"listings": {"$lt": 1000}})
create_map(profiles)
sys.exit(0)
for p in listings:
prices.append(p["price"])
brand = p["brand"].lower()
sold = p["sold"].lower()
# Check that brand data is available
if brand:
if not brand in brands:
brands[brand] = 1
else:
brands[brand] += 1
if sold == "yes":
if not brand in sold_brands:
sold_brands[brand] = 1
else:
sold_brands[brand] += 1
comments = p["comments"]
if comments:
total_comments = total_comments + len(comments)
trade_mentions, dates = process_comments(comments)
total_trade_mentions = trade_mentions + total_trade_mentions
if trade_mentions > 0:
listing_trade_mentions += 1
for p in profiles:
listings_count.append(p["listings"])
followers_count.append(p["followers"])
following_count.append(p["following"])
location = p["location"].lower()
if "atlanta" in location:
atlantians.append(p)
georgians.append(p)
else:
options = ("georgia", "ga")
if any(o in location for o in options):
georgians.append(p)
if "trade" in location:
trade_listings += 1
options = ("no", "don't", "not", u"❌", "sorry", u"🚫", u"✋")
if any(o in location for o in options):
no_trade_listings += 1
if "bundle" in location:
bundle_listings += 1
options = ("paypal", "pp")
if any(o in location for o in options):
paypal_listings += 1
options = ("negociable", "negotiable", "negoatiating",
"negotiate", "offer", "deal", "lower")
if any(o in location for o in options):
negotiable_listings += 1
get_mean = lambda val: str(np.mean(np.array(val)))
get_median = lambda val: str(np.median(np.array(val)))
print "* Stats"
print "** General Stats"
print "- Ok?: " + str(poshmark.count() == \
(tot_listings.count() + profiles.count()))
print "- Total number of documents in collection: " + str(poshmark.count())
print "- Total number of listings: " + str(tot_listings.count())
print "- Listings < $200, i.e. real listings: " + str(listings.count())
print "- Total number of of profiles: " + str(profiles.count())
print "- Total number of comments: " + str(total_comments)
print "- Average price: " + get_mean(prices)
print "- Mean price: " + get_median(prices)
print "** On Trade"
print "- Listings that mention the word trade: " + str(listing_trade_mentions)
print_percentage("- % of listings that mention trade: ", \
listing_trade_mentions, listings.count())
print "- Number of trade mentions in comments: " + str(total_trade_mentions)
print_percentage("- % of comments that mention trade: ", \
total_trade_mentions, total_comments)
print "** Profile Stats"
print "- Average number of listings: " + get_mean(listings_count)
print "- Mean number of listings: " + get_median(listings_count)
print "- Average number of followers: " + get_mean(followers_count)
print "- Mean number of followers: " + get_median(followers_count)
print "- Average number of following: " + get_mean(following_count)
print "- Mean number of following: " + get_median(following_count)
print_percentage("- % That mention trade on profile: ", \
trade_listings, profiles.count())
print_percentage("- % That say NO trade on profile: ", \
no_trade_listings, profiles.count())
print_percentage("- % That mention bundles: ", \
bundle_listings, profiles.count())
print_percentage("- % That mention Paypal: ", paypal_listings, profiles.count())
print_percentage("- % That mention that the price is negotiable: ", \
negotiable_listings, profiles.count())
print "** Locals"
print "- Atlantians: " + str(len(atlantians))
print "- Georgians: " + str(len(georgians))
chart_prices(prices)
chart(listings_count, title="Listings", buckets=15)
chart(followers_count, title="Followers")
chart(following_count, title="Following", buckets=15)
sorted_brands = sorted(brands.iteritems(), key = lambda (k, v): (v, k))
top_brands = sorted_brands[-15:]
chart_brands([k for k, v in top_brands], [v for k, v in top_brands])
sorted_brands = sorted(sold_brands.iteritems(), key = lambda (k, v): (v, k))
top_brands = sorted_brands[-15:]
chart_brands([k for k, v in top_brands], [v for k, v in top_brands])
# chart(dates)