2015. október 2.

Building a Plex Plugin from scratch

First of all, some important url-s for learning:
 Seconds, the skeleton  (directories and files) of a plex plugin (all are case sensitive):
  • MyPlugin.bundle
    • Contents
      • Code
        • __init__.py
      • Info.plist
An absolute minimal content of the Info.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
 <key>CFBundleIdentifier</key>
 <string>com.plexapp.plugins.myplugin</string>
 <key>PlexFrameworkVersion</key>
 <string>2</string>
 <key>PlexClientPlatforms</key>
 <string>*</string>
</dict>
</plist>
An absolute minimal content of the __init__.py (for a responding plugin):
@handler('/video/myplugin', 'My Plugin')
def Main(): # this is the landing page of the plugin because of the prefix handler.
    return ObjectContainer()
That's it. Every plugin grows out from this root :)

Okay, one thought to developing a plex plugin: without a proper IDE, you're dooooomed! :D Okay, maybe not, but if you use other people's code than sorting out python syntax errors (like: there's a tab instead of space, and here's a comma missing, thet indentation is only 2 space not 4, etc) can be quite annoying since the Plex server does not help much with debugging (it either responds or not, but that's it).
Anyway, I managed to make my first plex plugin work... or at least some parts of it, and at least on my PC :) Here it is.

2015. október 1.

Plex Plugin Installer

So, since I found plex plugin manual installation painfully boring, I wrote a nautilus script to do the job for me.
Instead of copy-pasting, you can also download this script form github.

I wasted a couple hours finding out that  if I use "double quotes" instead of 'single quotes' when assigning the plugin folder path to the PLUGIN_FOLDER variable, then 1.) the path will break on spaces and won't work; 2.) if I surround the call with '"'-s (a double quote between two single quotes) or just another "-s the program will fail not finding the files/directories. This was the tricky part because if instead of running I echoed the lines, they looked perfectly okay, and if I copy-pasted them to the command line they would even run properly... only not within a script.

#!/bin/bash
# nautilus script
# install PLEX plugin on Ubuntu 12.04.

# run this script by right clicking on the directory you want to install
install_plugin () {
 # standard feature: install by copying folder
 # future enhancement: install from ZIP file

 PARAMETERS=( "$@" )               ## array containing all params passed to the script

 PLUGIN_FOLDER='/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-ins/'

 # read folder paths into TO_BE_INSTALLED
 IFS=$'\n' read -d '' -r -a TO_BE_INSTALLED < <(printf '%s\n' $PARAMETERS); unset $IFS

 need_to_restart_server=false

 for package in "${TO_BE_INSTALLED[@]}"; do
  package_name="${package##*/}" # Strip longest match of substring (*/) from front of string

 # sanity check: is the path correct? it should be a directory and end with ".bundle".
  if  [[ -d "$package" ]]; then # if selected path IS a directory
   if [[ "${package_name##*.}" == "bundle" ]]; then
    installed_package=$PLUGIN_FOLDER$package_name
   # check if plugin already exists
    if [[ -d "$installed_package" ]]; then 
     # if yes, ask to delete it 
     echo -n "A package with the same name is already installed. Would you like to overwrite it? (yes/no): "; read overwrite 
     if [[ $overwrite == [Yy][Ee][Ss] || $overwrite == [Yy] ]]; then 
      sudo rm -R "$installed_package"
     else
      echo "You chose not to overwrite $package_name so it will not be processed."
      continue
     fi
    fi
   # install plugin
    #copy the downloaded plugin to the plex plug-in directory
    sudo cp -R "$package" "$PLUGIN_FOLDER"

    #change the permissions to be like the existing plug-ins 
    #(this is not needed in 14.04 anymore)
    sudo chmod ugoa+rx "$installed_package"

    #change the owner of the files to be like the existing plug-ins
    sudo chown -R plex:plex "$installed_package"

    need_to_restart_server=true

   else # if not "bundle"
    echo "Package name should end with '.bundle'. $package_name will not be processed."
    continue # process next package instead
   fi

  else # if not directory
   echo "Package should be a directory. $package_name will not be processed."
   continue # process next package instead
  fi

 done
# when finished with all packages, restart plex server.
if [[ $need_to_restart_server = true ]]; then 
 sudo service plexmediaserver stop
 sleep 5
 sudo service plexmediaserver start
 
 echo "Installation complete. Your new plug-in should show up now in PLEX."
fi
}

export -f install_plugin

gnome-terminal --execute bash -c 'install_plugin "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"; bash'