Are Python module functions supported in Plex Channel files

Can I use Python module functions when programming your Plex channel python files? I ask because you have to call these modules and import the functions before you use them and I am not sure how that would work once the channel is downloaded locally to each users machine.

 

In particular, I was wanting to use urlparse.urlparse and the os.path.splittext functions to manipulate a url address.  So if they are supported, I would need to know the proper path for the os.path.splitext command to make it work universally.

urlparse and os are from the "standard library", meaning they come with Python.

just put

import urlparse
import os

at the top of your code and you should be all set

Where possible you should avoid importing things unless you really really need them.  It's hard to keep it consistent because of the differences between the PMS versions across different OSes.  Some use the built-in python (I think OSX does this), others use a bundled version of python (Windows?) .. which means you may not always have the modules available on all platforms.

What are you trying to do with these URLs shopgirl?  You can usually use built-in framework type stuff to get a lot of things done without it being too painful as far as parsing URLs.

Where possible you should avoid importing things unless you really really need them.  It's hard to keep it consistent because of the differences between the PMS versions across different OSes.  Some use the built-in python (I think OSX does this), others use a bundled version of python (Windows?) .. which means you may not always have the modules available on all platforms.

What are you trying to do with these URLs shopgirl?  You can usually use built-in framework type stuff to get a lot of things done without it being too painful as far as parsing URLs.

That consistency and whether the modules were supported was my concern.  I still wonder about the splitext command since it uses os.path so you are also supposed to specify a path. I tested this function by cutting it out of my code and making it a separate Python file and running it locally in the Python GUI I have installed on my computer .  And when I ran it locally, I had to include the following lines to the beginning of my code to get the modules to work:

from urlparse import urlparse
from os import path 
from ntpath import splitext

BTW, is there an easy way for me to run python code directly through my PMS server so I could see print commands and error messages? Maybe through the URL of the server (http://localhost:32400/) or a command prompt. If I want to add a specialized Python function to the code of a channel, it would be nice to know that I could test it out by just saving that particular function as a python file and run it separately from the rest of the channel code through the Plex Media Server. 

Basically the reason I was using these modules was to create a corresponding XML video info file for URLs that were passed through my URL Service. I just looked back at the Plex API documentation and couldn't find anything that appears to help you manipulate the URL itself.

I had found that for every HTML page that played a video on the site that I am building the channel for, there was a corresponding XML video info file with the same file name that provided all the data I needed to collect in the URL Service including the exact location of the video file. So, I knew if I could figure out a way to take the user defined HTML video page that was passed to my URL service and create the corresponding XML video info page, then I would be able to easily use XMLElementforURL to pull out all the info I needed. But the XML video info pages are in a separate double layered directory called /xml/video-page/ .

So for example a video page that has a URL of:

http://www.lstudio.com/web-therapy/season-4-morals-to-the-max.html

has a corresponding video info xml page at:

http://www.lstudio.com/xml/video-page/web-therapy/season-4-morals-to-the-max.xml

So I used the urlparse command from the Python module urlparse to cut the "page" or folder and filename (web-therapy/season-4-morals-to-the-max.html) out of the URL and then added that to a constant variable I gave a value of http://www.lstudio.com/xml/video-page. Then I used the splitext command from the os.path module to change the extension from .html to .xml.

I then tested the function by making it a separate python file and using my locally installed Python GUI and used some print commands at different points in the function to make sure it was changing the value properly through all the steps.

Sorry didn't even think about the fact that it would just be easier to show you the actual code of the function.  Here is the function I created:

# All video information is available in the xml/video-page folder
XML_URL = 'http://www.lstudio.com/xml/videos-page'

open url parse library for urlparse

from urlparse import urlparse

open os.path library for splitext

from os import path
from ntpath import splitext

################################################################################################
def CreateInfoXML(URL):

Afer reading in the URL, extract the path and add that to XML_URL then change that to an .xml file

this oart if the program merges parts of two url addresses and changes the extension from html to xml

# First we extract the path from the end of URL
URL_PART = urlparse (URL)
URL_PART = URL_PART.path

# Then join this to the XML video page prefix variable
XML_URL = XML_URL + URL_PART

# Then we split .html from the URL, delete it, and add .xml
XML_URL = splitext(XML_URL)[0] + ".xml"

I think you may be complicating things more than necessary.  I would just use a couple simple replace commands.

xml_url = url.replace('http://www.lstudio.com/','http://www.lstudio.com/xml/videos-page/').replace('.html','.xml')

Just my $0.02 FWIW

I think you may be complicating things more than necessary.  I would just use a couple simple replace commands.

xml_url = url.replace('http://www.lstudio.com/','http://www.lstudio.com/xml/videos-page/').replace('.html','.xml')

Just my $0.02 FWIW

Yep exactly what I would do as well.  The simpler and the more use of built-ins the better it will be for compatibility.

That is much simpler.  Thank you for the help.

I was confused about string methods, so I didn't even think about using that.  But I think I finally figured it out.

While on the subject of strings.  What would be the easiest way to extract just the numbers from an element in a web page? Is there an xpath command that would only pull the numbers in a string?  Or would I pull in the string and then delete everything except the number using a python string method?

I am currently pulling a list of all the series or shows that are available on a website. I found that in addition to a field or item with the series name, there is also a field or item listing that number of videos in that series, so I would like to extract that number into a variable as well.  But the number is surrounded by text in the element. Here is an example of an element that contains a number that I want to extract:

SERIES:   10 videos

I just want to get the number "10" in this element and save it to a variable.

I know I can just use the following command to extract everything in the string

NumVideos = xpath.('/div[@class="botomText"]/text()')[0]

But is there an additional xpath command or alternative to text() that will just return the number? Or will I need to just extract the entire string like the example above and then use a python command to strip the letters out of the string? If so, what would be the best way to delete all the spaces and other characters in the string?

If all you want is the number values from the string, one way to achieve it in a Plex framework-friendly fashion would be to declare a custom regex and use that to grab only the numerical digits from the string.

'''TOP OF FILE'''
RE_Numbers = Regex('.+?([0-9]+).+?')

In the above snippet, we define a regular expression (everything within the single quotes ' ' ) that will grab (whatever is defined within the inner parentheses ( ), in this case) all contiguous digits ([0-9]+) and ignore anything before or after them ('.+?'). 

Then to use it, I like to use the "search" method although most methods available with python regex objects will work too. We grab the first (and likely only) match in the list by appending ".group(1)" to the statement.

NumVideos = RE_Numbers.search(pageContent.xpath('//div[@class=""bottomText"]/text()')[0]).group(1)

You could also use string methods to strip everything but the numbers away but that would likely require that you know exactly what possible text might be included in all situations that you will be trying to handle. That tends to be a PITA and more likely to break with minor changes to the source website.

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