Created
September 1, 2016 00:02
-
-
Save NigoroJr/460fee47f4cd3eabbee1f5bb9a7228e5 to your computer and use it in GitHub Desktop.
This file contains 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 '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 |
This file contains 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
#!/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/&/&/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