My First Plugin

Questions from a new Plex developer
Hi All,

I am an experienced developer, but new to Plex and Python. I want to create a plugin to empty the OS trash. To make it as user-friendly as possible, I want to list each accessible server to the user so they can choose one or more.

How do I get a list of the accessible servers?

Also, is there a reference to the information accessible from Plex (Python functions or URLs)?

Thanks for your help!

Caleb

You’ve picked a very interesting project for your first plugin. On the surface, it may seem like a fairly straight-forward concept but could turn out to be quite problematic. I see a couple issues that you’ll have to deal with along the way:

[list=1][]Plugins are not aware of more than one PMS. A plugin is able to retrieve some information about the instance of PMS that it inhabits but it is not aware of any other instances of PMS on the same network or that may be available via myPlex.[]By design, plugins do not have access to OS level functions.[/list]

There may be ways to deal with these issues but you’re well off the beaten path in terms of plugin development. Realistically, Plex plugins are primarily for Video and Audio streaming. That being said, I’ll offer what advice I can.

[list=1][]You could a.) install your plugin on each instance of PMS and it will be responsible only for the Server on which it is installed, or b.) try to parse the PMS log to find the IP address of each PMS which identifies itself on the network. There will be issues with either approach. If you choose a, when multiple PMSs are on “visible” to one client, each instance of your plugin will show up in the channel list. They will identify themselves with the PMS that spawned them but, it’s still a less than desirable effect for the user. If you choose b, parsing the PMS log is a PITA and then you’ll need to find a way to pass commands to the server using the IP address as a starting point. Since the commands you’ll need to implement are not available via the Plex HTTP API, you’ll be fighting an uphill battle.[]It is possible to import python’s OS library, as long as you use the “elevated” code policy in the plugin’s Info.plist


<key>PlexPluginCodePolicy</key><string>Elevated</string>

. Once you’ve done that you can import pretty much any python library you need. The proper implementation of the necessary commands is up to you.[/list]

Good luck :slight_smile:

Thank you Mikedm139!



My original design was to have the plugin installed on each server but only appear once on the client. The client would provide a list of servers allowing the user to choose which to empty. Then, the client would trigger the plugins (installed on each server) via URLs to empty the trash.



Since this is either not doable or very difficult, I think I will take the first approach you suggested. (having the plugin installed on each server and having each one appear on the client.



For MacOSX, I planned to use the subprocess library to launch applescript telling the Finder to empty the trash. The code for this is:


<br />
p = subprocess.Popen(<br />
        ['osascript', '-e', 'tell application \"Finder\" to empty the trash'], <br />
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)<br />




Would I need to use "elevated" in this case? I suspect that I will need to make similar calls to do so on Windows.

Also, do you know what the server does with deleted movies/tv shows on Linux? Is there a trash area it moves them to or does it just remove them?

Caleb

I would guess that you likely will need the elevated Code policy to make use of the subprocess python module. You can always try it without. If ‘subprocess’ is black-listed in the restricted python used by the plugin framework, then you will need to use elevated code policy to be able to import. Otherwise the plugin won’t load.



I’m not really a linux guy but, to the best of my knowledge, movies deleted by PMS-linux are deleted rather than sent to “trash-can”.

I have completed the first version of my plugin. Please take a look at it and offer suggestions for improvement. The source code is fairly short and easy to read.



Caleb



Looks good to me. A couple notes:
-First, there are some lines in the Info.plist that are not necessary. They exist in a lot of the plugins just because stuff tends to get copied and pasted a lot. They don't break anything but you can remove them for the same of tidiness. These are the ones that can be deleted without causing problems:

[s]PlexPluginConsoleLogging[/s]
[s]1[/s]
[s]PlexPluginMode[/s]
[s]AlwaysOn[/s]
[s]CFBundleVersion[/s]
[s]0.1[/s]
[s]CFBundleShortVersionString[/s]
[s]0.1[/s]
[s]
[/s]
[s]-[/s] Second, I see you wrote you're own function to find the friendly name for the PMS running the plugin. That's great. If you're interested, there is a framework function that will provide that for you. It's not part of the "public" (or documented) framework API but since your plugin is already using the "Elevated" code policy, that's not really an issue. If you want to try it, here it is:

Core.get_server_attribute('friendlyName')

Thanks for your feedback, Mikedm139!



I included CFBundleVersion and CFBundleShortVersionString so that the version could be seen in Finder. As long as these will not cause a problem, I would like to leave them in.



Thanks for the friendlyName advice. I am definitely using it.



I thought of another plugin that I would like to make: Restart Plex. When the client resides on the same machine as the server, the server can easily restart Plex. The first thing I want to do is check if the client is on the same physical machine as the server. Is there a way I check this? One idea that I had is to check the URL made to invoke the plugin from the client (if the host is “localhost”).



Caleb



There is not a supported way to achieve this. You might be able to check the headers of a request made by the client for details about where it resides. The PMS log will contain some detail about the client making requests if you want to look there for starters.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.