-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.coffee
173 lines (130 loc) · 3.96 KB
/
server.coffee
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
fs = require('fs')
async = require('async')
svpply = require('svpply').API
api = new svpply()
count = 0
userName = 'Nicolas Forestier'
# userName = 'Jeanie Choi'
# userName = "Harper Reed"
# userName = "Yotam Troim"
# userName = "Feltron"
userName = "Elodie P"
GRAPH = true
getDataForUser = ( username ) ->
api.users.find( {"query": userName}, (data) ->
# console.log data.response.users
# for user in data.response.users
# console.log "#{user.id} \t #{user.products_count} \t #{user.name}"
_id = data.response.users[0].id
# console.log data.response.users[0]
api.users.owns( _id, (ownrq) ->
ownsProduct = ownrq.response.products
nbrWants = data.response.users[0].products_count
nbrRequest = ~~ (nbrWants/100)
requests = []
console.log nbrWants, nbrRequest
if nbrRequest > 0
for n in [0..nbrRequest]
offset = n*100
requests.push offsetRequestProductForger( _id, offset )
else
requests.push offsetRequestProductForger( _id, 0 )
console.log "#{requests.length} REQUESTS"
async.parallel( requests, (err, requestsResults) =>
_products = []
_result = []
for resreq in requestsResults
for prod in resreq.response.products
prod.own = false
_products.push prod
for ownp in ownsProduct
ownp.own = true
_products.push ownp
console.log "#{_products.length} PRODUCTS \t/ #{ownsProduct.length} OWNS \t / #{resreq.response.products.length} WANTS"
productRequests = []
for prod in _products
productRequests.push productRequestForger( prod )
async.parallel( productRequests, (err, results) =>
products = []
users = {}
for result in results
p = setProduct( result[0] )
products.push p
for user in result[1]
u = setUser( user )
if !users[u.id]?
u.products = [ p.id ]
users[u.id] = u
else
users[u.id].products.push( p.id )
fs.writeFile('products.json', JSON.stringify( products ), (err) ->
throw err if err
console.log 'PRODUCTS FILE : OK'
)
fs.writeFile('users.json', JSON.stringify( users ), (err) ->
throw err if err
console.log 'USERS FILE : OK'
)
)
)
)
)
### --- ###
offsetRequestProductForger = ( _id, offset ) ->
return ( callback ) =>
api.users.wants( _id, offset, (data) ->
callback( null, data )
)
productRequestForger = (product) ->
return ( callback ) =>
api.products.users(product.id, (data) ->
count += 1
result = []
if data?
result = data.response.users
console.log "#{count} \t | #{product.id} \t- #{data.response.users.length}/#{product.saves} users"
else
console.log "#{count} \t | #{product.id} \t- NO DATA"
callback(null, [product, result])
)
setProduct = ( product, own ) ->
item = {}
item.id = product.id
item.title = product.page_title
item.url = product.page_url
item.price = [product.price, product.formatted_price]
item.img = product.image
item.wants = product.saves
item.store = {}
item.categories = []
item.own = product.own
item.store.id = product.store.id
item.store.name = product.store.name
for cat in product.categories
item.categories.push ( cat.name )
return item
setUser = ( user ) ->
item = {}
item.id = user.id
item.name = user.name
item.username = user.username
item.location = user.location
item.url = user.url
item.img = user.avatar
item.gender = user.gender_preference
item.products = user.products_count
item.network = [user.users_following_count, user.users_followers_count]
return item
if GRAPH
getDataForUser( userName )
else
for index in [200...500]
api.users.show(index, (data) ->
user = data.response.user
return false if !user?
if user.products_count > 300
console.log "#{user.id} \t| #{user.products_count} \t | #{user.name}"
)
api.remaining( (data) ->
console.log "REMAINING #{data.response.remaining}"
)