Monday, December 04, 2006

mt-daapd play counter

Hello,

Since a couple of months I use the mt-daapd software to make my server share my music collection over my local network. I posted about this a while ago. Since I live with 8 people in a house I was very curious if anybody besides me was using the music server, and how often do they play songs. I found out that the admin pages show how many songs are played but not who has played them.

than I found out that the daap-server could keep a log of all actions going on. The log looks like this
2006-12-02 23:17:48: Session 0: Streaming file help.mp3' to 172.19.3.22 (offset 0)
2006-12-02 23:17:49: Session 0: Streaming file 'yesterday.mp3' to 172.19.3.22 (offset 0)
2006-12-02 23:19:54: Session 0: Streaming file 'imagine.mp3' to 172.19.3.22 (offset 0)
Since I installed my php server I thought it must be possible to write some php code to count the ip-adresses of the different users. It took a while but finally it worked.
The php code is probably not as efficient as possible. If you know an easier way please comment:

?php
$myFile = "mt-daapd.log";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);

if (preg_match_all("/172.19.3.22/", $theData, $john)) {
echo "John ", count($john[0]), "
";
} else {
echo "John did not play any songs";
}
if (preg_match_all("/172.19.3.18/", $theData, $jef)) {
echo "jef ", count($jef[0]), "
";
} else {
echo "Jef did not play any songs";
}

if (preg_match_all("/172.19.3.19/", $theData, $lisa)) {
echo "Lisa ", count($lisa[0]);
} else {
echo "Lisa did not play any songs";
}
?>

No comments: