#!/usr/bin/perl

use warnings;
use strict;

# A short little script to yank down a list of sounds played in the
# previous day on 95.3 Party since their Web based Active X
# control trash for listening in live doesn't work, even on Windows.

# Version 1.0

BEGIN { @AnyDBM_File::ISA = qw(GDBM_File NDBM_File) }
use AnyDBM_File;
use Fcntl;

# Setup a place to keep track of what we've reported already; No dups

my %db;
my $db = "$ENV{HOME}/.953db";

tie %db, 'GDBM_File', $db, O_CREAT|O_RDWR, 0640;

use LWP::UserAgent;

# Fake IE

my $ua = LWP::UserAgent->new(
	agent => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; T312461)'
);


my %args = (
	startday => 'yesterday',
	starttime => 'midnight',
	endday => 'yesterday',
	endtime => '23',
	callsign => 'wpyo-fm',
	tz => '0',
	st => 'time',
	tagline => '953 Party',
	station_id => '10046'
);

my $args = join( '', map { "$_=$args{$_}&" } keys %args );

my $response = $ua->get(
	"http://953party.com/cgi-bin/lastsongs/search.cgi?$args",
);

my $result = join( '', $response->content() );

my @songs = (
	$result =~ m~top">(?!&nbsp)([^<]+?)(?!\n)<br>(?:\s*<a[^>]+?>|\s*)(.*?)(?:</a>\s*|\s*)</td>\s+~sg
);

# Ensure we're divisible by two
scalar( @songs ) % 2 && die;

# Associate artists with songs
my %songs = reverse @songs;

print "These songs played yesterday on 95.3 Party: Orlando's Party station!\n\n";

my $seen;
while( my( $k, $v ) = each %songs ) {
	if( $k eq '' ) {
		# Someone fsck'd up
		next;
	}
	if( exists $db{ "$k $v" } ) {
		# Already reported
		next;
	}
	else {
		$db{ "$k $v" }++;
		print "$k -> $v\n";
		$seen = 1;
	}
}

if( ! $seen ) {
	print "Looks like there weren't any or this already ran for today.\n";
}

print "\n";

untie %db;


