-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify_network.rb
executable file
·160 lines (135 loc) · 4.26 KB
/
spotify_network.rb
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
#!/usr/bin/env ruby
# https://github.com/obrie/spotify_web/
require 'spotify_web'
require 'json'
require 'dotenv'
Dotenv.load
username = ENV['spotify_username']
password = ENV['spotify_password']
@spotify = SpotifyWeb::Client.new(username, password)
MAX_CUTTOFF = 12
MATCH_CUTTOFF = 0.07
@ids = []
@current_songs = []
def id_for(song)
id = [song["name"], song["artist"]].join("_")
id = id.downcase().gsub(/\s+/,"_").gsub(/\W+/,"")
# if @ids.include? id
# puts "WARNING: already have id for:#{song["name"]}"
# end
@ids << id
id
end
# Get similar songs
def get_similar(old_song)
puts old_song
begin
results = @spotify.song.similar(old_song["artist"], old_song["name"])
rescue Exception => msg
puts "ERROR: #{msg}"
results = []
end
songs = []
# puts results.inspect
results.each do |r|
match = r["match"].to_f
if match > MATCH_CUTTOFF
song = {}
song["match"] = match
song["name"] = r["name"]
song["artist"] = r["artist"]["name"]
song["id"] = id_for(song)
song["playcount"] = r["playcount"].to_i
songs << song
end
end
songs
end
def links_for(origin, songs)
links = []
songs.each do |song|
link = {"source" => origin["id"], "target" => song["id"]}
reverse_link = {"target" => origin["id"], "source" => song["id"]}
if !links.include?(link) and !links.include?(reverse_link)
links << link
end
end
links
end
def unseen_songs(current_songs, new_songs)
unseen = []
current_song_ids = current_songs.collect {|cs| cs["id"]}
new_songs.each do |song|
if !current_song_ids.include? song["id"]
unseen << song
end
end
unseen
end
def expand(songs, links, root)
new_songs = get_similar(root)
unseen = unseen_songs(songs, new_songs)[0..MAX_CUTTOFF]
new_links = links_for(root, unseen)
[unseen, new_links]
end
def grab(root, output_filename)
links = []
all_songs = []
first_iteration, new_links = expand(all_songs, links, root)
all_songs.concat first_iteration
links.concat(new_links)
unlinked_songs = []
puts all_songs.length
first_iteration.clone()[1..-1].each do |song|
puts song["name"]
new_songs, new_links = expand(all_songs, links, song)
all_songs.concat(new_songs)
unlinked_songs.concat(new_songs)
links.concat(new_links)
puts all_songs.length
end
# second_iteration = all_songs.sample(10)
# second_iteration.each do |song|
# puts song["name"]
# new_songs, new_links = expand(all_songs, links, song)
# all_songs.concat(new_songs)
# unlinked_songs.concat(new_songs)
# links.concat(new_links)
# puts all_songs.length
# end
# song_ids = all_songs.collect {|s| s["id"]}
# second_iteration = all_songs.sample(20)
# second_iteration.each do |song|
# new_songs = get_similar(song)
# new_links = []
# new_songs.each do |sim_song|
# if song_ids.include?(sim_song["id"]) and sim_song["match"] > MATCH_CUTTOFF
# new_links << sim_song
# end
# end
# links.concat(links_for(song, new_links))
# end
data = {}
data["nodes"] = all_songs
data["links"] = links
File.open(output_filename, 'w') do |file|
file.puts JSON.pretty_generate(JSON.parse(data.to_json))
end
end
roots = [
# {"name" => "Halo", "artist" => "Beyoncé", "filename" => "halo.json"},
# {"name" => "Thriller", "artist" => "Michael Jackson", "filename" => "thriller.json"},
# {"name" => "Hotline Bling", "artist" => "Drake", "filename" => "hotline_bling.json"},
# {"name" => "I Will Always Love You", "artist" => "Whitney Houston", "filename" => "i_will_always_love_you.json"},
# {"name" => "Despacito", "artist" => "Luis Fonsi", "filename" => "despacito.json"},
# {"name" => "Apologize", "artist" => "Timbaland", "filename" => "apologize.json"},
# {"name" => "Respect", "artist" => "Aretha Franklin", "filename" => "respect.json"},
# {"name" => "Billie Jean", "artist" => "Michael Jackson", "filename" => "billie_jean.json"},
# {"name" => "Pray For Me", "artist" => "The Weeknd", "filename" => "pray_for_me.json"},
# {"name" => "Run the World (Girls)", "artist" => "Beyoncé", "filename" => "run_the_world.json"}
{"name" => "Alright", "artist" => "Kendrick Lamar", "filename" => "alright.json"}
]
roots.each do |root|
root["id"] = id_for(root)
grab(root, root["filename"])
end