Track exists in playlist - best method ?

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

Moderator: Gurus

raybeau528
Posts: 401
Joined: Thu Sep 27, 2007 4:19 pm
Location: Connecticut

Track exists in playlist - best method ?

Post by raybeau528 » Mon Feb 23, 2009 10:39 am

What would be the best method to determine, via a script, if a track exists in a playlist?

I certainly could loop through all tracks and check for matching song.id. Not heavy processing if I'm checking for 1 track in 1000. Checking for 1000 tracks in a 10,000 track playlist could result in a million cycles!

Does the AddTrackbyID(ID) method return any indication whether the track ID already exists in the playlist?

The Properties editor / Classification lists the playlists for which the selected song is a member. Is there a scripting property/method to access that?

Thanks!

Ray

ZvezdanD
Posts: 3090
Joined: Thu Jun 08, 2006 7:40 pm

Re: Track exists in playlist - best method ?

Post by ZvezdanD » Mon Feb 23, 2009 11:00 am

raybeau528 wrote:What would be the best method to determine, via a script, if a track exists in a playlist?
I haven't tried, but you could try something like this:

Code: Select all

If SDB.Database.OpenSQL("SELECT * FROM PlaylistSongs, Playlists WHERE PlaylistSongs.IDSong = " & iSongDataID & " AND PlaylistSongs.IDPlaylist = Playlists.IDPlaylist AND Playlists.PlaylistName = '" & sPlaylistName & "'").EOF Then
Magic Nodes 4.3.3 / 5.2 RegExp Find & Replace 4.4.9 / 5.2  Invert Selection/Select None 1.5.1  Export/Create Playlists for Child Nodes 4.1 / 5.4  Expand Child Nodes/Expand All 1.1.2  Event Logger 2.7  Filtered Statistics Report 1.6  Track Redirection & Synchronization 3.4.2  Restore/Synchronize Database 3.1.7 / 4.0  Find Currently Playing Track 1.3.2  Queue List 1.2.1  Add to Library on Play 1.0.1  Tree Report for Child Nodes 1.1.1  Update Location of Files in Database 1.4.3 / 2.2.2  Inherit Child Playlists 1.0.2  Add Currently Playing/Selected Track(s) to Playlist 1.1.2

raybeau528
Posts: 401
Joined: Thu Sep 27, 2007 4:19 pm
Location: Connecticut

Re: Track exists in playlist - best method ?

Post by raybeau528 » Mon Feb 23, 2009 11:05 am

Thanks for the super quick response!!! I'll give it a try.

Ray

ZvezdanD
Posts: 3090
Joined: Thu Jun 08, 2006 7:40 pm

Re: Track exists in playlist - best method ?

Post by ZvezdanD » Mon Feb 23, 2009 11:19 am

It could be even shorter if you know playlist's ID:

Code: Select all

If SDB.Database.OpenSQL("SELECT * FROM PlaylistSongs WHERE PlaylistSongs.IDSong = " & iSongDataID & " AND PlaylistSongs.IDPlaylist = " & iPlaylistID).EOF Then
Magic Nodes 4.3.3 / 5.2 RegExp Find & Replace 4.4.9 / 5.2  Invert Selection/Select None 1.5.1  Export/Create Playlists for Child Nodes 4.1 / 5.4  Expand Child Nodes/Expand All 1.1.2  Event Logger 2.7  Filtered Statistics Report 1.6  Track Redirection & Synchronization 3.4.2  Restore/Synchronize Database 3.1.7 / 4.0  Find Currently Playing Track 1.3.2  Queue List 1.2.1  Add to Library on Play 1.0.1  Tree Report for Child Nodes 1.1.1  Update Location of Files in Database 1.4.3 / 2.2.2  Inherit Child Playlists 1.0.2  Add Currently Playing/Selected Track(s) to Playlist 1.1.2

raybeau528
Posts: 401
Joined: Thu Sep 27, 2007 4:19 pm
Location: Connecticut

Re: Track exists in playlist - best method ?

Post by raybeau528 » Mon Feb 23, 2009 11:23 am

ZvezdanD,

I tried this:

Code: Select all

Sub TrackinPlaylist(arg)
	dim isongdataid : iSongDataID = 6431
	dim sPlaylistName : sPlaylistName = "Test"

	If SDB.Database.OpenSQL("SELECT * FROM PlaylistSongs, Playlists WHERE PlaylistSongs.IDSong = " & iSongDataID & " AND PlaylistSongs.IDPlaylist = Playlists.IDPlaylist AND Playlists.PlaylistName = '" & sPlaylistName & "'").EOF Then
		msgbox("In Playlist")
	else
		msgbox("Not in Playlist")
	End If

End Sub
I created a playlist named "Test". I copied the song.id's to custom1 so I could see them and verified that one track song.id was 6431. I hard coded them for a quick test. Result was NOT in playlist. I don't know sql but those look like the only variables.

Ray

raybeau528
Posts: 401
Joined: Thu Sep 27, 2007 4:19 pm
Location: Connecticut

Re: Track exists in playlist - best method ?

Post by raybeau528 » Mon Feb 23, 2009 11:24 am

I can get the playlist ID . I'll try that next but it will be later today. Thanks for your expert help!

Ray

ZvezdanD
Posts: 3090
Joined: Thu Jun 08, 2006 7:40 pm

Re: Track exists in playlist - best method ?

Post by ZvezdanD » Mon Feb 23, 2009 11:45 am

raybeau528 wrote:Result was NOT in playlist.
Well, if EOF is True that mean the recordset is empty, so you should switch MsgBox-es after Then and Else or put Not after If.
Magic Nodes 4.3.3 / 5.2 RegExp Find & Replace 4.4.9 / 5.2  Invert Selection/Select None 1.5.1  Export/Create Playlists for Child Nodes 4.1 / 5.4  Expand Child Nodes/Expand All 1.1.2  Event Logger 2.7  Filtered Statistics Report 1.6  Track Redirection & Synchronization 3.4.2  Restore/Synchronize Database 3.1.7 / 4.0  Find Currently Playing Track 1.3.2  Queue List 1.2.1  Add to Library on Play 1.0.1  Tree Report for Child Nodes 1.1.1  Update Location of Files in Database 1.4.3 / 2.2.2  Inherit Child Playlists 1.0.2  Add Currently Playing/Selected Track(s) to Playlist 1.1.2

ZvezdanD
Posts: 3090
Joined: Thu Jun 08, 2006 7:40 pm

Re: Track exists in playlist - best method ?

Post by ZvezdanD » Mon Feb 23, 2009 12:02 pm

By the way, if you want to check if all 1000 tracks belong to same playlist, I suppose that it is faster to check this with a single SQL query, instead of one SQL query for each track. Here is an example:

Code: Select all

' sSongDataIDArray = "1234, 1235, ..., 9999"

If Not SDB.Database.OpenSQL("SELECT * FROM PlaylistSongs WHERE PlaylistSongs.IDSong IN (" & sSongDataIDArray & ") AND PlaylistSongs.IDPlaylist = " & iPlaylistID).EOF Then
Magic Nodes 4.3.3 / 5.2 RegExp Find & Replace 4.4.9 / 5.2  Invert Selection/Select None 1.5.1  Export/Create Playlists for Child Nodes 4.1 / 5.4  Expand Child Nodes/Expand All 1.1.2  Event Logger 2.7  Filtered Statistics Report 1.6  Track Redirection & Synchronization 3.4.2  Restore/Synchronize Database 3.1.7 / 4.0  Find Currently Playing Track 1.3.2  Queue List 1.2.1  Add to Library on Play 1.0.1  Tree Report for Child Nodes 1.1.1  Update Location of Files in Database 1.4.3 / 2.2.2  Inherit Child Playlists 1.0.2  Add Currently Playing/Selected Track(s) to Playlist 1.1.2

raybeau528
Posts: 401
Joined: Thu Sep 27, 2007 4:19 pm
Location: Connecticut

Re: Track exists in playlist - best method ?

Post by raybeau528 » Mon Feb 23, 2009 8:22 pm

Thanks ZvezdanD ! All 3 methods worked perfectly!

Ray

Post Reply