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

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

Viewing 10 posts - 1 through 10 (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



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

Viewing 10 posts - 1 through 10 (of 176 total)
  • The forum ‘Add-on Software’ is closed to new topics and replies.