-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify.rb
52 lines (34 loc) · 1.31 KB
/
spotify.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
require 'rspotify'
# API authentication
RSpotify::authenticate("key", "secret")
# read the artists from the folder structure
artists_list = Dir.entries("D:/Music/")
n = artists_list.length
# there is some empty "." values, not sure why
artists_list.delete_at(n-1)
artists_list.delete_at(0)
artists_list.delete_at(0)
puts artists_list
not_found = Array.new
not_matched = Array.new
artists_list.each do |artist_search|
artists = RSpotify::Artist.search(artist_search)
# if artist is not found, skip
if artists.empty?
not_found.push(artist_search)
next
end
artist = artists.first
# if artist does not exactly match to the #1 search result, skip
# there is some encoding problem with artist.name obtained from the API, non-english letters get scrambled, need to follow that artists manually
if artist.name.downcase != artist_search.downcase
not_matched.push(artist_search)
next
end
# call the api to follow the artist by id
puts "working on #{artist.name}"
puts `curl -X PUT "https://api.spotify.com/v1/me/following?type=artist&ids=#{artist.id}" -H "Accept: application/json" -H "Authorization: Bearer AUTHTOKEN"` #don't forget to input the token here
end
# print artists that did not get followed for on of two reasons
puts "ARTISTS NOT FOUND: #{not_found}"
puts "ARTISTS NOT MATCHED: #{not_matched}"