FireFly Playcount Updater

This forum is for questions / discussions regarding development of addons / tweaks for MediaMonkey.

Moderators: Gurus, Addon Administrators

monitoringpost
Posts: 48
Joined: Sun Oct 08, 2006 12:43 pm

FireFly Playcount Updater

Post by monitoringpost »

I wrote the following script sometime ago under v2 and it worked flawlessly. I've been meaning to update it to v3 but as you can see it's been awhile. Anyways, although the script does read the FireFly logs correctly the script doesn't update MM v3 at all. I know some of the code syntax has changed but quite honestly just haven't been able to get into it. So, I'm posting the code here in hopes that other might update it and find some value. Anybody have any questions let me know. Hopefully, we can get this running again...

Code: Select all

'//////////////////////////////////////
' Firefly|MediaMonkey Playcount Updater
'//////////////////////////////////////

'Written by: MonitoringPost aka Iris
      'Date: December 8th, 2008
   'Version: 1.0.1

'Place in .ini:
'[AddFireflyPlaycounts]
'FileName=ImportFireflyPlaycounts.vbs
'ProcName=AddFireflyPlaycounts
'Order=11
'DisplayName=&Import Firefly Media Server Playcounts
'Description=Updates MediaMonkey with Firefly Media Server Plays
'Language=VBScript
'ScriptType=0

'Option Explicit

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Sub AddFireflyPlaycounts

   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set file1 = objFSO.OpenTextFile("\\Mss-hdd\Public\Our Documents\Firefly.log",ForReading)

   'Define vars
   Dim DB : Set DB = SDB.Database
   Dim file1, file2, file3
   Dim strContents, arrLines
   Dim strArtist, strTrack
   Dim list, itm, i, j, x
   Dim lastSyncDate, Count
   Dim simulation
   
   simulation = False 'Change value to "True" for simulated run
   strCheckForString = "Session 0: Streaming file '"
   
   'Create Simulation Report
   If simulation = True Then
     Set file2 = objFSO.OpenTextFile(Script.ScriptPath&".log",ForWriting,True)
     file2.WriteLine "Firefly Results - Simulated Run " & Now()
     file2.WriteLine "-----------------------------------------------------"
   End If
   
   'Read Date of Last Synchronization
    Set file3 = objFSO.OpenTextFile(Script.ScriptPath&".ini",ForReading)
    Do Until file3.AtEndOfStream
      lastSyncDate = Mid(file3.ReadLine,23,22)
    Loop
    file3.Close
    
   'Initialize progress bar
   Set Progress = SDB.Progress
   Progress.Text = "Processing..."
   
   'Parse Firefly log
   'Minimum debug level of '5' needs to be set under Firefly's
   'advanced configuration for the necessary log entries to be
   'generated.
   strContents = file1.ReadAll
   file1.Close
   
   arrLines = Split(strContents, vbLf)
   Progress.MaxValue = UBound(arrLines)
   For x = 0 To UBound(arrLines)
     strLength = Len(arrLines(x))
     If Mid(arrLines(x),33,Len(strCheckForString)) = strCheckForString Then
       'Format Date & Time to computer's regional settings
       strDate = FormatDateTime(Left(arrLines(x),10),2)
       strTime = FormatDateTime(Mid(arrLines(x),12,8),3)
       'Check that played date was logged after last run of script
       If DateDiff("s",lastSyncDate,strDate & " " & strTime) > 0 Then 'continue processing
         'Add additional 'Else If' statements for file types as needed
         If InStr(1,arrLines(x),".mp3",1) > 0 Then
           ext = "mp3" : y = 3 'y equals number of chars in extension
           FileLength = InStr(1,arrLines(x),".mp3",1)
         ElseIf InStr(1,arrLines(x),".wma",1) > 0 Then
           ext = "wma" : y = 3
           FileLength = InStr(1,arrLines(x),".wma", 1)
         ElseIf InStr(1,arrLines(x),".wav",1) > 0 Then
           ext = "wav" : y = 3
           FileLength = InStr(1,arrLines(x),".wav", 1)
         ElseIf InStr(1,arrLines(x),".ogg",1) > 0 Then
           ext = "ogg" : y = 3
           FileLength = InStr(1,arrLines(x),".ogg", 1)
         ElseIf InStr(1,arrLines(x),".flac",1) > 0 Then
           ext = "flac" : y = 4
           FileLength = InStr(1,arrLines(x),".flac", 1)
         End If
         'Parse track filename for Artist Name & Song Title
         'using the mask: <ArtistName> - <Title>
         strTrackFilename = Mid(arrLines(x),60,FileLength - 60) & "." & ext
         strArtist = Mid(strTrackFilename,1,InStr(1,strTrackFilename,"-",1)-2)
         strTrack = Mid(strTrackFilename,Len(strArtist) + 4)
      
         'Retrieve list of selected tracks from MediaMoney
         Set list = SDB.CurrentSongList
         If list.Count = 0 Then
           Exit Sub
         End If
          
        'Process selected tracks from MediaMonkey
         For i=0 To list.count-1
           Set itm = list.Item(i)
           'Check for Match
           If ext = Right(itm.Path,y) Then
             arrPath = Split(itm.Path,"\")
             For j=0 To UBound(arrPath)
               strTrackFilename2 = arrPath(j)
             Next
             If strTrackFileName = strTrackFileName2 Then
               Progress.Text = "Processing Track: " & strArtist & " - " & strTrack
                 If simulation = True Then
                   If DateDiff("s",strDate & " " & strTime,itm.LastPlayed) < 0 Then
                     file2.WriteLine strDate & " " & FormatDateTime(strTime,4) & " " & strArtist & " - " & strTrack
                   Else
                     file2.WriteLine strArtist & " - " & strTrack & " " & "Played: " & itm.LastPlayed
                   End If
                 Else
                   Count = Count + 1
                   DB.ExecSQL("UPDATE Songs SET PlayCounter=" & (itm.PlayCounter + 1) & " WHERE Id=" & itm.songID)
                   If DateDiff("s",strDate & " " & strTime,itm.LastPlayed) < 0 Then 'Update the Last Time Played
                     DB.ExecSQL("UPDATE Songs SET LastTimePlayed='" & strDate & " " & strTime & "' WHERE Id=" & itm.songID)
                     DB.ExecSQL("INSERT INTO Played (IdSong, PlayDate) VALUES (" & itm.songID & ", '" & strDate & " " & strTime & "')")
                   Else 'Leave the time alone
                     DB.ExecSQL("UPDATE Songs SET LastTimePlayed='" & itm.LastPlayed & "' WHERE Id=" & itm.songID)
                     DB.ExecSQL("INSERT INTO Played (IdSong, PlayDate) VALUES (" & itm.songID & ", '" & itm.LastPlayed & "')")
                   End If
                   'Refresh Screen
                   itm.PlayCounter = itm.playcounter + 1
                 End If
             End If
           End If
         Next
       End If 'Track skipped - it's already been synchronized
     End If 'Log entry not valid so skip 
   Progress.Increase
   Next
'Finished - so clean-up
Set Progress = Nothing
If simulation = True Then
  file2.Close
End If

'Log Results
If Count > 0 Then
  Set file3 = objFSO.OpenTextFile(Script.ScriptPath&".ini",ForAppending,True)
  file3.WriteLine "Last Synchronization: " & Now() & " Tracks Synchronized: " & Count
  file3.Close
End If
End Sub]
Roku M1001 v3.0.52 beta | R1000 v3.0.52 beta
XBMC vPre2.1 SVN rev13360
NAS: Maxtor SS+ v2.6.2-openmss1-rc2
Linux Server: Firefly svn-1586-1 mipsel pkg
Router: Linksys WRT54GS
MediaMonkey v3.2.5.1306 | iRock Remote on iPhone
... and one kick ass rig!

Image