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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
525
526
527
528
529
530
531
532
533
534
# {
#   # load packages
#   suppressPackageStartupMessages(library(dplyr))
#   suppressPackageStartupMessages(library(spotifyr))
# 
#   # Set up environment
#   client_ID <- "bc0b388b3801497f8162615befb50a43"
#   client_secret <- "512e20aa79ff4a228cc4e95ab46a45fd"
# 
#   Sys.setenv(SPOTIFY_CLIENT_ID = client_ID)
#   Sys.setenv(SPOTIFY_CLIENT_SECRET = client_secret)
# 
#   access_token <- get_spotify_access_token()
# }


get_artists_collaborators <- function(spotify_artist_id) {
  # related artists nodes function
  get_Nodes <- function(artist_id) {
    # get artists related to main artist
    related_artists <- get_related_artists(
      id = artist_id,
      include_meta_info = TRUE
    )

    # get other artists that are related to the
    # artists that are related to the main artist
    other_related <- c()
    for (i in 1:nrow(related_artists$artists)) {
      result <- get_related_artists(
        id = related_artists$artists[["id"]][i],
        include_meta_info = TRUE
      )
      other_related <- append(other_related, result)
    }

    images <- c()
    for (i in other_related) { # this loops through the list
      for (k in 1:nrow(i)) { # this loops through each table in list
        image_urls <- i$images[[k]]$url[2] # the third image is collected per row in each table
        images <- append(images, image_urls)
      }
    }

    genre <- c()
    for (i in (other_related)) { # this loops through each list
      for (j in 1:nrow(i)) { # this loops through each table in list
        result <- i$genres[[j]][2] # this collects the 2nd item in the vector of genres
        genre <- append(genre, result)
      }
    }


    nodes <- data.frame(
      name = tolower(c(
        other_related[[1]]$name,
        other_related[[2]]$name,
        other_related[[3]]$name,
        other_related[[4]]$name,
        other_related[[5]]$name,
        other_related[[6]]$name,
        other_related[[7]]$name,
        other_related[[8]]$name,
        other_related[[9]]$name,
        other_related[[10]]$name,
        other_related[[11]]$name,
        other_related[[12]]$name,
        other_related[[13]]$name,
        other_related[[14]]$name,
        other_related[[15]]$name,
        other_related[[16]]$name,
        other_related[[17]]$name,
        other_related[[18]]$name,
        other_related[[19]]$name,
        other_related[[20]]$name
      )),
      id = c(c(
        other_related[[1]]$id,
        other_related[[2]]$id,
        other_related[[3]]$id,
        other_related[[4]]$id,
        other_related[[5]]$id,
        other_related[[6]]$id,
        other_related[[7]]$id,
        other_related[[8]]$id,
        other_related[[9]]$id,
        other_related[[10]]$id,
        other_related[[11]]$id,
        other_related[[12]]$id,
        other_related[[13]]$id,
        other_related[[14]]$id,
        other_related[[15]]$id,
        other_related[[16]]$id,
        other_related[[17]]$id,
        other_related[[18]]$id,
        other_related[[19]]$id,
        other_related[[20]]$id
      )),
      popularity = c(c(
        other_related[[1]]$popularity,
        other_related[[2]]$popularity,
        other_related[[3]]$popularity,
        other_related[[4]]$popularity,
        other_related[[5]]$popularity,
        other_related[[6]]$popularity,
        other_related[[7]]$popularity,
        other_related[[8]]$popularity,
        other_related[[9]]$popularity,
        other_related[[10]]$popularity,
        other_related[[11]]$popularity,
        other_related[[12]]$popularity,
        other_related[[13]]$popularity,
        other_related[[14]]$popularity,
        other_related[[15]]$popularity,
        other_related[[16]]$popularity,
        other_related[[17]]$popularity,
        other_related[[18]]$popularity,
        other_related[[19]]$popularity,
        other_related[[20]]$popularity
      )),
      followers = c(c(
        other_related[[1]]$followers.total,
        other_related[[2]]$followers.total,
        other_related[[3]]$followers.total,
        other_related[[4]]$followers.total,
        other_related[[5]]$followers.total,
        other_related[[6]]$followers.total,
        other_related[[7]]$followers.total,
        other_related[[8]]$followers.total,
        other_related[[9]]$followers.total,
        other_related[[10]]$followers.total,
        other_related[[11]]$followers.total,
        other_related[[12]]$followers.total,
        other_related[[13]]$followers.total,
        other_related[[14]]$followers.total,
        other_related[[15]]$followers.total,
        other_related[[16]]$followers.total,
        other_related[[17]]$followers.total,
        other_related[[18]]$followers.total,
        other_related[[19]]$followers.total,
        other_related[[20]]$followers.total
      )),
      profile = c(c(
        other_related[[1]]$external_urls.spotify,
        other_related[[2]]$external_urls.spotify,
        other_related[[3]]$external_urls.spotify,
        other_related[[4]]$external_urls.spotify,
        other_related[[5]]$external_urls.spotify,
        other_related[[6]]$external_urls.spotify,
        other_related[[7]]$external_urls.spotify,
        other_related[[8]]$external_urls.spotify,
        other_related[[9]]$external_urls.spotify,
        other_related[[10]]$external_urls.spotify,
        other_related[[11]]$external_urls.spotify,
        other_related[[12]]$external_urls.spotify,
        other_related[[13]]$external_urls.spotify,
        other_related[[14]]$external_urls.spotify,
        other_related[[15]]$external_urls.spotify,
        other_related[[16]]$external_urls.spotify,
        other_related[[17]]$external_urls.spotify,
        other_related[[18]]$external_urls.spotify,
        other_related[[19]]$external_urls.spotify,
        other_related[[20]]$external_urls.spotify
      )),
      images = images,
      genre = genre
    )

    ## Remove duplicate nodes and labels in data frame

    nodes_df <- distinct(nodes, name, id, popularity, profile,
      images, genre, followers,
      .keep_all = T
    )


    return(nodes_df)
  }

  # get related artists nodes
  related_artists <- get_Nodes(artist_id = spotify_artist_id)

  # get related artists data
  artist_related_artists <- function(related_artist) {
    related_artists_data <- list()

    for (i in 1:nrow(related_artist)) {
      # Get the artist ID from the second column of related_artists
      artist_id <- related_artist[[2]][i]

      # Retrieve the artist's albums using the artist ID
      result <- get_artist_albums(artist_id, limit = 50)

      # Create a data frame from the result
      related_artists_albums <- data.frame(result)

      # Add the data frame to the list
      related_artists_data[[i]] <- related_artists_albums
    }

    return(related_artists_data)
  }

  related_artists_data <- artist_related_artists(related_artist = related_artists)

  # get the artists collaborators
  get_collaborators <- function(data, artist_name) {
    artists <- c() # initialize empty vector
    # outer loop loops through the length of artists list
    for (i in 1:length(data$artists)) {
      # inner loop loops through the length of individual
      # "name" column in artists list
      for (j in 1:length(data$artists[[i]][3][, ])) {
        # scrapes the artist names
        result <- data$artists[[i]][3][j, ]
        # appends the names to "artists" vector
        artists <- append(artists, result)
      }
    }

    artists <- unique(artists) # removes duplicate names
    artists <- tolower(artists) # turns to lowercase
    # turns the search artist's name to NA
    artists <- gsub(tolower(artist_name), NA, artists)
    artists <- na.omit(artists) # remove NA from vector

    return(artists)
  }

  # function that gets the collaborators data
  collab_df <- function(related_artists_data, artist_data) {
    collaborators <- c()
    artists_list <- c()
    for (i in 1:length(related_artists_data)) {
      result <- get_collaborators(related_artists_data[[i]],
        artist_name = artist_data[[1]][i]
      )

      collaborators <- c(collaborators, result)
      artists_list <- c(artists_list, rep(artist_data[[1]][i], times = length(result)))
    }

    artists_collaborators <- data.frame(artists = artists_list, collaborators = collaborators)

    return(artists_collaborators)
  }

  # application of the function
  collabs <- collab_df(
    related_artists_data = related_artists_data,
    artist_data = related_artists
  )

  # get attribute data for each collaborator
  attribute_data <- list()
  for (i in 1:nrow(collabs)) {
    attribute_data[[i]] <- search_spotify(collabs$collaborators[[i]],
      type = "artist",
      include_meta_info = T
    )
  }

  # collect attributes of collaborators
  {
    name <- c()
    id <- c()
    popularity <- c()
    followers <- c()
    profile <- c()
    images <- c()
    genre <- c()

    for (i in 1:length(attribute_data)) {
      name <- c(name, attribute_data[[i]][[1]][[2]][5][[1]][1])
      id <- c(id, attribute_data[[i]][[1]][[2]][3][[1]][1])
      popularity <- c(popularity, attribute_data[[i]][[1]][[2]][6][[1]][1])
      followers <- c(followers, attribute_data[[i]][[1]][[2]][11][[1]][1])
      profile <- c(profile, attribute_data[[i]][[1]][[2]][9][[1]][1])
      images <- c(images, attribute_data[[i]][[1]][[2]][4][[1]][1])
      genre <- c(genre, attribute_data[[i]][[1]][[2]][1][[1]][1])
    }
  }

  # loop through images list and store converted
  # data frames in a list
  images_df_list <- list()
  for (i in 1:length(images)) {
    images_df_list[[i]] <- list2DF(images[[i]])
  }

  # loop through the list of data frames & extract
  # the image urls
  images_vec <- c()
  for (i in 1:length(images_df_list)) {
    images_vec <- c(images_vec, images_df_list[[i]]$url[[1]][1])
  }

  len_diff_img <- name |>
    length() - images_vec |>
    length()

  # add a repetition of the last 6 urls to the vector
  # so that its length is equal to the length of other
  # attribute vectors
  images_vec <- c(
    images_vec,
    rep(images_vec[tail(length(images_vec))], times = len_diff_img)
  )

  # get genre data
  genre_vec <- c()
  for (i in 1:length(genre)) {
    genre_vec <- c(genre_vec, genre[[i]][1])
  }

  music_genres <- c()
  for (m in 1:length(genre_vec)) {
    music_genres <- c(music_genres, genre_vec[[m]])
  }

  len_diff_gnr <- name |>
    length() - music_genres |>
    length()

  music_genres <- c(
    music_genres,
    rep(music_genres[tail(length(music_genres))], times = len_diff_gnr)
  )

  # create collaborators data frame
  collaborators_df <- data.frame(
    name = name,
    id = id,
    popularity = popularity,
    followers = followers,
    profile = profile,
    images = images_vec,
    genre = music_genres
  )

  # filter out 2Pac
  collaborators_df <- collaborators_df |>
    filter(name != "2Pac")

  # rename columns in collabs
  colnames(collabs) <- c("Vertex1", "Vertex2")

  # grab Vertex1 attributes
  popularity <- c()
  for (i in 1:nrow(collabs)) {
    result <- filter(
      related_artists,
      related_artists$name == collabs$Vertex1[[i]][1]
    )[[3]]
    popularity <- c(popularity, result)
  }

  followers <- c()
  for (i in 1:nrow(collabs)) {
    result <- filter(
      related_artists,
      related_artists$name == collabs$Vertex1[[i]][1]
    )[[4]]
    followers <- c(followers, result)
  }

  profile <- c()
  for (i in 1:nrow(collabs)) {
    result <- filter(
      related_artists,
      related_artists$name == collabs$Vertex1[[i]][1]
    )[[5]]
    profile <- c(profile, result)
  }

  images <- c()
  for (i in 1:nrow(collabs)) {
    result <- filter(
      related_artists,
      related_artists$name == collabs$Vertex1[[i]][1]
    )[[6]]
    images <- c(images, result)
  }

  genre <- c()
  for (i in 1:nrow(collabs)) {
    result <- filter(
      related_artists,
      related_artists$name == collabs$Vertex1[[i]][1]
    )[[7]]
    genre <- c(genre, result)
  }

  # convert "names" in collaborators_df to lowercase
  collaborators_df$name <- tolower(collaborators_df$name)

  # filter out "various artists" from collabs
  collabs <- collabs |>
    filter(Vertex2 != "various artists")

  # check if name in Vertex2 is an English character
  ascii_check <- c()
  for (i in 1:nrow(collabs)) {
    ascii_check <- c(ascii_check, collabs$Vertex2[[i]][1] |> stringi::stri_enc_isascii())
  }

  # append check result to collabs dataframe
  collabs$ASCII <- ascii_check

  # filter out non-English characters
  collabs <- collabs |>
    filter(ASCII != FALSE)

  # delete ASCII column
  collabs$ASCII <- NULL

  # delete rows from Vertex1 attributes to equal
  # collabs rows
  popularity <- popularity[-c(1 + length(popularity):nrow(collabs))]

  followers <- followers[-c(1 + length(followers):nrow(collabs))]

  profile <- profile[-c(1 + length(profile):nrow(collabs))]

  images <- images[-c(1 + length(images):nrow(collabs))]

  genre <- genre[-c(1 + length(genre):nrow(collabs))]

  # grab Vertex2 attributes
  popularityB <- c()
  for (i in 1:nrow(collabs)) {
    result <- filter(
      collaborators_df,
      collaborators_df$name == collabs$Vertex2[[i]][1]
    )[[3]]
    popularityB <- c(popularityB, result)
  }

  followersB <- c()
  for (i in 1:nrow(collabs)) {
    result <- filter(
      collaborators_df,
      collaborators_df$name == collabs$Vertex2[[i]][1]
    )[[4]]
    followersB <- c(followersB, result)
  }

  profileB <- c()
  for (i in 1:nrow(collabs)) {
    result <- filter(
      collaborators_df,
      collaborators_df$name == collabs$Vertex2[[i]][1]
    )[[5]]
    profileB <- c(profileB, result)
  }

  imagesB <- c()
  for (i in 1:nrow(collabs)) {
    result <- filter(
      collaborators_df,
      collaborators_df$name == collabs$Vertex2[[i]][1]
    )[[6]]
    imagesB <- c(imagesB, result)
  }

  genreB <- c()
  for (i in 1:nrow(collabs)) {
    result <- filter(
      collaborators_df,
      collaborators_df$name == collabs$Vertex2[[i]][1]
    )[[7]]
    genreB <- c(genreB, result)
  }

  # delete rows from Vertex2 attributes to equal
  # collabs rows
  popularityB <- popularityB[-c(1 + length(popularityB):nrow(collabs))]

  followersB <- followersB[-c(1 + length(followersB):nrow(collabs))]

  profileB <- profileB[-c(1 + length(profileB):nrow(collabs))]

  imagesB <- imagesB[-c(1 + length(imagesB):nrow(collabs))]

  genreB <- genreB[-c(1 + length(genreB):nrow(collabs))]

  # create flat file of collaborators
  {
    collabs$`Vertex1 popularity` <- popularity
    collabs$`Vertex1 followers` <- followers
    collabs$`Vertex1 profile` <- profile
    collabs$`Vertex1 images` <- images
    collabs$`Vertex1 genre` <- genre

    collabs$`Vertex2 popularity` <- popularityB
    collabs$`Vertex2 followers` <- followersB
    collabs$`Vertex2 profile` <- profileB
    collabs$`Vertex2 images` <- imagesB
    collabs$`Vertex2 genre` <- genreB
  }



  return(collabs)
  
  
}

# test
# tictoc::tic()
# steve_wonder_collab_network <- get_artists_collaborators(spotify_artist_id = "7guDJrEfX3qb6FEbdPA5qi")
# tictoc::toc()
# 
# 
# steve_wonder_collab_network |> View()
# 
# tictoc::tic()
# billie_eilish_collab_network <- get_artists_collaborators(spotify_artist_id = "6qqNVTkY8uBg9cP3Jd7DAH")
# tictoc::toc()
# 
# billie_eilish_collab_network |> View()
# write.csv(billie_eilish_collab_network,file = "billie_eilish_collab_network.csv")
# 
# tictoc::tic()
# madonna_collab_network <- get_artists_collaborators(spotify_artist_id = "6tbjWDEIzxoDsBA1FuhfPW")
# tictoc::toc()
# 
# madonna_collab_network |> View()
# 
# tictoc::tic()
# diana_ross_collab_network <- get_artists_collaborators(spotify_artist_id = "3MdG05syQeRYPPcClLaUGl")
# tictoc::toc()
#