FireFly Media Server › Firefly Media Server Forums › Firefly Media Server › General Discussion › counter per user
- This topic has 8 replies, 4 voices, and was last updated 17 years, 11 months ago by rpedde.
-
AuthorPosts
-
05/12/2006 at 3:34 PM #866WzrdOfOOstGuest
Hello I have been using mt-daapd server since a couple of months and wrote a bit of php code to display the number of played songs per user. it uses the log file to check. I am not a very experienced php-programmer actually this is my first bit of code. So don`t flame me…
Check my blog for my experiences with the daap server.
$myFile = "mt-daapd.log";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$address = array('/172.19.3.22/','/172.19.3.19/','/172.19.3.18/') ;
$name = array(john, Joe, Lisa);
for ( $counter = 0; $counter <= 2; $counter += 1) {
if (preg_match_all($address[$counter], $theData, $user)) {
echo $name[$counter]," ", count($user[0]), "
";
} else {
echo $name[$counter]," did not play any songs";
}
}
?>
[url][/url]
06/12/2006 at 5:20 AM #7732rpeddeParticipant@WzrdOfOOst wrote:
Hello I have been using mt-daapd server since a couple of months and wrote a bit of php code to display the number of played songs per user. it uses the log file to check. I am not a very experienced php-programmer actually this is my first bit of code. So don`t flame me…
Check my blog for my experiences with the daap server.
$myFile = "mt-daapd.log";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$address = array('/172.19.3.22/','/172.19.3.19/','/172.19.3.18/') ;
$name = array(john, Joe, Lisa);
for ( $counter = 0; $counter <= 2; $counter += 1) {
if (preg_match_all($address[$counter], $theData, $user)) {
echo $name[$counter]," ", count($user[0]), "
";
} else {
echo $name[$counter]," did not play any songs";
}
}
?>
[url][/url]
Neat! There are some interesting things you could do with that if you wanted to play in the database as well… Using php and sqlite, you could take results like that and make them an iTunes playlist by poking it right into the database. Then you could have iTunes playlists (for example), with the last 5 things everyone played.
Or the last 20 songs anyone has played. Or what have you.
Neat stuff you can do by reaching right into the database and poking around.
— Ron
06/12/2006 at 8:06 AM #7733fizzeParticipantyup!
I think this would play real nice together with riro’s DB “hack” π
only problem is, that the DB doesnt know whom it was played to the last time. So correlating the DB with the log file might be difficult….06/12/2006 at 10:41 AM #7734CCRDudeParticipantWasn’t there some problem that sqlite wasn’t designed for multiple concurrent accesses? I remember when I tried to fool around with the sqlite db while mt-daapd was running, that caused some problems.
Small thing about the php: unless you’ve logrotate setup for the logfile, you probably should read only the last n KB, otherwise you may need more memory than php got assigned.
06/12/2006 at 10:58 AM #7735WzrdOfOOstGuest@CCRDude wrote:
Small thing about the php: unless you’ve logrotate setup for the logfile, you probably should read only the last n KB, otherwise you may need more memory than php got assigned.
Thanks for your comment, but I do not quite understand what you mean. How do I read the last n KB of the file and still get all the information into the string?
06/12/2006 at 1:18 PM #7736CCRDudeParticipantWell, depends on what you need π
My thought was that the last 64 KB or so of text would be sufficient: would mean nearly 1000 log lines. Everything older probably doesn’t count as “last played” anyway π
$readsize = 65535;
fseek($fh, filesize($myFile) - $readsize);
$theData = fread($fh, $readsize);If you always want to parse the full file, I would recommend reading and parsing in blocks, like:
$readsize = 8192;
while (!feof($fh))
{ $theData = fread($fh, $readsize);
processTheData($theData);
}Which is untested code, just to demonstrate a bit what I mean π
The later would mean an improved parsing, but then that would have advantages as well, as you could split the text into an array of lines, and use grep on each line.Oh, one more thing: your for-loop can be made easier…
$names = array(john, Joe, Lisa);
foreach ($names as $name)
{ // you can use $name as the current array part in the loop now
}edit: fgets to read line by line would be even better of course π
edit2: I did just create a script myself that parses the full log file, and inserts each play event into a separate mysql db. Nice for creating some kind of statistics that’ll tell which song got played how often etc. π
Only thing thats not so nice about this is that the log does contain only the filename really. Something like full path, music data (without tags) checksum, or some other clear identifier that would survive a full db scan (or creating a new db) would be very nice πBtw here’s a full ereg()-Expression to separate each line into the proper fields:
([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}) (([0-9]{8})): Session ([0-9]{1,2}): Streaming file '([a-zA-Z0-9 .]*)' to ([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}) (offset ([0-9]{1,2}))
ereg() returns an array where you can access the following data by index:
0=full term, 1=date/time, 2=cardinal, 3=session id, 4=filename, 5=client ip, 6=offset.07/12/2006 at 3:58 AM #7737rpeddeParticipant@CCRDude wrote:
Wasn’t there some problem that sqlite wasn’t designed for multiple concurrent accesses? I remember when I tried to fool around with the sqlite db while mt-daapd was running, that caused some problems.
Yes. The db is locked during transactions, so you have to have a timeout. If you have timeouts set in the php code and you do your business quickly in the db, it should be okay. The server should keep retrying for some time (30 sec, iirc) until it can get a lock on the db.
07/12/2006 at 7:47 AM #7738fizzeParticipantjust out of curiosity, how big is the overhead of a running mySQL DB on a slug with mt-daapd vs. the sqlite?
I want to run swisscenter on a slug anyhow, which requires a DB. Right now Im thinking of running mt-daapd on the same slug, and using mySQL for both, then transactions etc wouldnt be that of an issue.
any clues?
08/12/2006 at 4:19 AM #7739rpeddeParticipant@fizze wrote:
just out of curiosity, how big is the overhead of a running mySQL DB on a slug with mt-daapd vs. the sqlite?
I want to run swisscenter on a slug anyhow, which requires a DB. Right now Im thinking of running mt-daapd on the same slug, and using mySQL for both, then transactions etc wouldnt be that of an issue.
any clues?
My understanding is that it pretty much tanks it. I had heard people say that they tried to run a lamp stack on it, and it wasn’t the php+apache that was the killer, it was the mysql.
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.