discogs (1457B)
1 #!/usr/bin/env ruby 2 # Client to query Discogs 3 require "discogs-wrapper" # Needed for API access 4 5 my_user_token = ENV['DISCOGS_API_TOKEN'] 6 7 if my_user_token == nil 8 raise "No user token set." 9 end 10 11 # Create a wrapper 12 wrapper=Discogs::Wrapper.new("iTunesStyleGetter", user_token: my_user_token) 13 14 # Join the rest of the arguments into a search string 15 query=ARGV.join(" ") 16 17 # Initialise arrays 18 genres,styles=[],[] 19 20 # Search for the query 21 search=wrapper.search("#{query}") 22 23 if search.results.empty? 24 STDERR.puts 'discogs: no search results' 25 exit 1 26 end 27 28 # Add the genres of the first result 29 search.results.each do |result| 30 if result.genre? && !result.genre.empty? 31 result.genre.each do |g| 32 if g.include? "," 33 g.split(",").each { |x| genres.push "#{x}" } 34 else 35 genres.push "#{g}" 36 end 37 end 38 break 39 end 40 end 41 42 # Add the styles of the first result 43 search.results.each do |result| 44 if result.style? && !result.style.empty? 45 result.style.to_a.each {|x| styles.push "#{x}"} 46 end 47 break 48 end 49 50 # Add the url of the first result 51 url = "http://discogs.com#{search.results[0].uri}".split(" ") 52 53 # Join everything together into a 2d array 54 # 2 different separators ("~", ",") because bash 55 result = [genres.join("~"), styles.join("~"), url] 56 57 # Unless only asking for genre, also add title 58 if !(ARGV.include?('-g') || ARGV.include?('--genre')) 59 result = [search.results.first.title] + result 60 end 61 62 # Send result to stdout 63 p result