FireFly Media Server › Firefly Media Server Forums › Firefly Media Server › Add-on Software › FireStats – Top 40 php script (now with added charts)
- This topic has 175 replies, 17 voices, and was last updated 15 years, 10 months ago by wwarren.
-
AuthorPosts
-
25/11/2007 at 4:22 PM #1982sonichouseParticipant
I have butchered together a script to find out what the most popular tracks are on my Firefly server.
The script is written in php, and has some hardcoded paths for the database. I am new to php so this could be more elegant.
Just copy the firefly.css to your web folder (in my case it was /opt/share/www/cherokee) and then create firefly.php as
if(isset($_GET))
$mode = $_GET;
else
$mode = "Tracks";
try {
if($mode=="Tracks") {
$Target = "Albums";
$limit=40;
} else {
$Target = "Tracks";
$limit=10;
}
$query="
select Artist,Album,title as Track,play_count,track as No
from songs
where play_count >= 1
group by 4,1,2,3,5
order by 4 desc,1 asc,2 asc,5
limit $limit";
$album_query="
select Album,Artist,sum(play_count) as Played
from songs
where play_count > 0 and album<>''
group by 1,2
having sum(play_count) >= 1
order by 3 desc,1 asc,2 asc
limit $limit";
$dbh = new PDO('sqlite:/opt/var/mt-daapd/songs3.db');
$dbh->exec('SET CHARACTER SET UTF8');
print "nnn";
print "n";
print "Firefly - Top $limit $mode
n";
print "nShow $Target";print "";
if($mode=="Tracks")
{
print "nt
Played Artist Album Track Title Track # nn";
foreach ($dbh->query($query) as $row)
{
print "t"; n";
print "$row[play_count] ";
print "$row[Artist] ";
print "$row[Album] ";
print "$row[Track] ";
print "$row[No] ";
print "
}
print "n";
}
if($mode=="Albums")
{
print "nt
Tracks Played Album Artist nn";
foreach ($dbh->query($album_query) as $row)
{
print "t"; n";
print "$row[Played] ";
print "$row[Album] ";
print "$row[Artist] ";
print "
}
print "n"; nn";
}
print "
$dbh = null;
}
catch (PDOException $e)
{
print "Error!: " . $e->getMessage() . "
";
die();
}
?>
Once I learn how to add some nice graphics etc this can only imrove.
[Edit]
Updated to toggle between Tracks and Albums
[/Edit]/Steve
26/11/2007 at 3:51 AM #14690rpeddeParticipant@sonichouse wrote:
I have butchered together a script to find out what the most popular tracks are on my Firefly server.
The script is written in php, and has some hardcoded paths for the database. I am new to php so this could be more elegant.
Just copy the firefly.css to your web folder (in my case it was /opt/share/www/cherokee) and then create firefly.php as
<?php
if(isset($_GET))
$mode = $_GET;
else
$mode = "Tracks";
try {
if($mode=="Tracks") {
$Target = "Albums";
$limit=40;
} else {
$Target = "Tracks";
$limit=10;
}
$query="
select Artist,Album,title as Track,play_count,track as No
from songs
where play_count >= 1
group by 4,1,2,3,5
order by 4 desc,1 asc,2 asc,5
limit $limit";
$album_query="
select Album,Artist,sum(play_count) as Played
from songs
where play_count > 0 and album''
group by 1,2
having sum(play_count) >= 1
order by 3 desc,1 asc,2 asc
limit $limit";
$dbh = new PDO('sqlite:/opt/var/mt-daapd/songs3.db');
$dbh->exec('SET CHARACTER SET UTF8');
print "nnn";
print "th{font-size: 7pt}td{font-size: 7pt; padding: 2 1em 0 1em;border-right: thin solid; border-bottom: thin dotted;}n";
print "Firefly - Top $limit $mode
n";
print "nShow $Target";print "";
if($mode=="Tracks")
{
print "nt
n";Played Artist Album Track Title Track # nn";
foreach ($dbh->query($query) as $row)
{
print "t"; n";
print "$row[play_count] ";
print "$row[Artist] ";
print "$row[Album] ";
print "$row[Track] ";
print "$row[No] ";
print "
}
print "
}
if($mode=="Albums")
{
print "nt
n";Tracks Played Album Artist nn";
foreach ($dbh->query($album_query) as $row)
{
print "t"; n";
print "$row[Played] ";
print "$row[Album] ";
print "$row[Artist] ";
print "
}
print "
}
print "nn";
$dbh = null;
}
catch (PDOException $e)
{
print "Error!: " . $e->getMessage() . "
";
die();
}
?>
Once I learn how to add some nice graphics etc this can only imrove.
[Edit]
Updated to toggle between Tracks and Albums
[/Edit]/Steve
And this is the best example yet of “why sqlite”. The gdbm backend was faster than sqlite, but you couldn’t do this kind of thing with gdbm as backend.
Nice. Thanks for sharing this.
— Ron
26/11/2007 at 10:31 AM #14691sonichouseParticipant26/11/2007 at 3:05 PM #14692fizzeParticipantNice chart π
For sqlite2 users, you might want to change line 35 to
$dbh = new PDO('sqlite2:/opt/var/mt-daapd/songs.db');
Works otherwise like a charm though. π
26/11/2007 at 3:42 PM #14693sonichouseParticipant@fizze wrote:
Nice chart π
Glad you liked it, the latest version has a dependency on libchart.php from http://naku.dohcrew.com/libchart/pages/introduction/
The code is
<?php
if(isset($_GET))
$mode = $_GET;
else
$mode = "Tracks";
try {
if($mode=="Tracks") {
$limit=40;
$Target = "Albums";
} else {
$limit=10;
$Target = "Tracks";
}
$query="
select Artist,Album,title as Track,play_count,track as No
from songs
where play_count >= 1
group by 4,1,2,3,5
order by 4 desc,1 asc,2 asc,5
limit $limit";
$album_query="
select Album,Artist,sum(play_count) as Played
from songs
where play_count > 0 and album''
group by 1,2
having sum(play_count) >= 1
order by 3 desc,1 asc,2 asc
limit $limit";
$dbh = new PDO('sqlite:/opt/var/mt-daapd/songs3.db');
$dbh->exec('SET CHARACTER SET UTF8');
print "nnn";
print "th{font-size: 7pt}td{font-size: 7pt; padding: 2 1em 0 1em;border-right: thin solid; border-bottom: thin dotted;}n";
print "Firefly - Top $limit $mode
n";
print "nShow $Target";print "
";
if($mode=="Tracks")
{
print "nt
n";Played Artist Album Track Title Track # nn";
foreach ($dbh->query($query) as $row)
{
print "t"; n";
print "$row[play_count] ";
print "$row[Artist] ";
print "$row[Album] ";
print "$row[Track] ";
print "$row[No] ";
print "
}
print "
}
if($mode=="Albums")
{
include "./libchart-1.2/libchart/classes/libchart.php";
$chart = new PieChart(800,300);
$dataSet = new XYDataSet();
print "
";
print "nt
n";Tracks Played Album Artist nn";
foreach ($dbh->query($album_query) as $row)
{
$n = "$row[Played]";
print "t"; n";
print "$row[Played] ";
print "$row[Album] ";
print "$row[Artist] ";
print "
$dataSet->addPoint(new Point("$row[Album] - $row[Artist] ( $n )", $n));
}
print "
/**
* Pie chart demonstration
*
*/
$chart->setDataSet($dataSet);
$chart->setTitle("Top $limit $mode");
$chart->render("$mode.png");
}
print "nn";
$dbh = null;
}
catch (PDOException $e)
{
print "Error!: " . $e->getMessage() . "
";
die();
}
?>
27/11/2007 at 10:09 AM #14694sonichouseParticipantNew release with Summary page here
Enjoy π
28/11/2007 at 5:14 AM #14695rpeddeParticipant@sonichouse wrote:
@rpedde wrote:
Nice. Thanks for sharing this.
— RonThanks Ron,
It is nice to give something back π
To see it in action check this out.
The Pie Chart rendering is quite a hit on the old under-clocked slug π
If you click the Tracks link, you can see how fast it could be./Steve
That’s seriously sweet.
— Ron
28/11/2007 at 7:14 PM #14696masParticipantIndeed. Tried it out also. Hehe I had to increase the php memory limit from 12MB to 24MB to make it run. Thats also why its such a hit and takes like 10 secs to display a chart. Indeed a hit on the slug but works nicely.
And it seems the version on your server is newer than the posted one here.
29/11/2007 at 12:15 AM #14697sonichouseParticipant@mas wrote:
And it seems the version on your server is newer than the posted one here.
I am in the middle of re-factoring, adding functions instead of repeated in-line code.
This is my first php adventure, so my code is improving slowly.
Thankfully my C coding days were only about 4-5 years ago, and it is all slowly coming back to me.I will post a zip file when I feel it is in a more maintainable state.
/Steve
29/11/2007 at 2:39 PM #14698sonichouseParticipantHi All,
The first release (v1.0) is available to download from here.
As previously mentioned this relies on phpChart from here.
Here is a teaser of what it produces
Feedback and comments always welcome.
You can even embed it in your Firefly web pages by creating FireStats.html as
@include hdr.html@
<Iframe src="http://localhost:8008/firefly.php"
frameborder=0 marginwidth=-20 width=800 height=1800>
@include ftr.html@
And then add a reference in hdr.html
/Steve
-
AuthorPosts
- The forum ‘Add-on Software’ is closed to new topics and replies.