Subsonic smart playlists via db / useful db commands
Posted: Tue Jan 03, 2017 3:49 pm
Hi,
I've been evaluating a few different streaming server options over the past few months. I'm coming over from iTunes / syncing which is becoming too cumbersome for me. I wanted a service that would give me one of iTunes more useful features - smart playlists. This is not available as standard in Subsonic, but with a bit of SQL manipulation I managed to get a solution.
I took inspiration from this Reddit thread https://www.reddit.com/r/subsonic/comments/49vhsh/example_playlist_management_using_the_db/
The commands relate to Madsonic however, so I had to figure out the correct syntax for the db version that Subsonic uses.
Personally, I have 8 playlists: "Starred", "Last 1 month", "Last 3 months", "Last 6 months", "Last 12 moths", "Last 24 months", "Music videos" and "Synced".
I manually created the playlists in the Subsonic GUI and then typed the following into mysubsonicserver.com/db.view
This gives the index numbers of my playlists, which I then need to use for subsequent commands to populate these playlists.
This first one populates a playlist called "Starred" (playlist_id 0) with all of my starred media files:
Then variations of the following are issued to add items into a playlist by the "modification" date of the source file. "The 30 + 1" bit refers to the amount of days prior to todays date to include in the playlist. I did write an additional shell script to make sure all my file mod dates were equal to the date that they were added to iTunes. I'll include that later on:
As you can see, I include music videos in my playlists. You can delete that if needs be.
The next one copies all video files into my "music videos" playlist (index 6)
I did have an issue whereby it was difficult for me to get all of my previously starred (or "loved" in iTunes) items to show up as "starred" in Subsonic. It's easy in Madsonic - import a .m3u playlist, "select all" then "star all" (or similar). Not so easy in Subsonic, so it needed additional db work:
This command clears all currently "starred" items, then takes the items from a playlist (id 10 in this instance) and "stars" them.
Hope this helps someone out. Took me a long time to figure out and I could have done with a tutorial like this
Shell script below to scan iTunes XML file and change created / modified dates according to "added" date in the iTunes XML. I use on OSX with a symlink mounted iTunes library residing on a NAS.
Use at your own risk!!!
Paste the script into a text file, save it as script.sh. Then go to terminal and
I've been evaluating a few different streaming server options over the past few months. I'm coming over from iTunes / syncing which is becoming too cumbersome for me. I wanted a service that would give me one of iTunes more useful features - smart playlists. This is not available as standard in Subsonic, but with a bit of SQL manipulation I managed to get a solution.
I took inspiration from this Reddit thread https://www.reddit.com/r/subsonic/comments/49vhsh/example_playlist_management_using_the_db/
The commands relate to Madsonic however, so I had to figure out the correct syntax for the db version that Subsonic uses.
Personally, I have 8 playlists: "Starred", "Last 1 month", "Last 3 months", "Last 6 months", "Last 12 moths", "Last 24 months", "Music videos" and "Synced".
I manually created the playlists in the Subsonic GUI and then typed the following into mysubsonicserver.com/db.view
- Code: Select all
SELECT * FROM PLAYLIST
This gives the index numbers of my playlists, which I then need to use for subsequent commands to populate these playlists.
This first one populates a playlist called "Starred" (playlist_id 0) with all of my starred media files:
- Code: Select all
DELETE from PLAYLIST_FILE where playlist_id = 0
insert into PLAYLIST_FILE (playlist_id, media_file_id) select 0, media_file_id from starred_media_file;
Then variations of the following are issued to add items into a playlist by the "modification" date of the source file. "The 30 + 1" bit refers to the amount of days prior to todays date to include in the playlist. I did write an additional shell script to make sure all my file mod dates were equal to the date that they were added to iTunes. I'll include that later on:
- Code: Select all
DELETE FROM playlist_file WHERE PLAYLIST_ID = 1;
INSERT INTO playlist_file (MEDIA_FILE_ID, PLAYLIST_ID)
SELECT ID, '1'
FROM media_file WHERE DATEDIFF('day', "CHANGED", CURRENT_DATE) <= 30 + 1 AND TYPE='MUSIC' OR DATEDIFF('day', "CHANGED", CURRENT_DATE) <= 30 + 1 AND TYPE='VIDEO'
SELECT COUNT(*) FROM playlist_file WHERE PLAYLIST_ID=1;
As you can see, I include music videos in my playlists. You can delete that if needs be.
The next one copies all video files into my "music videos" playlist (index 6)
- Code: Select all
DELETE FROM playlist_file WHERE PLAYLIST_ID = 6;
INSERT INTO playlist_file (MEDIA_FILE_ID, PLAYLIST_ID)
SELECT ID, '6'
FROM media_file WHERE TYPE='VIDEO'
SELECT COUNT(*) FROM playlist_file WHERE PLAYLIST_ID=6;
I did have an issue whereby it was difficult for me to get all of my previously starred (or "loved" in iTunes) items to show up as "starred" in Subsonic. It's easy in Madsonic - import a .m3u playlist, "select all" then "star all" (or similar). Not so easy in Subsonic, so it needed additional db work:
- Code: Select all
DELETE FROM starred_media_file;
INSERT INTO starred_media_file (MEDIA_FILE_ID, USERNAME, CREATED) SELECT MEDIA_FILE_ID,'your_user_name',NOW FROM playlist_file WHERE PLAYLIST_ID = 10;
This command clears all currently "starred" items, then takes the items from a playlist (id 10 in this instance) and "stars" them.
Hope this helps someone out. Took me a long time to figure out and I could have done with a tutorial like this
Shell script below to scan iTunes XML file and change created / modified dates according to "added" date in the iTunes XML. I use on OSX with a symlink mounted iTunes library residing on a NAS.
Use at your own risk!!!
Paste the script into a text file, save it as script.sh. Then go to terminal and
- Code: Select all
sh script.sh path_to_your_itunes_library_xml
- Code: Select all
#!/bin/bash
counter=0
while read line
do
name=$line
fulldate=`echo $name | sed -n 's:.*\<key\>Date\ Added\<\/key\>\<date\>\(.*\)\<\/date\>.*:\1:p' | cut -b 1-10` # finds the date text between the "<key>Date Added</key><date>" and "</date>" tags
fullpath=`echo $name | sed -n 's:.*\<key\>Location\<\/key\>\<string\>file\:\/\/\(.*\)\<\/string\>.*:\1:p'` # filds the filepath relevant to the date above between the "<key>Location</key><string>file://" and "</string>" text
if [ ! -z "$fulldate" ] # If the variable is NOT empty
then
year=`echo $fulldate | cut -b 1-4` # process the date text into workable day, month, year
month=`echo $fulldate | cut -b 6-7`
day=`echo $fulldate | cut -b 9-10`
fi
if [ ! -z "$fullpath" ] # If a valid filepath has been found
then
parsedpath=`printf "%b\n" "${fullpath//%/\\x}" | sed 's/#38;//g'` # converts the %20 and others to a form that bash recognises, the "sed" deals with "&" characters which the conversion misses. The "g" option is global - replaces all instaces
setfile -d "$month/$day/$year 01:00:00" -m "$month/$day/$year 01:00:00" "$parsedpath"
# "setfile" above is a Mac OSX command which enables full modification of the "created" and "modified" dates. You can use the "touch" command as an alternative - commented out below
#touch -t "$year""$month""$day"0100.00 '$fullpath'
#touch -t "$year""$month""$day"0100.00 "$parsedpath"
counter=$((counter+1))
# echo $counter, "PATH:" $parsedpath # uncomment these to get verbose output
# echo "DAY:"$day "MONTH:"$month "YEAR:"$year
fi
done < $1
echo Processed "$counter" files