forked from akandykeller/cloudchaser
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsc_api_calls.py
524 lines (453 loc) · 17.7 KB
/
sc_api_calls.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import soundcloud
from py2neo import authenticate, Relationship, Graph
from py2neo.packages.httpstream.http import SocketError
from requests.exceptions import HTTPError
from utils import get_results, handle_http_errors
from functools import partial
client = soundcloud.Client(client_id="454aeaee30d3533d6d8f448556b50f23")
id2username_cache = {}
# need to navigate and set the password to "pass" for first time
authenticate("localhost:7474", "neo4j", "cloudchaser")
userGraph = Graph()
userGraph.delete_all()
def getUserAttr(resource, attr):
# if hasattr(resource, 'user'): return resource.user[attr]
if hasattr(resource, attr):
return getattr(resource, attr)
return None
getUsername = partial(getUserAttr, attr="username")
getUserid = partial(getUserAttr, attr="id")
@handle_http_errors
def id2username(profile, kind="users"):
global id2username_dict
username = id2username_cache.get(profile, None)
if username is not None:
return username
# username is none, we don't have it in cache
result = client.get("/%s/%s" % (kind, str(profile)))
if kind == "comments":
username = result.user["username"]
elif kind == "tracks":
username = result.title
else:
username = result.username
# encode it correctly
username = str(username.encode("utf-8"))
id2username_cache[profile] = username
return username
def getRelationships(profile, client, url):
return get_results(client, url)
@handle_http_errors
def getFollowings(profile):
# get list of users who the artist is following.
followings = get_results(client, "/users/{0:s}/followings/".format(str(profile)))
return followings
@handle_http_errors
def getFollowers(profile):
followers = get_results(client, "/users/{0:s}/followers/".format(str(profile)))
return followers
@handle_http_errors
def getFavorites(profile):
favorites = get_results(client, "/users/{0:s}/favorites/".format(str(profile)))
return favorites
@handle_http_errors
def getComments(profile):
comments = get_results(client, "/users/{0:s}/comments/".format(str(profile)))
return comments
@handle_http_errors
def getTracks(profile):
tracks = get_results(client, "/users/{0:s}/tracks/".format(str(profile)))
return tracks
def getUserInfo(profile):
info = [
"id",
"permalink",
"username",
"avatar_url",
"country",
"city",
"website",
"track_count",
"followers_count",
"followings_count",
]
return {i: getUserAttr(profile, i) for i in info}
def getTrackInfo(track):
info = [
"id",
"user_id",
"created_at",
"streamabale",
"downloadable",
"playback_count",
"download_count",
"favoritings_count",
"comment_count",
]
return {i: getUserAttr(track, i) for i in info}
def getUserFavInfo(fav):
info = [
"id", # as 'track_id',
"user_id",
"last_modified"
# 'user'
]
return {i: getUserAttr(fav, i) for i in info}
def getTrackFavInfo(fav):
info = ["id", "username", "last_modified"] # as 'user_id',
return {i: getUserAttr(fav, i) for i in info}
def getUserCommInfo(comm):
info = ["id", "user_id", "track_id", "timestamp"]
return {i: getUserAttr(comm, i) for i in info}
def getTrackCommInfo(comm):
info = [
"id",
"user_id",
"track_id",
"timestamp"
# 'user'
]
return {i: getUserAttr(comm, i) for i in info}
# def getWeight(profile, neighbor, profileGraph, attr):
# if profileGraph.has_edge(profile, neighbor, key=attr):
# return profileGraph.get_edge_data(profile, neighbor, key=attr)['weight'] + 1
# else:
# return 1
# def addWeight(action, profile, neighbor, profileGraph, attr):
# new_weight = getWeight(profile, neighbor, profileGraph, attr)
# profileGraph.add_edge(profile, neighbor, key=attr, weight=new_weight)
# print "\t", u"{0:>15s}: {1:s} --> {2:s}".format(action, getUsername(profile), getUsername(neighbor))
# return new_weight
def addNode(profile):
query = "MERGE (profile:soundcloud {id: {profile}.id}) \
ON CREATE SET profile={profile} "
try:
userGraph.cypher.execute(query, {"profile": getUserInfo(profile)})
except SocketError:
print(
(
"\t\t\t",
"----Cannot connect to cypher db. Assume the query was executed successfully.----",
)
)
return True
def addPair(profile, neighbor):
try:
addNode(profile)
addNode(neighbor)
except SocketError:
print(
(
"\t\t\t",
"----Cannot connect to cypher db. Assume the query was executed successfully.----",
)
)
return True
def addFollow(profile, neighbor):
query = (
"MERGE (profile:soundcloud {id: {profile}.id}) \
ON CREATE SET profile={profile} "
"MERGE (neighbor:soundcloud {id: {neighbor}.id}) \
ON CREATE SET neighbor={neighbor} "
"MERGE (profile)-[r:follows]->(neighbor)"
)
if profile is not None and neighbor is not None:
profile_info = getUserInfo(profile)
neighbor_info = getUserInfo(neighbor)
try:
userGraph.cypher.execute(
query, {"profile": profile_info, "neighbor": neighbor_info}
)
if profile_info["username"] and neighbor_info["username"]:
print(
(profile_info["username"] + " follows " + neighbor_info["username"])
)
except SocketError:
print(
(
"\t\t\t",
"----Cannot connect to cypher db. Assume the query was executed successfully.----",
)
)
if profile is None:
print("Profile not found.")
if neighbor is None:
print("Neighbor not found.")
return True
def addUserFav(profile, favorite): # , track):
query = (
"MERGE (profile:soundcloud {id: {profile}.id}) \
ON CREATE SET profile={profile} "
"MERGE (favorite:soundcloud {id: {favorite}.user_id}) \
ON CREATE SET favorite={favorite} "
# 'MERGE (profile)-[r:favorites]->(favorite)'
)
# Create new favorite relationship if it dne
# If so, append the new track to the relationship property
# 'MERGE (profile)-[r:favorites]->(neighbor) ON CREATE SET r.track_ids = [{track}] \
# ON MATCH SET r.track_ids = \
# CASE WHEN {track} NOT IN r.track_ids \
# THEN r.track_ids + [{track}] \
# ELSE r.track_ids \
# END')
if profile is not None and favorite is not None:
profile_info = getUserInfo(profile)
fav_info = getUserFavInfo(favorite)
track_id = fav_info["id"]
try:
userGraph.cypher.execute(
query,
{
"profile": profile_info,
# 'track': track,
"favorite": fav_info,
},
)
prof = userGraph.find_one("soundcloud", "id", profile_info["id"])
print("Profile properties - ")
print((prof.properties))
fav = userGraph.find_one("soundcloud", "id", fav_info["user_id"])
print("Favorite properties - ")
print((fav.properties))
if not Relationship(prof, "favorites", fav).exists:
userGraph.create_unique(
Relationship(prof, "favorites", fav, track_ids=[track_id])
)
else:
Relationship(prof, "favorites", fav)["track_ids"] += [track_id]
if profile_info["username"] and fav_info["user_id"]:
try:
print(
(
profile_info["username"]
+ " favorites "
+ id2username(fav_info["user_id"])
)
)
except UnicodeDecodeError:
print(
(
profile_info["username"]
+ " favorites "
+ str(fav_info["user_id"])
)
)
except TypeError:
print("User's favorite not printed due to Type Error.")
except SocketError:
print(
(
"\t\t\t",
"----Cannot connect to cypher db. Assume the query was executed successfully.----",
)
)
if profile is None:
print("Profile not found")
if favorite is None:
print("Favorite not found")
return True
# Use SET to update properties
def addUserComm(profile, comment): # , comment):
query = (
"MERGE (profile:soundcloud {id: {profile}.id}) \
ON CREATE SET profile={profile} "
"MERGE (comment:soundcloud {id: {comment}.user_id}) \
ON CREATE SET comment={comment} "
"MERGE (profile)-[r:comments]->(comment)"
)
# Create new comment relationship if it dne
# Append new comment to property if relationship already exists
# 'MERGE (profile)-[r:comments]->(comment) ON CREATE SET r.comment_ids = [{comment}] \
# ON MATCH SET r.coment_ids = \
# CASE WHEN {comment} NOT IN r.comment_ids \
# THEN r.comment_ids + [{comment}] \
# ELSE r.comment_ids \
# END')
if profile is not None and comment is not None:
profile_info = getUserInfo(profile)
comm_info = getUserCommInfo(comment)
try:
userGraph.cypher.execute(
query,
{
"profile": profile_info,
#'track': track,
"comment": comm_info,
},
)
# if Relationship.exists(profile, "comments", neighbor):
# Relationship(profile, "comments", neighbor)['comment_ids'] += [comment]
# else:
# userGraph.create_unique(Relationship(profile, "comments", neighbor, track_ids = [track]))
if profile_info["username"] and comm_info["user_id"]:
try:
print(
(
profile_info["username"]
+ " comments "
+ id2username(comm_info["user_id"])
)
)
except UnicodeDecodeError:
print(
(
profile_info["username"]
+ " favorites "
+ str(comm_info["user_id"])
)
)
except TypeError:
print("User's comment not shown due to Type error.")
except SocketError:
print(
(
"\t\t\t",
"----Cannot connect to cypher db. Assume the query was executed successfully.----",
)
)
if profile is None:
print("Profile not found")
if comment is None:
print("Comment not found")
return True
def addTrackFav(favoriter, profile): # , track):
query = (
"MERGE (profile:soundcloud {id: {profile}.id}) \
ON CREATE SET profile={profile} "
"MERGE (favoriter:soundcloud {id: {favoriter}.id}) \
ON CREATE SET favoriter={favoriter} "
"MERGE (favoriter)-[r:favorites]->(profile)"
)
# Create new favorite relationship if it dne
# If so, append the new track to the relationship property
# 'MERGE (profile)-[r:favorites]->(neighbor) ON CREATE SET r.track_ids = [{track}] \
# ON MATCH SET r.track_ids = \
# CASE WHEN {track} NOT IN r.track_ids \
# THEN r.track_ids + [{track}] \
# ELSE r.track_ids \
# END')
if favoriter is not None and profile is not None:
profile_info = getUserInfo(profile)
fav_info = getTrackFavInfo(favoriter)
try:
userGraph.cypher.execute(
query,
{
"profile": profile_info,
# 'track': track,
"favoriter": fav_info,
},
)
# if Relationship.exists(profile, "favorites", neighbor):
# Relationship(profile, "favorites", neighbor)['track_ids'] += [track]
# else:
# userGraph.create_unique(Relationship(profile, "favorites", neighbor, track_ids = [track]))
if profile_info["username"] and fav_info["username"]:
print((fav_info["username"] + " favorites " + profile_info["username"]))
except SocketError:
print(
(
"\t\t\t",
"----Cannot connect to cypher db. Assume the query was executed successfully.----",
)
)
if favoriter is None:
print("Favoriter not found")
if profile is None:
print("Profile not found")
return True
# Use SET to update properties
def addTrackComm(commenter, profile): # , comment):
query = (
"MERGE (profile:soundcloud {id: {profile}.id}) \
ON CREATE SET profile={profile} "
"MERGE (commenter:soundcloud {id: {commenter}.user_id}) \
ON CREATE SET commenter={commenter} "
"MERGE (commenter)-[r:comments]->(profile)"
)
# Create new comment relationship if it dne
# Append new comment to property if relationship already exists
# 'MERGE (profile)-[r:comment]->(neighbor) ON CREATE SET r.comment_ids = [{comment}] \
# ON MATCH SET r.coment_ids = \
# CASE WHEN {comment} NOT IN r.comment_ids \
# THEN r.comment_ids + [{comment}] \
# ELSE r.comment_ids \
# END')
if commenter is not None and profile is not None:
profile_info = getUserInfo(profile)
comm_info = getTrackCommInfo(commenter)
try:
userGraph.cypher.execute(
query,
{
"profile": profile_info,
#'track': track,
"commenter": comm_info,
},
)
# if Relationship.exists(profile, "comments", neighbor):
# Relationship(profile, "comments", neighbor)['comment_ids'] += [comment]
# else:
# userGraph.create_unique(Relationship(profile, "comments", neighbor, track_ids = [track]))
if profile_info["username"] and comm_info["user_id"]:
try:
print(
(
id2username(comm_info["user_id"])
+ " comments "
+ profile_info["username"]
)
)
except UnicodeDecodeError:
print(
(
str(comm_info["user_id"])
+ " comments "
+ profile_info["username"]
)
)
except TypeError:
print("Track comment not shown due to Type error.")
except SocketError:
print(
(
"\t\t\t",
"----Cannot connect to cypher db. Assume the query was executed successfully.----",
)
)
if commenter is None:
print("Commenter not found")
if profile is None:
print("Profile not found")
return True
def addFollowings(profile, followings): # , profileGraph):
for follow in followings:
addFollow(profile, follow)
# addAction('follows', artist, user, addWeight('follows', artist, user, profileGraph, 'fol_weight'))
def addFollowers(profile, followers): # , profileGraph):
for follow in followers:
addFollow(follow, profile)
# addAction('follows', user, artist, addWeight('follows', user, artist, profileGraph, 'fol_weight'))
def addFavorites(profile, favorites): # , profileGraph):
for favorite in favorites:
addUserFav(profile, favorite) # , favorite.id)
# addAction('favorites', artist, user, addWeight('favorites', artist, user, profileGraph, 'fav_weight'))
def addComments(profile, comments): # , profileGraph):
for comment in comments:
addUserComm(profile, comment) # , comment.id)
# addAction('comments', artist, user, addWeight('comments', artist, user, profileGraph, 'com_weight'))
def addTracks(profile, tracks): # , profileGraph):
for track in tracks:
# get list of users who have favorited this user's track
try:
favoriters = get_results(client, "/tracks/" + str(track.id) + "/favoriters")
for favoriter in favoriters:
addTrackFav(favoriter, profile) # , track.id)
# addAction('favorites', user, artist, addWeight('favorites', user, artist, profileGraph, 'fav_weight'))
# get list of users who have commented on this user's track
commenters = get_results(client, "/tracks/" + str(track.id) + "/comments")
for commenter in commenters:
addTrackComm(commenter, profile) # , commenter.id)
# addAction('comments', user, artist, addWeight('comments', comment, artist, profileGraph, 'com_weight'))
except HTTPError:
print("Track not processed due to HTTP error.")