You are here: Home » Topic » FireStats – Top 40 php script (now with added charts)

FireStats – Top 40 php script (now with added charts)

FireFly Media Server (formerly mt-daapd) Firefly Media Server Forums Firefly Media Server Add-on Software FireStats – Top 40 php script (now with added charts)

This topic contains 175 replies, has 16 voices, and was last updated by  wwarren 9 years, 10 months ago.

Viewing 15 posts - 1 through 15 (of 176 total)
  • Author
    Posts
  • #1982

    sonichouse
    Participant

    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 "n";

    print "

    Firefly - Top $limit $mode

    n";
    print "nShow $Target";print "";

    if($mode=="Tracks")
    {

    print "ntnn";

    foreach ($dbh->query($query) as $row)
    {
    print "t";
    print "";
    print "";
    print "";
    print "";
    print "";
    print "n";
    }
    print "
    PlayedArtistAlbumTrack TitleTrack #
    $row[play_count]$row[Artist]$row[Album]$row[Track]$row[No]
    n";
    }

    if($mode=="Albums")
    {

    print "ntnn";

    foreach ($dbh->query($album_query) as $row)
    {
    print "t";
    print "";
    print "";
    print "";
    print "n";
    }
    print "
    Tracks PlayedAlbumArtist
    $row[Played]$row[Album]$row[Artist]
    n";
    }

    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

    #14690

    rpedde
    Participant

    @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 "ntnn";

    foreach ($dbh->query($query) as $row)
    {
    print "t";
    print "";
    print "";
    print "";
    print "";
    print "";
    print "n";
    }
    print "
    PlayedArtistAlbumTrack TitleTrack #
    $row[play_count]$row[Artist]$row[Album]$row[Track]$row[No]
    n";
    }

    if($mode=="Albums")
    {

    print "ntnn";

    foreach ($dbh->query($album_query) as $row)
    {
    print "t";
    print "";
    print "";
    print "";
    print "n";
    }
    print "
    Tracks PlayedAlbumArtist
    $row[Played]$row[Album]$row[Artist]
    n";
    }

    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

    #14691

    sonichouse
    Participant

    @rpedde wrote:

    Nice. Thanks for sharing this.
    — Ron

    Thanks 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

    #14692

    fizze
    Participant

    Nice 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. 🙂

    #14693

    sonichouse
    Participant

    @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 "ntnn";

    foreach ($dbh->query($query) as $row)
    {
    print "t";
    print "";
    print "";
    print "";
    print "";
    print "";
    print "n";
    }
    print "
    PlayedArtistAlbumTrack TitleTrack #
    $row[play_count]$row[Artist]$row[Album]$row[Track]$row[No]
    n";
    }

    if($mode=="Albums")
    {
    include "./libchart-1.2/libchart/classes/libchart.php";

    $chart = new PieChart(800,300);
    $dataSet = new XYDataSet();

    print "Pie chart
    ";
    print "
    ntnn";

    foreach ($dbh->query($album_query) as $row)
    {
    $n = "$row[Played]";
    print "t";
    print "";
    print "";
    print "";
    print "n";
    $dataSet->addPoint(new Point("$row[Album] - $row[Artist] ( $n )", $n));
    }
    print "
    Tracks PlayedAlbumArtist
    $row[Played]$row[Album]$row[Artist]
    n";

    /**
    * 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();
    }
    ?>
    #14694

    sonichouse
    Participant

    New release with Summary page here

    Enjoy 😀

    #14695

    rpedde
    Participant

    @sonichouse wrote:

    @rpedde wrote:

    Nice. Thanks for sharing this.
    — Ron

    Thanks 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

    #14696

    mas
    Participant

    Indeed. 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.

    #14697

    sonichouse
    Participant

    @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

    #14698

    sonichouse
    Participant

    Hi 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 [email protected]

    <Iframe src="http://localhost:8008/firefly.php"
    frameborder=0 marginwidth=-20 width=800 height=1800>


    @include [email protected]

    And then add a reference in hdr.html

    /Steve

    #14699

    rpedde
    Participant

    @sonichouse wrote:

    Hi 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 [email protected]

    <Iframe src="http://localhost:8008/firefly.php"
    frameborder=0 marginwidth=-20 width=800 height=1800>


    @include [email protected]

    And then add a reference in hdr.html

    /Steve

    Clearly I have to find a place for user contributed stuff. I keep wanting to work some on a nightlies page that doesn’t suck, along with a way for people to add contributed builds and stuff (like add-ons and whatnot), but my eyes are bigger than my stomach, or something.

    — Ron

    #14700

    sonichouse
    Participant

    A new version v1.01 has been uploaded here.

    Changes are

      1. Pie charts resized slightly to allow more information for track names.
      2. New Menu bar with ff color scheme
      3. Headers now left aligned to fit better with firefly web pages.

    #14701

    blamm
    Participant

    Hi.

    I would like to get this going on my PC. What config do I need to do to the files?

    #14702

    sonichouse
    Participant

    You need a web server capable of php.

    I use cherokee on my slug, but apache will also work.

    In functions.php change the path of the database if required.

    In the getFirefly function this defaults to localhost and port 3689, amend if required.

    The other thing is libchart, as detailed in previous posts.

    Steve

    #14703

    mas
    Participant

    http://www.lighttpd.net is what I would suggest. Also a small and fast webserver.

    You also need php-cgi, which you can install with (on debian)

    aptitude install php php-cgi lighttpd

    all together.

Viewing 15 posts - 1 through 15 (of 176 total)

The forum ‘Add-on Software’ is closed to new topics and replies.