Sync "rating" and "last played" with MusicMagic Mixer

My code was done with the help of Andy’s code, thanks for start up :).

It will sync your whole Media Library in WinAmp to the MusicMagic database. You can tweak the Media Library query string to make it sync less files (for example, only recently heard ones).

A discussion of this code can be found here. The version history is below the code.

Notice that you need at least MMM 1.1.6 for this script to work well.

Set wo = CreateObject("ActiveWinamp.Application")
 
'turn on error handling
On Error Resume Next
Err.Clear
 
Set WshShell = CreateObject("WScript.Shell")
 
' finding path for log file
log_file = WshShell.RegRead ("HKLM\SOFTWARE\Predixis\MusicMagic Mixer\1.1\LogFileName")
if log_file = "" then
	log_file = "C:\ML_To_MMM_ErrorLog.txt"
	call WshShell.RegWrite("HKLM\SOFTWARE\Predixis\MusicMagic Mixer\1.1\LogFileName", log_file)
end if
 
Dim fso, f1, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile (log_file)
Set f1 = fso.GetFile(log_file)
Set ts = f1.OpenAsTextStream(ForWriting, True)
 
ts.WriteLine("Beginning Error Logging ....")
 
Set http = CreateObject("Microsoft.XmlHttp")
 
Set fso = CreateObject("Scripting.FileSystemObject")
 
query = "select * from Win32_Process where Name = 'MusicMagicMixer' or Name = 'MusicMagicMixer.exe'"
Set wmiProcesses = GetObject("winmgmts:").ExecQuery(query)
 
' running MusicMagic Mixer if necessary
If wmiProcesses.count < 1 Then
    path = WshShell.RegRead ("HKLM\SOFTWARE\Predixis\MusicMagic Mixer\1.1\InstallDir")
    MMMapp = """" & path & "\MusicMagicMixer.exe"""
    WshShell.Run MMMapp, 7, False
End if
 
' getting last sync
LastSync = WshShell.RegRead ("HKLM\SOFTWARE\Predixis\MusicMagic Mixer\1.1\LastWinampSync")
If LastSync = "" then
	LastSync = "1/1/1970"
	MsgBox("Running the WinAmp sync for the first time, it might a few minutes for a big collection. Please hang on.")
End If
 
' fixing date incompatibilty
Dim dtTemp, szMonth, szDay, szYear
dtTemp = LastSync
szMonth = Month(dtTemp)
szDay = Day(dtTemp)
szYear = Year(dtTemp)
LastSync = szMonth & "/" & szDay & "/" & szYear & " " & timevalue(dttemp)
ts.writeline("Sync for files played later than: " & LastSync)
 
query = "(type = 0) AND (lastplay >= ["&LastSync&"])"
'MsgBox(query)
mlq = wo.medialibrary.runqueryarray(query)
'mlq = wo.medialibrary.runqueryarray("(type == 0) AND ((lastplay >= [14 days ago]))")
 
'MsgBox("Importing all song data into MusicMagic Mixer, it might take a few minutes for a big collection. Please hang on.")
 
i = 0
error_count = 0
for each track in mlq	
 
	'save original filename
	filename1 = track.filename
 
	filename = URLEncode(track.filename)
 
 
	url = "http://localhost:10002/api/setPlayCount?song=" & filename & "&count=" & track.playcount
	http.open "PUT", url, FALSE
	http.send
	url = "http://localhost:10002/api/setRating?song=" & filename & "&rating=" & track.rating
	http.open "PUT", url, FALSE
	http.send
	url = "http://localhost:10002/api/setLastPlayed?song=" & filename & "&time=" & track.lastplay
	http.open "PUT", url, False
	http.send
 
	response = AscB(http.responseBody)
 
	If response = 48 Then
		error_count = error_count + 1
		ts.WriteLine("Failed sync: " & filename1 & " (encoded as: " & filename & ").")
	End If
 
	i=i+1
Next
 
call WshShell.RegWrite("HKLM\SOFTWARE\Predixis\MusicMagic Mixer\1.1\LastWinampSync", Now)
 
msgbox("Done! " & i & " items were updated." & vbCrLf & "Items with errors: " & error_count & vbCrLf & "You can see the log file here: " & log_file)
 
ts.WriteLine(".... Error Logging Complete.")
 
quit
 
Function URLEncode(text)
   Dim i, acode, char, translate
   translate = True
   URLEncode = text
   For i = Len(URLEncode) To 1 Step -1
	   char = Mid(URLEncode, i, 1)
       acode = Asc(Mid(URLEncode, i, 1))
       if (acode = 32) or (acode >= 48 and acode <= 57) or (acode >= 65 and acode <= 90) or (acode >= 97 and acode <= 122) Then
           translate = False
       End If
       If acode >= 224 And acode <= 250 Then
           URLEncode = Mid(URLEncode, 1, i-1) & ChrW(acode+1264) & Mid(URLEncode, i+1, len(URLEncode))
           translate = False
       End if
       if acode = 32 Then
           ' replace space with "%20"
           URLEncode = Mid(URLEncode, 1, i-1) & "%20" & Mid(URLEncode, i+1, len(URLEncode))
           translate = False
       End If
       if translate = True Then
           ' replace punctuation chars with "%hex"
           URLEncode = Left(URLEncode, i - 1) & "%" & Hex(acode) & Mid(URLEncode, i + 1)
       End If
       translate = true
   Next
End Function

Version history

23/10/2005

  • Fixed the bug with the wrong time format that caused 0 items to be updated.
  • The log file location is not stored in the registry.

20/10/2005

  • Now it writes the last time you ran the script into the registry and the next only the songs that were listened after that will be updated. In other words, a major boost in update time.

30/9/2005

  • Handles correctly songs with special characters and foreign letters
  • Logs the errors into an external file
  • Now works correctly on both WinXP and Win2000
  • Can be run as an external file - When you run it, it’d automatically run both MMM and WinAmp, if they are not already running and then update your songs.

Credits

 
examples/application/sync_rating_and_last_played_with_musicmagic_mixer.txt · Last modified: 2006/11/06 15:44
 
Recent changes RSS feed Creative Commons License Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
Music Plugins