Script to increase PlayCounter

Post a reply

Smilies
:D :) :( :o :-? 8) :lol: :x :P :oops: :cry: :evil: :roll: :wink:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Script to increase PlayCounter

by MeMeMe » Thu Jan 05, 2006 1:38 pm

Here's the code I used.

This file was named IncrPlayCntr.vbs

Code: Select all

' increments play counter 
' Source: http://www.mediamonkey.com/forum/viewtopic.php?t=1461 
' modified to work with a non-English date format and to also update the "Played" table. 

Option Explicit 

Sub IncrPlayCntr 
  ' Define variables 
  Dim list, itm, i 

  ' Get list of selected tracks from MediaMonkey 
  Set list = SDB.SelectedSongList 
  If list.count=0 Then 
    Set list = SDB.AllVisibleSongList 
  End If 

  ' Process all selected tracks 
  For i=0 To list.count-1 
    Set itm = list.Item(i) 
    'must use sql because itm.UpdateDB does not update the PlayCounter property 
    SDB.database.execSQL("UPDATE Songs SET PlayCounter=" & (itm.PlayCounter + 1) & " WHERE Id=" & itm.songID) 
    SDB.database.execSQL("UPDATE Songs SET LastTimePlayed='" & Now() & "' WHERE Id=" & itm.songId) 
    ' there is another DB table that stores when the song was played, 
    ' so in order to stay consistent, we update this, too. 
    ' This table is used for the "50 last played songs" playlist 
    SDB.database.execSQL("INSERT INTO Played (IdSong, PlayDate) VALUES (" & itm.songId & ", '" & Now() & "')") 
    
    'refreshes screen 
    itm.playcounter = itm.playcounter + 1 
    itm.UpdateDB 
  Next 
End Sub
This is PlayCounter.vbs

Code: Select all

Sub SetPlayCounter 

  ' Define variables 
  Dim list, itm, i, newPlayCounter, mb, progress 
  
  'Get a new value for the Play Counter from the user 

  newPlayCounter = InputBox("Enter the new value for the Played field", "Set Play Counter") 
  
  'If Canceled, exit 
  If  newPlayCounter = "" Then 
     Exit Sub 
  End If 
  
  'Check that the text entered is a valid parameter. Inform user if it isn't. 
  If Not IsNumeric(newPlayCounter) Then 
     mb = MsgBox("You did not enter a number. Please try again.",0,"Error") 
     Exit Sub 
  ElseIf newPlayCounter < 0 Then 
     mb = MsgBox("Only positive numbers or 0 are allowed. Please try again.",0,"Error") 
     Exit Sub 
  End If 

  ' Get list of selected tracks from MediaMonkey 
  Set list = SDB.SelectedSongList 
  If list.count=0 Then 
     Set list = SDB.AllVisibleSongList 
  End If  
  
  'No songs selected? 
  If list.count = 0 Then 
     mb = MsgBox("No songs were selected. Please select some songs and try again",0,"Error") 
     Exit Sub 
  End If 
  
  'Set up progress 
  Set Progress = SDB.Progress 
  Progress.Text = "Writing New Play Counters..." 
  Progress.MaxValue = list.count 

  'Process all selected tracks 
  For i=0 To list.count-1 
    Set itm = list.Item(i) 
    'Set the Play Counter 
    itm.PlayCounter = newPlayCounter 
    itm.UpdateDB 
    Progress.value = i+1 
    If Progress.terminate Then 
       Exit For 
    End if    
  Next 
  
  Set Progress =  nothing 
  
End Sub 

Sub ResetPlayCounter 
    
   'This is drastic, so require confirmation from user 
    
   Dim yesimsure 

   yesimsure = InputBox("Are you sure you want to reset the play counter for all tracks in the Library? Write YES to proceed. Note: You will have to refresh the view (F5) in order to see the changes.", "Reset Play Counter") 
    
   If yesimsure <> "YES" Then 
      Exit Sub 
   End If 

   'Reset the counter... 
   SDB.Database.ExecSQL("UPDATE SONGS SET PLAYCOUNTER = 0 WHERE TRUE=TRUE") 

End Sub 

Sub RestorePlayCounter 

   'This is drastic, so require confirmation from user 
    
   Dim yesimsure 

   yesimsure = InputBox("Are you sure you want to restore the play counter to the number of times each track was played in MediaMonkey? Write YES to proceed. Note: You will have to refresh the view (F5) in order to see the changes.", "Restore Play Counter") 
    
   If yesimsure <> "YES" Then 
      Exit Sub 
   End If 

   Dim Str1, Str2, Str3, Str4, Str5 
    
   'Create a temporary table which stores song IDs and the number of times each song was played 
   Str1 = "SELECT Songs.ID, timesPlayed INTO updateCounter " 
   Str2 = "FROM Songs, [SELECT Played.idSong as SID, Count(Played.idSong) AS timesPlayed  FROM Played GROUP BY Played.idSong]. AS subq " 
   Str3 = "WHERE Songs.Id =subq.SID" 
    
   SDB.Database.ExecSQL(Str1 & Str2 & Str3) 
    
   'Use the table just created to update the playCounter field in the songs table 
   Str4 = "UPDATE Songs, updateCounter SET songs.playcounter = updateCounter.timesPlayed " 
   Str5= "WHERE songs.id=updateCounter.id" 
    
   SDB.Database.ExecSQL(Str4 & Str5) 
    
   'Delete temporary table 
   SDB.Database.ExecSQL("DROP TABLE updateCounter") 
    

End Sub

Function Min(x, y) 
   If x<y Then 
      Min = x 
   Else 
      Min = y 
   End If 
End Function 
          
Function Max(x, y) 
   If x>y Then 
      Max = x 
   Else 
      Max = y 
   End If 
End Function 

Sub SetPlayCounterAdvanced 

  ' Define variables 
  Dim list, itm, i, userInput, newC , mb, progress, C 
  
  'Get a new value for the Play Counter from the user 

  userInput = InputBox("Enter a formula for the new play counter. Use C to refer to the current value. Examples: 0, C+1, Max(1,C).", "Set Play Counter (Advanced)") 
  
  'If Canceled, exit 
  If  userInput = "" Then 
     Exit Sub 
  End If 
  

  ' Get list of selected tracks from MediaMonkey 
  Set list = SDB.SelectedSongList 
  If list.count=0 Then 
     Set list = SDB.AllVisibleSongList 
  End If  
  
  'No songs selected? 
  If list.count = 0 Then 
     mb = MsgBox("No songs were selected. Please select some songs and try again",0,"Error") 
     Exit Sub 
  End If 
  
  'Set up progress 
  Set Progress = SDB.Progress 
  Progress.Text = "Writing New Play Counters..." 
  Progress.MaxValue = list.count 

  'Process all selected tracks 
  For i=0 To list.count-1 
  
    Set itm = list.Item(i) 
    C = itm.playCounter 
    'Evaluate user input 
    newC = Eval(userInput) 
    
    'Set the Play Counter 
    itm.PlayCounter = newC 
    itm.UpdateDB 
    Progress.value = i+1 
    If Progress.terminate Then 
       Exit For 
    End if    
  Next 
  
  Set Progress =  nothing 
  
End Sub
And I added these to the script.ini file:

Code: Select all


[SetPlayCounterAdvanced] 
FileName=PlayCounter.vbs 
ProcName=SetPlayCounterAdvanced 
Order=20 
DisplayName=Set Play Counter (Advanced) 
Description=Modify the play counter by setting it to a numerical value or a formula depending on the current value 
Language=VBScript 
ScriptType=0

[ChangePlayCounter] 
FileName=PlayCounter.vbs 
ProcName=ChangePlayCounter 
Order=20 
DisplayName=Change Play Counter 
Description=Change the play counter of the selected files 
Language=VBScript 
ScriptType=0 

[ResetPlayCounter] 
FileName=PlayCounter.vbs 
ProcName=ResetPlayCounter 
Order=21 
DisplayName=Reset Play Counter 
Description=Reset the play counter of all files in the library 
Language=VBScript 
ScriptType=0

[IncrementPlayCounter] 
FileName=IncrPlayCntr.vbs 
ProcName=IncrPlayCntr 
Order=20 
DisplayName=Increment Play Counter 
Description=Increment the play counter by 1 
Language=VBScript 
ScriptType=0

by Guest » Thu Jan 05, 2006 1:20 pm

please tell me what page you got the code from in that message so i can go get the same code and see if a reinstll of it will work for me.

thanks. :lol:

roving cowboy / keith hall
just checking back.

by MeMeMe » Thu Jan 05, 2006 12:34 pm

I have used the Advanced conter and Increment counter today, and they worked fine.

by Guest » Thu Jan 05, 2006 12:12 pm

i still can not get that script to work in version 2.5 it only changes the count to 0 no matter what i tell it to adjust it to?

is there anyone updating that script to work in 2.5 monkey.?

:(

roving cowboy / keith hall.

just not feeling very logging in today. :lol:

by MeMeMe » Thu Jan 05, 2006 10:23 am

Thanks

by mjs93 » Thu Jan 05, 2006 12:21 am

Script to increase PlayCounter

by MeMeMe » Wed Jan 04, 2006 8:37 pm

I'd like to increase the Playcounts of all my songs by 1-5, based on which playlist they are in.
It would be fine to have five separate scripts, and to manually select each playlist in turn, run the correct script, and then go to the next.

What would such a script look like?

Top