Skip to content

Instantly share code, notes, and snippets.

@NigoroJr
Created September 1, 2016 00:02
Show Gist options
  • Save NigoroJr/460fee47f4cd3eabbee1f5bb9a7228e5 to your computer and use it in GitHub Desktop.
Save NigoroJr/460fee47f4cd3eabbee1f5bb9a7228e5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'open-uri'
require 'nokogiri'
API_URL = 'http://opml.radiotime.com/Describe.ashx?id=s142523'
XPATH_HAS_SONG = '//has_song'
XPATH_CURRENT_SONG = '//current_song'
XPATH_CURRENT_ARTIST = '//current_artist'
doc = Nokogiri::HTML.parse(open(API_URL))
has_song = doc.xpath(XPATH_HAS_SONG).text == 'true'
current_song = doc.xpath(XPATH_CURRENT_SONG).text
current_artist = doc.xpath(XPATH_CURRENT_ARTIST).text
if has_song
puts "#{current_song} - #{current_artist}"
else
STDERR.puts 'No Song Info Available'
return 1
end
#!/bin/sh
STATION_ID="s142523"
API_URL="http://opml.radiotime.com/Describe.ashx?id=$STATION_ID"
# First put all info into one variable
CURRENT_INFO="$( curl --silent -L "$API_URL" | \
egrep '(<current_(song|artist)>|has_song)' | \
sed -e 's/&amp;/&/g' )"
# Extract fields
HAS_SONG="$( echo "$CURRENT_INFO" | \
grep 'has_song' | \
sed -e 's/[^>]*>\(.*\)<[^>]*>/\1/' )"
CURRENT_SONG="$( echo "$CURRENT_INFO" | \
grep 'current_song' | \
sed -e 's/[^>]*>\(.*\)<[^>]*>/\1/' )"
CURRENT_ARTIST="$( echo "$CURRENT_INFO" | \
grep 'current_artist' | \
sed -e 's/[^>]*>\(.*\)<[^>]*>/\1/' )"
# Strip CRLF that they, for some reason, add at radiotime
HAS_SONG="$( echo $HAS_SONG | tr -d '\r' )"
CURRENT_SONG="$( echo $CURRENT_SONG | tr -d '\r' )"
CURRENT_ARTIST="$( echo $CURRENT_ARTIST | tr -d '\r' )"
if [ $HAS_SONG = 'true' ]; then
echo "$CURRENT_SONG - $CURRENT_ARTIST"
else
echo "No Song Info Available"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment