Last active
June 16, 2016 15:57
-
-
Save techouse/42836b4753fb412bf05c to your computer and use it in GitHub Desktop.
Super lean DI.FM player for *nix
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/env perl | |
use warnings; | |
use strict; | |
use autodie; | |
use LWP::UserAgent; | |
use JSON; | |
use DBIx::Simple; | |
use Text::Table; | |
my $mplayer = `/usr/bin/which mplayer 2>&1`; | |
chomp $mplayer; | |
die "MPlayer not installed!\n" if $mplayer =~ /^which: no/; | |
undef $mplayer; | |
my ( | |
$streams_url, | |
$config_dir, | |
$streams_db, | |
$streams_table, | |
$get_streams, | |
$stream, | |
$keyword, | |
@pubs | |
) = ( | |
'http://listen.di.fm/public3', # set manually | |
"$ENV{'HOME'}/.config", # set manually | |
"difm_streams.db", # set manually | |
'streams', | |
0, | |
0, | |
$ARGV[0], | |
1 .. 7 | |
); | |
mkdir $config_dir unless -d $config_dir; | |
$get_streams = 1 unless -e "$config_dir/$streams_db"; | |
my $db = DBIx::Simple->connect( | |
"dbi:SQLite:dbname=$config_dir/$streams_db", | |
'', # empty username | |
'', # empty password | |
{ sqlite_unicode => 1 } | |
); | |
if ($get_streams) { | |
my $ua = LWP::UserAgent->new; | |
my $response = $ua->get($streams_url); | |
if ($response->is_success) { | |
my $streams = JSON::decode_json $response->decoded_content; | |
if (@$streams) { | |
$db->query(qq( | |
CREATE TABLE IF NOT EXISTS "$streams_table" ( | |
"id" INTEGER PRIMARY KEY, | |
"key" TEXT, | |
"name" TEXT, | |
"times_played" INTEGER DEFAULT 0, | |
"last_played" INTEGER DEFAULT 0, | |
UNIQUE("id", "key") | |
) | |
)); | |
foreach (@$streams) { | |
my %stream_data = ( | |
id => $_->{'id'}, | |
key => $_->{'key'}, | |
name => $_->{'name'}, | |
times_played => 0, | |
last_played => 0 | |
); | |
$db->query(qq( | |
INSERT OR IGNORE | |
INTO "$streams_table" ( | |
@{[join ',', keys %stream_data]} | |
) | |
VALUES (??) | |
), | |
values %stream_data | |
); | |
undef %stream_data; | |
} | |
} | |
undef $streams; | |
} | |
else { | |
die $response->status_line; | |
} | |
undef $response; | |
undef $ua; | |
} | |
if (defined $keyword && $keyword ne '--random') { | |
exit print $db->query(" | |
SELECT | |
name, | |
key, | |
id | |
FROM $streams_table | |
ORDER BY name ASC | |
")->text('table') if $keyword eq '--list' || $keyword eq '-l'; | |
# find station by keyword | |
$stream = $db->query(qq( | |
SELECT | |
id, | |
key, | |
name | |
FROM $streams_table | |
WHERE id = ? | |
OR key = ? | |
OR name = ? | |
LIMIT 1 | |
), | |
$keyword, | |
$keyword, | |
$keyword | |
)->hash; | |
$stream = $db->query(qq( | |
SELECT | |
id, | |
key, | |
name | |
FROM $streams_table | |
WHERE key LIKE ? | |
OR name LIKE ? | |
LIMIT 1 | |
), | |
"%$keyword%", | |
"%$keyword%" | |
)->hash unless $stream; | |
die "Can't find a stream with that keyword!\n" unless $stream; | |
} | |
elsif (defined $keyword && $keyword eq '--random') { | |
$stream = $db->query(qq( | |
SELECT | |
id, | |
key, | |
name | |
FROM $streams_table | |
ORDER BY RANDOM() | |
LIMIT 1 | |
))->hash; | |
} | |
else { | |
# get last played station | |
$stream = $db->query(qq( | |
SELECT | |
id, | |
key, | |
name | |
FROM $streams_table | |
WHERE last_played = ? | |
LIMIT 1 | |
), | |
1 | |
)->hash; | |
# select random | |
unless ($stream) { | |
$stream = $db->query(qq( | |
SELECT | |
id, | |
key, | |
name | |
FROM $streams_table | |
ORDER BY RANDOM() | |
LIMIT 1 | |
))->hash; | |
} | |
} | |
$db->query(qq( | |
UPDATE $streams_table | |
SET last_played = 0 | |
WHERE id <> ? | |
), | |
$stream->{'id'} | |
); | |
$db->query(qq( | |
UPDATE $streams_table | |
SET last_played = 1, | |
times_played = times_played + 1 | |
WHERE id = ? | |
), | |
$stream->{'id'} | |
); | |
$db->disconnect; | |
undef $db; | |
my $stream_url = sprintf( | |
"http://pub%d.di.fm/di_%s_aac", | |
$pubs[rand @pubs], # to randomize the requests | |
$stream->{'key'} | |
); | |
exec(qq(mplayer '$stream_url')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment