Check for Existing URL Service

I’ve come to the part in my plugin where I’m starting to use URL Services to get the final video link, but I’m dealing with many different hosts here. I’ve come to the conclusion that plugins are pretty unforgiving when an error occurs, so I expect the whole thing not to work if I’m missing even a single URL Service. Is there some kind of code I can include in my init.py so it will essentially skip links that don’t have an associated URL Service? Hopefully this way I can finish the main code, leave it alone, then just add the URL Services one by one.

After looking through the code of a similar plugin I’ve found that URLService.NormalizeURL (for one) can be called directly. I’m not sure what this would return if an appropriate URL Service didn’t exist though. I’ve ran into another snag in my code and I like to deal with one thing at a time.

You could always use a try/except block to catch the errors returned when no url service is found. Log the problem url so that you can hunt them down and create the necessary url service(s).


Thanks for the reply! That probably makes the most sense programmatically, assuming there's no default return value for Normalize(). The example I saw didn't use a try/except, but I didn't exactly understand how it determined the function failed in the first place.

The NormalizeURL() function will return a URL as long as 1. there is an URL service for the URL being passed, and 2. the URL service implements the NormalizeURL() function. AKAIK if you call NormalizeURL() explicitly, and it fails, it will still throw an ugly exception and most likely cause problems for the plugin.

I asked this question a while back and it looked like I had the answer, but that was way before I actually tried it. Whether or not a URL service exists for a given URL I do not get an exception. I actually get the exact same URL passed back to me when I invoke URLService.NormalizeURL(‘http://example.com/’). Depending on the context I can see how throwing an exception for this call could be a bad thing so I don’t necessarily recommend the function throw such an exception.



I did want to share though, that there is another way to determine if a service exists for a given URL. Digging through the framework code I found ServiceIdentifierForURL (not mentioned in framework documentation). So code such as this should leave you in no doubt that a service exists for your URL:


<br />
if URLService.ServiceIdentifierForURL(url2) is not None:<br />
     vc = VideoClipObject(url = url2)<br />


I asked this question a while back and it looked like I had the answer, but that was way before I actually tried it. Whether or not a URL service exists for a given URL I do not get an exception. I actually get the exact same URL passed back to me when I invoke URLService.NormalizeURL('http://example.com/'). Depending on the context I can see how throwing an exception for this call could be a bad thing so I don't necessarily recommend the function throw such an exception.

I did want to share though, that there is another way to determine if a service exists for a given URL. Digging through the framework code I found ServiceIdentifierForURL (not mentioned in framework documentation). So code such as this should leave you in no doubt that a service exists for your URL:

if URLService.ServiceIdentifierForURL(url2) is not None:
     vc = VideoClipObject(url = url2)

I had questions about checking the validity of videos with the Plex URL service in another post and Sander1 directed me to this post.

I had problems using the code above. Before adding the code, my channel code produced video list and I was able to play the videos.  After adding the code, some of those videos that played without the code were moved to the else category with the code.

I tested it on a section of my code that returns RSS feed videos. I have three different feeds listed. Prior to adding the url service check code, it returned a video list for all three of the feeds. After adding the code, two of those feeds ended up going to the else statement of the code above and my channel only produced a video list for one of the feeds.

One of the feeds that the code put into the else section and returned no service for produced a video list, but when you chose to play the videos, it gave you an server error, so I am assuming that it was returning accurate results for that feed. 

But the other feed it put in the else section and returned as no service for was not only produced a video list, but also played the videos. This second feed that it returned as not working with the Plex URL service even though it worked without the code was a very slow loading the video list and also took forever for the videos to load and start playing. 

Could the code above have a timeout limit and that is why it returned this feed as not working with the URL service even though it did?
 

I could be wrong, but it looks like ServiceIdentifierForURL() doesn't take into account fallback services which would otherwise be attempted if the initial lookup failed.  This would explain the behavior @shopgirl is seeing where fewer videos work when this test is performed.

If you wanted to test this theory for sure, note some cases where lookups fail (there is log output that includes the word "fallback" but I don't have an example handy), then add the conditional and see whether those same videos come back with identifiers or not.

There doesn't seem to be any fallback messages in the channel or system log.  It seems to find the service for it fine.  It finds the service for the first one, then says it is loading the service code and then loads the rest.

It does seem that the URL service for the RSS feed that doesn't work for the code above uses RTMP.  Could that affect the codes ability to work?

I just went back and tried using this code again. Before, I went ahead and set up exception code for if it returned false.  This time, just to test it, I just had it return a true or false value that printed that value to the log for each video it checked.  The code is showing a return of true for the RSS feed I thought was incorrect before. Either the way in which I was adding the error check to the code was incorrect or I was using it in the wrong place before. But it seems to be working now.

Sorry for the Duh moment there.