Last active
August 29, 2015 14:07
-
-
Save kmarsh/7be5e3143f342740cd1b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'open-uri' | |
require 'json' | |
require 'uri' | |
require 'pp' | |
# search-spotify | |
# | |
# Takes a bunch of songs, in `Artist - Title` format from STDIN and searches | |
# Spotify for them, one-by-one, outputting track URIs on STDOUT. Suitable for | |
# copying and pasting into a new Spotify playlist. | |
# | |
# Not affiliated or connected to Spotify in any way. (Except uses their official | |
# API... which is pretty neat!) | |
not_found = [] | |
ARGF.each do |line| | |
next if line.match(/@/) | |
artist, title = *line.chomp.split(' - ').map {|s| s.to_s.strip.gsub('-', ' ') } | |
url = "https://api.spotify.com/v1/search?q=artist:#{URI.encode(artist)}*%20title:#{URI.encode(title)}*&type=track&market=US&limit=1" | |
json = JSON.parse(open(url).read) | |
id = json['tracks']['items'][0]['uri'] rescue nil | |
if id | |
puts id | |
else | |
not_found << "#{artist} - #{title}" | |
end | |
sleep 0.5 | |
end | |
if not_found != [] | |
$stderr.puts "Not found: #{not_found.join(', ')}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment