-
-
Save franciscolourenco/36e34a524709789f8f0a 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 'json' | |
def load_json(url) JSON.parse(`wget -q -O- #{url}`) 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 | |
if !ARGV[0] | |
channels = load_json('http://somafm.com/channels.json')['channels'] | |
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 | |
channel_id = ARGV[0].chomp | |
puts "Tuning into #{channel_id}...\n\n" | |
end | |
Thread.new{ | |
current_song = '' | |
loop do | |
sleep 10 | |
song = load_json("http://somafm.com/songs/#{channel_id}.json")['songs'].first | |
if song != current_song | |
print "\n#{Time.now.strftime('%F %R')}: ".blue | |
print "Now playing: ".red | |
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 | |
} | |
if (/darwin/ =~ RUBY_PLATFORM) != nil # mac | |
`/Applications/VLC.app/Contents/MacOS/VLC -q -I dummy http://ice.somafm.com/#{channel_id}` | |
else # linux | |
`cvlc -q http://ice.somafm.com/#{channel_id}` | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment