Reply To: counter per user

#7736
CCRDude
Participant

Well, 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.