Skip to content

Instantly share code, notes, and snippets.

@im-grey
Forked from pachacamac/soma.rb
Created July 29, 2017 08:43
Show Gist options
  • Save im-grey/f29c089a85b9ef7bce07eeeb1a4a846b to your computer and use it in GitHub Desktop.
Save im-grey/f29c089a85b9ef7bce07eeeb1a4a846b to your computer and use it in GitHub Desktop.
soma
#!/usr/bin/env ruby
require 'json'
def load_json(url) JSON.parse(`wget -q -O- #{url}`, symbolize_names: true) end
class String
#extend: http://misc.flogisoft.com/bash/tip_colors_and_formatting
def black; "\e[30m#{self}\e[0m" end
def red; "\e[31m#{self}\e[0m" end
def green; "\e[32m#{self}\e[0m" end
def brown; "\e[33m#{self}\e[0m" end
def blue; "\e[34m#{self}\e[0m" end
def magenta; "\e[35m#{self}\e[0m" end
def cyan; "\e[36m#{self}\e[0m" end
def gray; "\e[37m#{self}\e[0m" end
def bg_black; "\e[40m#{self}\e[0m" end
def bg_red; "\e[41m#{self}\e[0m" end
def bg_green; "\e[42m#{self}\e[0m" end
def bg_brown; "\e[43m#{self}\e[0m" end
def bg_blue; "\e[44m#{self}\e[0m" end
def bg_magenta; "\e[45m#{self}\e[0m" end
def bg_cyan; "\e[46m#{self}\e[0m" end
def bg_gray; "\e[47m#{self}\e[0m" end
def bold; "\e[1m#{self}\e[22m" end
def italic; "\e[3m#{self}\e[23m" end
def underline; "\e[4m#{self}\e[24m" end
def blink; "\e[5m#{self}\e[25m" end
def reverse_color; "\e[7m#{self}\e[27m" end
end
channels = load_json('http://somafm.com/channels.json')[:channels]
if !ARGV[0]
sizes = channels.reduce(Hash.new(0)) { |s, e| e.each { |k, v| s[k] = s[k] < v.to_s.size ? v.to_s.size : s[k] }; s }
channels.each_with_index do |c,i|
print i.to_s.ljust(5).red.bold
print c[:id].ljust(sizes[:id] + 2).magenta
print c[:title].ljust(sizes[:title] + 2).green
puts c[:description].cyan
end
print "\nWhich channel nr would you like to hear? > "
nr = gets.chomp.to_i
channel_id = channels[nr][:id]
puts "\nTuning into #{channels[nr][:title]}...\n\n"
else
q = ARGV[0].chomp.downcase
channel_id = channels.find { |e| e[:id].downcase.include?(q) || e[:title].downcase.include?(q) }
abort "Sorry couldn't find channel that matches '#{q}'..." unless channel_id
channel_id = channel_id[:id]
puts "Tuning into #{channel_id}...\n\n"
end
Thread.abort_on_exception = true
Thread.new do
current_song = ''
loop do
sleep 10
song = nil
begin
song = load_json("http://somafm.com/songs/#{channel_id}.json")[:songs].first
rescue
next
end
next if song == current_song
print "\n#{Time.now.strftime('%R')}: ".blue
print song[:artist].bold.brown
print ' - '.bold.red
print song[:title].bold.brown
if song[:album] != ''
print ' - '.bold.red
print "[#{song[:album]}]".green
end
puts "\n"
current_song = song
end
end
stream_url = "http://ice.somafm.com/#{channel_id}"
puts stream_url
if ARGV[1] == 'sonos'
puts 'trying to play on sonos'
require 'sonos'
sonos = Sonos::System.new
speaker = sonos.speakers.first
speaker.volume = 40
speaker.play stream_url
speaker.play
else
# yep you gotta have vlc installed ... otherwise change your player here.
`cvlc -q #{stream_url}`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment