Skip to content

Instantly share code, notes, and snippets.

@warewolf
Last active January 5, 2018 18:22
Show Gist options
  • Select an option

  • Save warewolf/13b28dca3e827f7be439 to your computer and use it in GitHub Desktop.

Select an option

Save warewolf/13b28dca3e827f7be439 to your computer and use it in GitHub Desktop.
di.fm tracklist scraping perl script (so you can "tag" your likes)
#!/bin/bash
# softlink me to a channel name, e.g. clubdubstep chillout, trance.
CHANNEL=`basename $0`
difmwant $CHANNEL
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use JSON;
use HTTP::Request::Common;
use URI;
my $channelkey = shift @ARGV;
die "Need a channel name" unless $channelkey;
my $ua = LWP::UserAgent->new(
from => 'difm-perlapi@richardharman.com',
agent => 'Perl/difm-API',
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/";
my $recent_response = $ua->request($recent_req);
if ($recent_response->is_success) {
my $content = $recent_response->decoded_content();
my $utc;
{ local $ENV{TZ}="UTC"; $utc = time; }
my $tracklist = $json->decode($content);
#print Data::Dumper->Dump([$tracklist],[qw($tracklist)]);
@{$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; }
#!/bin/bash
difm.pl $* | head -1 | cut -f2 -d: | cut -b2- | tee -a ~/.wantmp3s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment