Created
March 24, 2014 16:13
-
-
Save warewolf/9743438 to your computer and use it in GitHub Desktop.
Previous track listing of di.fm channels
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/perl | |
| # usage: difm.pl channelname | |
| use strict; | |
| use warnings; | |
| use LWP::UserAgent; | |
| use JSON; | |
| use HTTP::Request::Common; | |
| use URI; | |
| my $channelkey = shift @ARGV; | |
| my $ua = LWP::UserAgent->new( | |
| from => 'difm-perlapi@richardharman.com', | |
| agent => 'Perl/difm-ghettoAPI', | |
| timeout => 180, | |
| ); | |
| my $json = JSON->new(); | |
| my $streamlist_req = GET 'http://listen.di.fm/streamlist'; | |
| my $streamlist_response = $ua->request($streamlist_req); | |
| if ($streamlist_response->is_success) { | |
| my $content = $streamlist_response->decoded_content(); | |
| my $streamlist = $json->decode($content); | |
| my ($channel) = grep { $_->{key} eq $channelkey } @$streamlist; | |
| die unless defined $channel; | |
| my $channel_id = $channel->{id}; | |
| my $recent_req = GET "http://api.audioaddict.com/v1/di/track_history/channel/$channel_id.jsonp?callback=_AudioAddict_TrackHistory_Channel"; | |
| my $recent_response = $ua->request($recent_req); | |
| if ($recent_response->is_success) { | |
| my $content = $recent_response->decoded_content(); | |
| $content =~ s/_AudioAddict_TrackHistory_Channel\(//ms; | |
| $content =~ s/\);$//ms; | |
| my $utc; | |
| { local $ENV{TZ}="UTC"; $utc = time; } | |
| my $tracklist = $json->decode($content); | |
| @{$tracklist} = map { $_->{type} eq "advertisement" ? () : $_ } @$tracklist; | |
| foreach my $track (splice(@$tracklist,0,3)) { | |
| printf "%ds ago: %s\n",$utc-$track->{started},$track->{track}; | |
| } | |
| } | |
| } else { | |
| exit 1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment