Cayars - Setup walk through and some tips and tricks

This is going to be a bit technical and needs refinement which I'll get to.  I literally just whipped this up and thought I'd share before I finish automating it.

The premise is to give "virtual library" support to Plex in a way we can do much of it manually.

Here is how it works.  In Plex we can edit the metadata and add tags.  One of the tags is "SHARING" and is also used (you guessed it) when you share with other people.  So for example if you only wanted to share things with a tag of "CARLO" then you add this as a tag and then select this tag "CARLO" when setting up your share.  Pretty straightforward for normal plex use.

Now what I've been able to do is make use of this "SHARING" tag.  So if I wanted to create a library called Recommended I would edit the movie meta-data via the web browser and and tag it VL-Recommended.  The "VL-" part is the preface for "Virtual Lib".  Technically, I'm not using it yet but will in the next release after a bit more testing.  added

So I could setup a Kids library and flag media with the sharing tag of "VL-Kids", etc

Now what I need to do is create a real folder on a drive called "Recommended" and lets say it's "C:\VirtualLib\Recommended".  I could setup "C:\VirtualLib\Kids", etc  I also need to create a real library in Plex called Recommended and point it appropriately just like any library.

Now the trick is that we will make use of sqlite3.exe on windows to run a small file with commands.  Mine is called "Recommended.txt" VirtualLib.txt and contains:

.open 'e:\com.plexapp.plugins.library.db'
.output VirtualLib.bat
SELECT 'mklink /J "C:\VirtualLib\' || replace(tag,'VL-','') ||'\' || replace(replace(file, rtrim(file, replace(file, '\', '' ) ), ''), '.' || replace(file, rtrim(file, replace(file, '.', '' ) ), ''),'') || '" "' || replace(file,replace(file, rtrim(file, replace(file, '\', '' ) ), ''),'')|| '"' FROM media_parts
join media_items on media_parts.media_item_id=media_items.id
join taggings on media_items.metadata_item_id=taggings.metadata_item_id
join tags on taggings.tag_id=tags.id
where tag like 'VL-%' order by tag, file;

The first line will issue an open statement and you put in the path of your database.  In this example I'm using a copy of my database located in the root of e:\.  I'd suggest shutting down Plex and making a copy of your db for test purposes until you have everything working correctly.  This is purely "read only" and doesn't modify the database so there should not be a problem.  After being satisfied change this to your actual database but keep a backup on hand just as a precaution (always good idea).

The second line sends the output of the run to recommended.bat

The rest is the SQL we want to run.

From the command prompt you would switch to the directory that has sqlite3.exe and recommended.bat in it and issue the following:

sqlite3 < VirtualLib.txt

On my system with a few moves flagged I get the following generated:

mklink /J "C:\VirtualLib\Recommended\127 Hours (2010)" "F:\Movies\#\127 Hours (2010)\"
mklink /J "C:\VirtualLib\Recommended\13 Going On 30 (2004)" "F:\Movies\#\13 Going on 30 (2004)\"
mklink /J "C:\VirtualLib\Recommended\17 Again (2009)" "F:\Movies\#\17 Again (2009)\"
mklink /J "C:\VirtualLib\Recommended\The 40-Year-Old Virgin (2005)" "F:\Movies\T\The 40-Year-Old Virgin (2005)\"
mklink /J "C:\VirtualLib\Recommended\Air America (1990)" "F:\Movies\A\Air America (1990)\"
mklink /J "C:\VirtualLib\Recommended\Air Force One (1997)" "F:\Movies\A\Air Force One (1997)\"

So if you are familiar with Windows junction points this should make sense what I did.  If not then the basic principal is that each of these lines creates a "link" to the real directory.  Plex will know the difference nor care.

BTW, the above Batch file needs to get executed/run to do it's magic.

That's basically all these is to it.  The part in the above file "C:\VirtualLib" is hard coded in the sql which you can easily edit to match your system.  You'll also see "VL-Recommended" VL-% in the sql which would NOT need to get changed to the tag you are using.

So the script looks for any tags you create that start with "VL-" and strips off the VL- part leaving the DIRECTORY NAME that gets inserted behind the hard coded "C:\VirtualLib".  So whenever you want to use a new VL-TAG value you want to create a new folder under C:\VirtualLib to match the tag value and also create a new library in Plex pointed at that location.

So this is a basic release that is mostly manual but it allows you to start using it now.  I plan on making it smart and parsing the tags. DONE

So for example it will look for any tags that start with "VL-" and know these are Virtual Library tags.  It will then parse the tag throwing out the "VL-" keeping the rest as the library name.  Then the only change that would be needed to the sql would be to set the virtual drive path.  IE "C:\VirtualLib\"  With the parsing just mentioned this could become "C:\VirtualLib\Recommended" or "C:\VirtualLib\Kids", etc  The last part is from the tag.

Right now this doesn't automatically remove the junctions if you remove the tags but you can remove the directory "Recommended" and re-create it which removes all junctions just prior to running the created batch file.  Of course all this could easily be setup to run as a task.

On my live system it takes only a couple of seconds to create 500 junctions.  I have this set to run everything from the task scheduler once per hour which is good enough for me.

I have one job that runs 23 times a day and only does additions.  One job a day that removes the directories (Recommended, Christmas, etc) then recreates them again which effectively removes any media no longer tagged, followed by running the batch file to recreate all junctions.

Let me know what you guys think,

Carlo

PS I didn't want to automate it yet because I need some eyes looking at it and the results you get.  Just because it parses the libraries for me doesn't mean it will for you so VIEW the created batch file to make sure everything looks correct.  Once we get some good responses and any kinks worked out (if any) we'll take it to the next step.

PPS While the above is geared for windows platforms it can very easily be changed to work on any platform that supports symlinks or junctions.