Alex Balgavy

Just some stuff about me.

Here's my dotfiles repository.

Check out my blog.

My keys: PGP, SSH

My crypto wallets (BTC, XMR)


What links here:

Bulk follow artists on spotify

Write down the artists in a file, one on each line. Then, get a spotify bearer token, e.g. with https://github.com/lrholmes/spotify-auth-cli.

Then, run this script, like ruby script.rb artist-file:

#!/usr/bin/env ruby
require 'uri'
require 'net/http'
require 'openssl'
require 'json'
token = "Bearer #{ENV["SPOTIFY_TOKEN"] or abort "Please set SPOTIFY_TOKEN in env"}"

all_lines = ARGF.readlines(chomp: true)
# Spotify requires groups of 50
all_lines.each_slice(50) do |lines|
  ids = lines.reduce([]) do |ids, artist|
    puts artist
    url = URI("https://api.spotify.com/v1/search")
    url.query = URI.encode_www_form({q: artist, type: :artist})

    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE

    request = Net::HTTP::Get.new(url)
    request["Authorization"] = token
    response = JSON.load(http.request(request).read_body)
    artist = response['artists']['items'][0]
    puts "Following #{artist['name']} #{artist['external_urls']['spotify']}"
    ids << artist['id']
  end

  url = URI("https://api.spotify.com/v1/me/following?type=artist")
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Put.new(url)
  request["Content-Type"] = 'application/json'
  request["Authorization"] = token
  request.body = JSON.dump({ids: ids})
  response = http.request(request)
  puts response
end