Skip to content

Commit

Permalink
[newui] Album art cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
klange committed May 14, 2011
1 parent 05f0b45 commit d4dfff6
Show file tree
Hide file tree
Showing 8 changed files with 423 additions and 120 deletions.
4 changes: 4 additions & 0 deletions bin/initdb-mysql.pl
Expand Up @@ -16,6 +16,7 @@
$db->do("DROP TABLE IF EXISTS players");
$db->do("DROP TABLE IF EXISTS playlists");
$db->do("DROP TABLE IF EXISTS playlist_contents");
$db->do("DROP TABLE IF EXISTS art_cache");

$db->do("CREATE TABLE songs (song_id INT UNSIGNED AUTO_INCREMENT, path
VARCHAR(1024) NOT NULL, artist VARCHAR(256), albumartist VARCHAR(256), album VARCHAR(256), title
Expand All @@ -37,3 +38,6 @@

$db->do("CREATE TABLE playlist_contents (playlist_id INT UNSIGNED, song_id INT
UNSIGNED, priority INT, UNIQUE(playlist_id,song_id))");

$db->do("CREATE TABLE art_cache (artist VARCHAR(256), album VARCHAR(256), title VARCHAR(256),
image BLOB, url VARCHAR(512))");
8 changes: 6 additions & 2 deletions bin/initdb-sqlite.pl
Expand Up @@ -16,6 +16,7 @@
$db->do("DROP TABLE IF EXISTS players");
$db->do("DROP TABLE IF EXISTS playlists");
$db->do("DROP TABLE IF EXISTS playlist_contents");
$db->do("DROP TABLE IF EXISTS art_cache");

$db->do("CREATE TABLE songs (song_id INTEGER PRIMARY KEY AUTOINCREMENT, path
VARCHAR(1024) NOT NULL, artist VARCHAR(256), albumartist VARCHAR(256), album VARCHAR(256), title
Expand All @@ -35,5 +36,8 @@
$db->do("CREATE TABLE playlists (who VARCHAR(256) NOT NULL, playlist_id INTEGER
PRIMARY KEY AUTOINCREMENT, title VARCHAR(256) NOT NULL)");

$db->do("CREATE TABLE playlist_contents (playlist_id INT, song_id INT
, priority INT, UNIQUE(playlist_id,song_id))");
$db->do("CREATE TABLE playlist_contents (playlist_id INT, song_id INT,
priority INT, UNIQUE(playlist_id,song_id))");

$db->do("CREATE TABLE art_cache (artist VARCHAR(256), album VARCHAR(256), title VARCHAR(256),
image BLOB, url VARCHAR(512))");
13 changes: 11 additions & 2 deletions index2.html
Expand Up @@ -12,8 +12,9 @@
<script src="www-data/jquery-ui-1.8.6.custom.min.js"></script>
<script src="www-data/jquery.tablesorter.min.js"></script>
<script src="www-data/jquery.address-1.3.min.js"></script>
<script src="www-data/acoustics2.js"></script>
<script src="www-data/jquery.reflection.js"></script>
<script src="www-data/jquery.favicon.js"></script>
<script src="www-data/acoustics2.js"></script>
</head>
<body>
<div id="header-bar">
Expand Down Expand Up @@ -271,6 +272,14 @@
<a href="javascript:unfullscreen()" class="control-button button-link">Close</a>
</div>
</div>
<div id="messageBox">no text... why?</div>
<div id="message-box">
<div id="message-box-inner">
<h1 id="message-box-title">Title</h1>
<span id="message-box-message">Content</span>
</div>
<div id="message-box-close">
<a class="control-button button-link" href="javascript:closeMessageBox();">Okay</a>
</div>
</div>
</body>
</html>
31 changes: 20 additions & 11 deletions json.pl
Expand Up @@ -34,19 +34,28 @@
my $mode = lc($q->param('mode') || '');
$mode = 'status' if $mode =~ /^_/ or $mode =~ /[^\w_]/ or $mode eq 'new';
$mode = 'status' unless $web->can($mode);

my($headers, $data) = $web->$mode;

$q->no_cache(1);
binmode STDOUT, ':utf8';
print $q->header(
@$headers,
-type => 'application/json',
);
print scalar JSON::DWIW->new({
pretty => 1,
escape_multi_byte => 1,
bad_char_policy => 'convert',
})->to_json($data);
my %headers = @$headers;

# If they don't specify a type, assume it is a data structure that we
# should encode to JSON and change the header accordingly.
unless ($headers{'-type'}) {
$headers{'-type'} = 'application/json';
$data = scalar JSON::DWIW->new({
pretty => 1,
escape_multi_byte => 1,
bad_char_policy => 'convert',
})->to_json($data);
$q->no_cache(1);
binmode STDOUT, ':utf8';
}

print $q->header(%headers);
if ($data) {
print $data;
}

# finish FastCGI if needed and auto-reload ourselves if we were modified
$req->Finish if $running_under_fastcgi;
Expand Down
90 changes: 90 additions & 0 deletions lib/Acoustics/Web.pm
Expand Up @@ -8,6 +8,7 @@ use Time::HiRes 'sleep';
use Moose;
use Module::Load 'load';
use List::Util 'shuffle';
use LWP::Simple;

has 'acoustics' => (is => 'ro', isa => 'Acoustics');
has 'cgi' => (is => 'ro', isa => 'Object');
Expand Down Expand Up @@ -917,5 +918,94 @@ sub stats
return [], $results;
}

=head2 art
Return album art image for the requested song.
=cut

sub art
{
my $self = shift;
my $artist = $self->cgi->param("artist");
my $album = $self->cgi->param("album");
my $title = $self->cgi->param("title");
my $set = $self->cgi->param("set");
my $size = 128;
if ($self->cgi->param("size")) {
$size = int($self->cgi->param("size"));
}

# It's okay if an argument is blank, for the most part.
if (!$artist) { $artist = ""; }
if (!$album) { $album = ""; }
if (!$title) { $title = ""; }
my $ret = {};

# Fix issues with albumless tracks
if (length $album < 1) {
$album = $title . "__" . $artist;
}

# This is a request to set to a specific URL.
if ($set && $set == "yes") {
my $url = $self->cgi->param("image");
my $image = get $url;
INFO("adding art cache for " . $title . ": " . $url);
$self->acoustics->query('delete_art_cache', {artist => $artist, album => $album, title => $title});
$self->acoustics->query('insert_art_cache', {artist => $artist, album => $album, title => $title, image => $image, url => $url });
return [], {};
}

# Check the database first.
my @queries = (
{artist => $artist, album => $album, title => $title},
{artist => $artist, album => $album},
{album => $album},
);

my @results;

# Try some queries until we get results.
while (!@results && @queries) {
@results = $self->acoustics->query('select_art_cache', shift @queries);
}

# We have a result from the database, dump that and bale.
if (@results) {
unless (length $results[0]->{image} > 10) {
# We found something, but we only have a URL stored for some reason.
# This could be because a plugin added it but didn't store the result.
$results[0]->{image} = get $results[0]->{url};
INFO("updating art cache for " . $results[0]->{title});
$self->acoustics->query('delete_art_cache', {artist => $artist, album => $album, title => $title});
$self->acoustics->query('insert_art_cache', {artist => $artist, album => $album, title => $title, image => $results[0]->{image}, url => $results[0]->{url} });
}
return [-type => "image/png"], $results[0]->{image};
}

# XXX: Extension hooks should be added here to
# pull from LastFM / Amazon / whatever

# Fall back to local icon.
my $last_try;
if ($size < 100) {
$last_try = "www-data/icons/cd_big.png";
} else {
$last_try = "www-data/icons/big_a.png";
}
open IMAGE, $last_try;
local $/;
$ret->{image} = <IMAGE>;
close IMAGE;


# TODO: We should probably use Perl's GD bindings to resize
# the result image to the requested size. I'm not
# entirely sure whether I want to, though - klange

return [-type => "image/png"], $ret->{image};
}


1;
40 changes: 36 additions & 4 deletions www-data/acoustics2.css
Expand Up @@ -919,13 +919,45 @@ a img {
border-top-right-radius: 15px;
}

#messageBox {
font-size: 12px;
}

.mini-album-art {
height: 16px;
width: 16px;
vertical-align: middle;
padding-right: 2px;
}

#message-box {
position: absolute;
top: 50%;
left: 50%;
width: 600px;
height: 120px;
margin-top: -60px;
margin-left: -300px;
z-index: 3;
padding: 20px;
padding-top: 5px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
-moz-box-shadow: 5px 5px 5px rgba(0,0,0,0.5);
-webkit-box-shadow: 5px 5px 5px rgba(0,0,0,0.5);
box-shadow: 5px 5px 5px rgba(0,0,0,0.5);
border: 1px solid #B62222;
background-color: #EC3232;
background-color: rgba(236,50,50,0.9);
color: #FFF;
text-align: center;

display: none;
}
#message-box div h1 {
font-size: 16px;
font-weight: bold;
}
#message-box-close {
position: absolute;
right: 10px;
bottom: 10px;
}

0 comments on commit d4dfff6

Please sign in to comment.