-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanalyse_connections.py
executable file
·412 lines (347 loc) · 21.2 KB
/
analyse_connections.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
'''
Created on June 30th, 2014
@author: annajordanous
'''
import sqlite3
def connect_to_db(db_path):
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
return cursor
def run_sql_query(cursor, query):
cursor.execute(query)
result = cursor.fetchall()
return result
def get_track_creator(cursor, track_id):
query = 'SELECT user_id FROM tracks WHERE id = '+str(track_id)
return run_sql_query(cursor, query)
def get_all_connections_for_user(cursor, user_id):
''' Demonstration of all the methods available in this file for getting information on connections to and from a given user'''
connections = dict()
try:
print('Finding FOLLOWS (users) relationships for user '+str(user_id))
num_users = get_num_users_this_user_follows(cursor, user_id)
print('Number of users this user follows: '+str(num_users))
connections['num_users_this_user_follows'] = num_users
list_users = get_users_this_user_follows(cursor, user_id)
print('List of users this user follows: '+str(list_users))
connections['list_users_this_user_follows'] = list_users
num_users = get_num_users_following_this_user(cursor, user_id)
print('Number of users who follow this user: '+str(num_users))
connections['num_users_following_this_user'] = num_users
list_users = get_users_following_this_user(cursor, user_id)
print('List of users who follow this user: '+str(list_users))
connections['list_users_following_this_user'] = list_users
except Exception as e:
print('Could not collect FOLLOWS relationships for this user: '+e.message+str(e.args))
try:
print('Finding LIKES (tracks) relationships for user '+str(user_id))
num_users = get_num_users_this_user_likes_tracks_of(cursor, user_id)
print('Number of users whose tracks this user has liked: '+str(num_users))
connections['num_users_this_user_likes_tracks_of'] = num_users
list_users = get_users_this_user_likes_tracks_of(cursor, user_id)
print('List of users whose tracks this user has liked: '+str(list_users))
connections['list_users_this_user_likes_tracks_of'] = list_users
num_users = get_num_users_liking_this_users_tracks(cursor, user_id)
print('Number of users who have liked this users tracks: '+str(num_users))
connections['num_users_liking_this_users_tracks'] = num_users
list_users = get_users_liking_this_users_tracks(cursor, user_id)
print('List of users who have liked this users tracks: '+str(list_users))
connections['list_users_liking_this_users_tracks'] = list_users
except Exception as e:
print('Could not collect LIKES relationships for this user: '+e.message+str(e.args))
try:
print('Finding COMMENTING relationships for user '+str(user_id))
num_users = get_num_users_this_user_commented_on_tracks_for(cursor, user_id)
print('Number of users whose tracks this user has commented on: '+str(num_users))
connections['num_users_this_user_has_commented_on_tracks'] = num_users
list_users = get_users_this_user_commented_on_tracks_for(cursor, user_id)
print('List of users whose tracks this user has commented on: '+str(list_users))
connections['list_users_this_user_has_commented_on_tracks'] = list_users
num_users = get_num_users_commenting_on_this_users_tracks(cursor, user_id)
print('Number of users who have commented on this users tracks: '+str(num_users))
connections['num_users_commenting_on_this_users_tracks'] = num_users
list_users = get_users_commenting_on_this_users_tracks(cursor, user_id)
print('List of users who have commented on this users tracks: '+str(list_users))
connections['list_users_commenting_on_this_users_tracks'] = list_users
except Exception as e:
print('Could not collect COMMENTING relationships for this user: '+e.message+str(e.args))
try:
print('Finding PLAYLIST-based relationships for user '+str(user_id))
num_users = get_num_users_this_user_playlisted_tracks_of(cursor, user_id)
print('Number of users who this user has playlisted tracks of: '+str(num_users))
connections['num_users_this_user_playlisted_tracks_of'] = num_users
list_users = get_users_this_user_playlisted_tracks_of(cursor, user_id)
print('List of users who this user has playlisted tracks of: '+str(list_users))
connections['list_users_this_user_playlisted_tracks_of'] = list_users
num_users = get_num_users_playlisting_this_user_tracks(cursor, user_id)
print('Number of users who have playlisted this users tracks: '+str(num_users))
connections['num_users_playlisting_this_users_tracks'] = num_users
list_users = get_users_playlisting_this_user_tracks(cursor, user_id)
print('List of users who have playlisted this users tracks: '+str(list_users))
connections['list_users_playlisting_this_users_tracks'] = list_users
except Exception as e:
print('Could not collect PLAYLIST-based relationships for this user: '+e.message+str(e.args))
try:
print('Finding GROUP-based relationships for user '+str(user_id))
num_users = get_num_users_in_this_users_groups(cursor, user_id)
print('Number of users in groups created by this user: '+str(num_users))
connections['num_users_in_groups_created_by_this_user'] = num_users
list_users = get_users_in_this_users_groups(cursor, user_id)
print('List of users in this users groups: '+str(list_users))
connections['list_users_in_groups_created_by_this_user'] = list_users
num_users = get_num_creators_of_groups_this_user_is_in(cursor, user_id)
print('Number of users creating groups that this user is in: '+str(num_users))
connections['num_users_creating_this_users_groups'] = num_users
list_users = get_creators_of_groups_this_user_is_in(cursor, user_id)
print('List of users creating groups that this user is in: '+str(list_users))
connections['list_users_creating_this_users_groups'] = list_users
except Exception as e:
print('Could not collect GROUP-based relationships for this user: '+e.message+str(e.args))
return connections
###################################################
# Queries for which users this given user follows #
###################################################
def get_num_users_this_user_follows(cursor, user_id):
num_users = run_sql_query(cursor,
'SELECT COUNT(DISTINCT followed) '
+'FROM x_follows_y '
+'WHERE follower='+str(user_id))
return num_users[0][0]
def get_users_this_user_follows(cursor, user_id):
users = run_sql_query(cursor,
'SELECT DISTINCT followed '
+'FROM x_follows_y '
+'WHERE follower='+str(user_id))
return {x[0] for x in users}
##################################################
# Queries for which users follow the given users #
##################################################
def get_num_users_following_this_user(cursor, user_id):
num_users = run_sql_query(cursor,
'SELECT COUNT(DISTINCT follower) '
+'FROM x_follows_y '
+'WHERE followed='+str(user_id))
return num_users[0][0]
def get_users_following_this_user(cursor, user_id):
users = run_sql_query(cursor,
'SELECT DISTINCT follower '
+'FROM x_follows_y '
+'WHERE followed='+str(user_id))
return {x[0] for x in users}
#########################################################################
# Queries for which users have had tracks favourited by the given users #
#########################################################################
def get_num_users_this_user_likes_tracks_of(cursor, user_id):
num_users = run_sql_query(cursor,
'SELECT COUNT(DISTINCT track_producer_id) '
+'FROM favourites '
+'WHERE user_id='+str(user_id))
return num_users[0][0]
def get_users_this_user_likes_tracks_of(cursor, user_id):
users = run_sql_query(cursor,
'SELECT DISTINCT track_producer_id '
+'FROM favourites '
+'WHERE user_id='+str(user_id))
return {x[0] for x in users}
###################################################################
# Queries for which users have favourited the given user's tracks #
###################################################################
def get_num_users_liking_this_users_tracks(cursor, user_id):
num_users = run_sql_query(cursor,
'SELECT COUNT(DISTINCT user_id) '
+'FROM favourites '
+'WHERE track_producer_id='+str(user_id))
return num_users[0][0]
def get_users_liking_this_users_tracks(cursor, user_id):
users = run_sql_query(cursor,
'SELECT DISTINCT user_id '
+'FROM favourites '
+'WHERE track_producer_id='+str(user_id))
return {x[0] for x in users}
##################################################################
# Queries for which users have tracks this user has commented on #
##################################################################
def get_num_users_this_user_commented_on_tracks_for(cursor, user_id):
num_users = run_sql_query(cursor,
'SELECT COUNT(DISTINCT tracks.user_id) '
+'FROM comments JOIN tracks '
+'WHERE comments.track_id = tracks.id '
+'AND comments.user_id='+str(user_id))
return num_users[0][0]
def get_users_this_user_commented_on_tracks_for(cursor, user_id):
users = run_sql_query(cursor,
'SELECT DISTINCT tracks.user_id '
+'FROM comments JOIN tracks '
+'WHERE comments.track_id = tracks.id '
+'AND comments.user_id='+str(user_id))
return {x[0] for x in users}
###############################################################
# Queries for which users have commented on this users tracks #
###############################################################
def get_num_users_commenting_on_this_users_tracks(cursor, user_id):
num_users = run_sql_query(cursor,
'SELECT COUNT(DISTINCT comments.user_id) '
+'FROM comments JOIN tracks '
+'WHERE comments.track_id = tracks.id '
+'AND tracks.user_id='+str(user_id))
return num_users[0][0]
def get_users_commenting_on_this_users_tracks(cursor, user_id):
users = run_sql_query(cursor,
'SELECT DISTINCT comments.user_id '
+'FROM comments JOIN tracks '
+'WHERE comments.track_id = tracks.id '
+'AND tracks.user_id='+str(user_id))
return {x[0] for x in users}
#########################################################################
# Queries for which users have had tracks playlisted by the given users #
#########################################################################
def get_num_users_this_user_playlisted_tracks_of(cursor, user_id):
num_users = run_sql_query(cursor,
'SELECT COUNT(DISTINCT user_id) '
+'FROM playlists '
+'WHERE track_producer_id='+str(user_id))
return num_users[0][0]
def get_users_this_user_playlisted_tracks_of(cursor, user_id):
users = run_sql_query(cursor,
'SELECT DISTINCT user_id '
+'FROM playlists '
+'WHERE track_producer_id='+str(user_id))
return {x[0] for x in users}
###################################################################
# Queries for which users have playlisted the given user's tracks #
###################################################################
def get_num_users_playlisting_this_user_tracks(cursor, user_id):
num_users = run_sql_query(cursor,
'SELECT COUNT(DISTINCT track_producer_id) '
+'FROM playlists '
+'WHERE user_id='+str(user_id))
return num_users[0][0]
def get_users_playlisting_this_user_tracks(cursor, user_id):
users = run_sql_query(cursor,
'SELECT DISTINCT track_producer_id '
+'FROM playlists '
+'WHERE user_id='+str(user_id))
return {x[0] for x in users}
##########################################################
# Queries for who is in groups created by the given user #
##########################################################
def get_num_users_in_this_users_groups(cursor, user_id):
num_users = run_sql_query(cursor,
'SELECT COUNT(DISTINCT group_mem.user_id) '
+'FROM groups JOIN group_mem '
+'WHERE groups.id=group_mem.group_id '
+'AND groups.creator_id='+str(user_id))
return num_users[0][0]
def get_users_in_this_users_groups(cursor, user_id):
users = run_sql_query(cursor,
'SELECT DISTINCT group_mem.user_id '
+'FROM groups JOIN group_mem '
+'WHERE groups.id=group_mem.group_id '
+'AND groups.creator_id='+str(user_id))
return {x[0] for x in users}
#########################################################
# Queries for who created the groups a given user is in #
#########################################################
def get_num_creators_of_groups_this_user_is_in(cursor, user_id):
num_users = run_sql_query(cursor,
'SELECT COUNT(DISTINCT groups.creator_id) '
+'FROM groups JOIN group_mem '
+'WHERE groups.id=group_mem.group_id '
+'AND group_mem.user_id='+str(user_id))
return num_users[0][0]
def get_creators_of_groups_this_user_is_in(cursor, user_id):
users = run_sql_query(cursor,
'SELECT DISTINCT groups.creator_id '
+'FROM groups JOIN group_mem '
+'WHERE groups.id=group_mem.group_id '
+'AND group_mem.user_id='+str(user_id))
return {x[0] for x in users}
def get_all_connections_between_two_users(cursor, user_id1, user_id2):
''' Demonstration of all the methods available in this file for getting information on connections between two given users'''
print('Finding relationships between the SoundCloud users '+str(user_id1)+' and '+str(user_id2))
connections = dict()
try:
if run_sql_query(cursor, 'SELECT count(followed) from x_follows_y WHERE follower='+str(user_id1))==1:
print(str(user_id1)+' follows '+str(user_id2))
u1_follows_u2 = True
else:
print(str(user_id1)+' does not follow '+str(user_id2))
u1_follows_u2 = False
connections['u1_follows_u2'] = u1_follows_u2
if run_sql_query(cursor, 'SELECT count(followed) from x_follows_y WHERE follower='+str(user_id2))==1:
print(str(user_id2)+' follows '+str(user_id1))
u2_follows_u1 = True
else:
print(str(user_id2)+' does not follow '+str(user_id1))
u2_follows_u1 = False
connections['u2_follows_u1'] = u2_follows_u1
except Exception as e:
print('Could not collect FOLLOWS relationships between these users: '+e.message+str(e.args))
try:
u1_likes_u2_tracks = run_sql_query(cursor,
'SELECT COUNT(track_id) FROM favourites '
+'WHERE user_id = '+str(user_id1)+' AND track_producer_id = '+str(user_id2))[0][0]
print('User '+str(user_id1)+' likes '+str(u1_likes_u2_tracks)+' tracks by '+str(user_id2))
connections['u1_likes_u2_tracks'] = u1_likes_u2_tracks
u2_likes_u1_tracks = run_sql_query(cursor,
'SELECT COUNT(track_id) FROM favourites '
+'WHERE user_id = '+str(user_id2)+' AND track_producer_id = '+str(user_id1))[0][0]
print('User '+str(user_id2)+' likes '+str(u2_likes_u1_tracks)+' tracks by '+str(user_id1))
connections['u2_likes_u1_tracks'] = u2_likes_u1_tracks
except Exception as e:
print('Could not collect LIKES relationships between these users: '+e.message+str(e.args))
try:
u1_comments_on_u2_tracks = run_sql_query(cursor,
'SELECT COUNT(comments.id) FROM comments JOIN tracks '
+'WHERE comments.track_id = tracks.id '
+'AND comments.user_id = '+str(user_id1)
+' AND tracks.user_id = '+str(user_id2))[0][0]
print('User '+str(user_id1)+' has made '+str(u1_comments_on_u2_tracks)+' comments on tracks by '+str(user_id2))
connections['u1_comments_on_u2_tracks'] = u1_comments_on_u2_tracks
u2_comments_on_u1_tracks = run_sql_query(cursor,
'SELECT COUNT(comments.id) FROM comments JOIN tracks '
+'WHERE comments.track_id = tracks.id '
+'AND comments.user_id = '+str(user_id2)
+' AND tracks.user_id = '+str(user_id1))[0][0]
print('User '+str(user_id2)+' has made '+str(u2_comments_on_u1_tracks)+' comments on tracks by '+str(user_id1))
connections['u2_comments_on_u1_tracks'] = u2_comments_on_u1_tracks
except Exception as e:
print('Could not collect COMMENTING relationships between these users: '+e.message+str(e.args))
try:
u1_playlists_u2_tracks = run_sql_query(cursor,
'SELECT COUNT(track_id) FROM playlists '
+'WHERE user_id = '+str(user_id1)+' AND track_producer_id = '+str(user_id2))[0][0]
print('User '+str(user_id1)+' has playlisted '+str(u1_playlists_u2_tracks)+' tracks by '+str(user_id2))
connections['u1_playlists_u2_tracks'] = u1_playlists_u2_tracks
u2_playlists_u1_tracks = run_sql_query(cursor,
'SELECT COUNT(track_id) FROM playlists '
+'WHERE user_id = '+str(user_id2)+' AND track_producer_id = '+str(user_id1))[0][0]
print('User '+str(user_id2)+' has playlisted '+str(u2_playlists_u1_tracks)+' tracks by '+str(user_id1))
connections['u2_playlists_u1_tracks'] = u2_playlists_u1_tracks
except Exception as e:
print('Could not collect PLAYLIST-based relationships between these users: '+e.message+str(e.args))
try:
u1_in_u2_groups = run_sql_query(cursor,
'SELECT COUNT(group_mem.group_id) FROM group_mem JOIN groups '
+'WHERE group_mem.group_id = groups.id '
+'AND group_mem.user_id = '+str(user_id1)
+' AND groups.creator_id = '+str(user_id2))[0][0]
print('User '+str(user_id1)+' is in '+str(u1_in_u2_groups)+' groups created by '+str(user_id2))
connections['u1_in_u2_groups'] = u1_in_u2_groups
u2_in_u1_groups = run_sql_query(cursor,
'SELECT COUNT(group_mem.group_id) FROM group_mem JOIN groups '
+'WHERE group_mem.group_id = groups.id '
+'AND group_mem.user_id = '+str(user_id2)
+' AND groups.creator_id = '+str(user_id1))[0][0]
print('User '+str(user_id2)+' is in '+str(u2_in_u1_groups)+' groups created by '+str(user_id1))
connections['u2_in_u1_groups'] = u2_in_u1_groups
except Exception as e:
print('Could not collect GROUP-based relationships between these users: '+e.message+str(e.args))
return connections
def main(db_path, user_id1 = 117854, user_id2 = 104353):
print('Analysing connections between users. Warning, for large data this may take some time to run.')
cursor = connect_to_db(db_path)
#print(get_all_connections_for_user(cursor, user_id1))
print(get_all_connections_between_two_users(cursor, user_id1, user_id2))
if __name__ == '__main__':
main('scdb_FINAL.sqlite')