Connect R to your Plex Server

Hello all, been awhile since I've posted. Here I'm going to show how you can use R to connect to your Plex Server, using my Home Setup as an Example. I'll also link to a shiny application hosted on shinyapps.io at the end of this blog.

Setup


Find your Plex Media Server database file, mine was/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/. Ugh, so many spaces. You may need to escape them if pasting this directory into your shell for a cd command. Also you'll need read and write permissions. I've added both my account and the plex account to a group called shield and gave group read/write controls on the directory.

You'll need a few packages for this to work, and if you're familiar with R, you should be able to do this easily. I'm running RStudio Server on the same server that Plex is running on, but connecting remotely should not be a problem (I probably should be jailing each of these).

To setup R, all you'll need to do is install the package RSQLite via the command install.packages('RSQLite') and the load the package with library(RSQLite)

Connecting to the Database



Connecting to the database should be quite simple, and below is the code that I used to connect.
plexLibrary = paste0("/var/lib/plexmediaserver/Library/Application", "\ Support/Plex\ Media\ Server/Plug-in\ Support", "/Databases/com.plexapp.plugins.library.db") mydb <- dbdriver="" dbi::dbconnect="" dbname="plexLibrary)<code" ite="">

See what I mean about spaces in folder directories. Anyway, mydb is now loaded and you can view the tables with DBI::dbListTables(mydb). We will be looking at the "metadata_items" table. I ended up making a function to connect to the database, and then I stored that database as an .RData file for inclusion in my deployable shiny application. Those wishing to look a little bit deeper can look at the next blog, which will briefly feature the data.

Cool Graph





So what I like best about this graph is the wealth of information present. The length of the bars represent how many ratings per genre we have, and as we can see there are few War movies. The first yellow line represents 500 ratings for that genre and the final red line represents 1000 ratings of a genre. Poor movies receiving a rating of 0 are red, which fades to yellow (5) and the highest ratings are green. We can immediately tell that it is hard to make a bad animation film. Of course this is my personal plex movie library, so maybe I like good movies :). Here is a heatmap with the highest frequency occurrences of ratings per genre in light blue:

Popular posts from this blog

Setup RStudio Server on Windows with a Virtual machine

Tufte and Few and the Human Connection