#!/usr/bin/perl ################################## # Personal Radio DJ - 1.1 # # By Andy Schmitz # # http://www.ARSchmitz.com/radio # # # # Creative Commons (BY-NC) # # License 2008-2010 # ################################## #Import required Packages. use LWP::UserAgent; use JSON; $ua = LWP::UserAgent->new; #If user enters no parameters or one of a few different help parameters, display the help screen. if ($ARGV[0] eq "" or $ARGV[0] eq "-?" or $ARGV[0] eq "/?" or $ARGV[0] eq "help") { print " radio [-v] station-code [num] [format] -v - Displays the Version and License Information. station-code - The 4 letter radio-code. num - The number of songs to display format - The format to display the info in. HTML ok. -a - Artist -s - Song Title -t - Time Played -c - Hyperlink to Cover Art -v - Hyperlink to Music Video -i - Song Id, used to link to iTunes Example: Shows the last song played radio WSUM Shows the last 4 songs played radio WSUM 4 Shows the album art of the last song played radio WSUM 1 \"\" Shows the last 3 songs played with a link to iTunes. radio WSUM 3 \"-a - -s Link\"\n" #Display the Version Information & give credit where it's due (to Yes.com). } elsif ($ARGV[0] eq "-v") { print " Personal Radio DJ - 1.1 By Andy Schmitz http://www.ARSchmitz.com/radio Creative Commons (BY-NC) License 2008-2010 NOTE: This program uses the Yes.com api. The information regarding its use can be found at http://api.yes.com. They have been very kind and without their servers, this program would be 100% useless. Thank You Very Much!"; } #If number of songs or song formatting has been left blank, provide defaults. $ARGV[1] = "1" if $ARGV[1] eq ""; $ARGV[2] = '%1$s - %2$s' if $ARGV[2] eq ""; #Change the formatting into a user friendly format. $ARGV[2] =~ s/-a/%1\$s/gi; #Artist $ARGV[2] =~ s/-s/%2\$s/gi; #Song Title $ARGV[2] =~ s/-t/%3\$s/gi; #Time Played $ARGV[2] =~ s/-c/%4\$s/gi; #Cover Art (Hyperlink) $ARGV[2] =~ s/-v/%5\$s/gi; #Music Video (Hyperlink) $ARGV[2] =~ s/-i/%6\$s/gi; #Song Id #Contact the servers and request a JSON object. my $req = HTTP::Request->new(POST => "http://api.yes.com/1/recent?"); $req->content("name=$ARGV[0]&max=$ARGV[1]"); $req->content_type('application/x-www-form-urlencoded'); $req = $ua->request($req); #Reach into that JSON object and extract the data. Put each formatted string into @formattedSongs. my @formattedSongs; if ($req->is_success) { my %json = %{from_json($req->content)}; foreach (@{$json{"songs"}}) { push(@formattedSongs, sprintf($ARGV[2], $$_{'by'}, $$_{'title'}, $$_{'at'}, $$_{'cover'}, $$_{'video'}, $$_{'id'})); } } #This is what people would usually expand upon. My program simply prints out the string. But you can do anything you want to! foreach(@formattedSongs) { print "$_\n"; }