Page 1 of 1

Pleas help write plugin

PostPosted: Thu Dec 08, 2011 6:41 pm
by Neoromancer
Hi volk!
Firs, I'am not developer but try to understand how work plugin.

I'm trying to write a plugin for this page http://tv.delfi.lt/

Rss http://tv.delfi.lt/feed/rss

I tried to write but do not work. My code:
  Code:
import org.serviio.library.metadata.*
import org.serviio.library.online.*

/** delfitv.info content URL extractor plugin.
 *
 * http://tv.delfi.lt/feed/rss
 *
 * @author Neoromancer
 *
 */
class delfitvUrlExtractor extends FeedItemUrlExtractor {

   final VALID_FEED_URL = '^http://ytv.delfi.lt/v/.*'
   
   String getExtractorName() {
      return getClass().getName();
   }

   boolean extractorMatches(URL feedUrl) {
      return feedUrl ==~ VALID_FEED_URL;
   }

   /* Quality is ignored on delfitv */
   ContentURLContainer extractUrl(Map links, PreferredQuality requestedQuality) {
      def linkUrl = links.alternate != null ? links.alternate : links.default
      // open the HTML page first      
      def webPage = linkUrl.getText()
      // find SWFObject : content file location
      // Example : so.addVariable('file','http://ytv.delfi.lt/v/55rqhjZ8.mp4%3FPHPSESSID%3D863c2f07ab6b1230367086add75ef866%26ft%3Da.mp4');
      def matcher = webPage =~ 'so.addVariable.*file.*'
      def linkParts = matcher[0].toString().split("'")
      def videoUrl = linkParts[3]
      return new ContentURLContainer(fileType: MediaFileType.VIDEO, contentUrl: videoUrl)
   }

   /* Groovy test method */
   static void main(args) {
      delfitvUrlExtractor extractor = new delfitvUrlExtractor()

      assert extractor.extractorMatches( new URL("http://tv.delfi.lt/feed/rss"))
      Map links = ['default': new URL("http://ytv.delfi.lt")]
      ContentURLContainer result = extractor.extractUrl(links, PreferredQuality.MEDIUM)
      println "Result: $result"
   }
}


I do not understand where my mistake. Help write a working plugin. Then try to understand how it works.

Thanks.

Re: Pleas help write plugin

PostPosted: Thu Dec 08, 2011 6:51 pm
by zip
you don't need a plugin for this RSS feed, it already includes the stream URL in it's media:content element

Re: Pleas help write plugin

PostPosted: Thu Dec 08, 2011 8:24 pm
by Neoromancer
Hmm... I added http://tv.delfi.lt/feed/rss but no show contents.

Re: Pleas help write plugin

PostPosted: Thu Dec 08, 2011 9:51 pm
by zip

Re: Pleas help write plugin

PostPosted: Fri Dec 09, 2011 5:19 am
by jhb50
As zip says: Did you read "READ BEFORE POSTING" viewtopic.php?f=5&t=1289

The log says:
Cannot parse resource from http://ytv.delfi.lt/v/EsMqIHmA.mp4. Message: Error during feed parsing : Invalid XML: Error on line 1: Content is not allowed in prolog.

The bad line is <media:content>http://ytv.delfi.lt/v/EsMqIHmA.mp4</media:content>
it needs to read:
<media:content url="http://ytv.delfi.lt/v/EsMqIHmA.mp4" />
to be valid so yes you need a groovy.

Re: Pleas help write plugin

PostPosted: Fri Dec 09, 2011 9:21 am
by zip
weird, I'll check it, but it's complaining about line 1, it looks more like the XML is dodgy with some characters at the beginning

Re: Pleas help write plugin

PostPosted: Fri Dec 09, 2011 5:34 pm
by jhb50
So you are saying that this form of media:content will be processed correctly by Serviio as well as the <media:content url=".." /> form that is documented?
<media:content>http://ytv.delfi.lt/v/EsMqIHmA.mp4</media:content>

Re: Pleas help write plugin

PostPosted: Fri Dec 09, 2011 6:31 pm
by zip
no, just checked, it needs the url as an attribute

Re: Pleas help write plugin

PostPosted: Fri Dec 09, 2011 10:06 pm
by jhb50
So that confirms he needs a groovy? Pretty simple one though.. just read the existing media content urls for each link and return them to serviio.

Just parse the Rss once in "extractorMatches" to get the items as below and then in "extractUrl" find the item for each link and extract and return the url .

That should be enough help.

  Code:
   boolean extractorMatches(URL feedUrl) {
      //get the rss contents
      def rssPage = new URL(feedUrl.toString()).getText()
      //now parse the rss contents into items
      def rssNodes = new XmlParser().parseText(rssPagenew)
      // and create a list of items for use by extractUrl
      itemNode = rssNodes.channel.item
       return feedUrl ==~ VALID_FEED_URL
   }


   ContentURLContainer extractUrl(Map links, PreferredQuality requestedQuality) {
      def linkUrl = links.default  /* The <link> tag */
      def contentUrl    /* The video to be played to be returned */

      // look at each item to find the link url and keep the match
      def matchedItem
      // set matchedItem with contents of Item with matching link
      itemNode.each { it -> if(it.link.text() == linkUrl.toString()) matchedItem = it } 

      //now extract the stream url       
      def mediaLink = matchedItem =~ 'your regex exression goes here'               
      contentUrl = mediaLink[0][1]
   
      return new ContentURLContainer(contentUrl: contentUrl)
   }

Re: Pleas help write plugin

PostPosted: Sat Dec 10, 2011 1:41 am
by zip
you probably don't have to parse the RSS in the plugin, it looks like there is a pattern between the default link URL and the video URL.

Re: Pleas help write plugin

PostPosted: Sat Dec 10, 2011 2:26 am
by jhb50
That's probably true for any rss but I had thought it better to do the parse of the whole rss once into items and then match the links to the items and then scan the item for the url rather than scan the rss from the beginning for each link. I don't know how a regex is matched but I assumed it would also take more resource the longer the string that has to be matched.

Re: Pleas help write plugin

PostPosted: Sat Dec 10, 2011 3:42 pm
by zip
No need to scan the whole feed for each item. the extractUrl method will get a map of links for each item, in this case it'll include something like

  Code:
['default':'http://tv.delfi.lt/video/tPA0Sr7h']


and from there the expected content URL is

  Code:
http://ytv.delfi.lt/v/tPA0Sr7h.mp4


so it'd be a few lines of code to go from one to the other

Re: Pleas help write plugin

PostPosted: Sat Dec 10, 2011 5:07 pm
by jhb50
OK, now I see..in this case the links can easily be transformed into the filename and the missing .mp4 extension added so we don't even need to look at the malformed media:content tag. But that's kinda special and if they ever change the model or the extension for one of the files you're sol.

So to avoid that I would look at the media:content tag but I either need to scan the rss for each item or just scan the rss once in the extractor for the feed. Maybe that's the same thing but I think the latter is neater and perhaps more effecient as I posted earlier. I'd appreciate your comments because I'm trying to learn about all this. :?

Re: Pleas help write plugin

PostPosted: Sat Dec 10, 2011 5:29 pm
by zip
jhb50 wrote:So to avoid that I would look at the media:content tag but I either need to scan the rss for each item or just scan the rss once in the extractor for the feed. Maybe that's the same thing but I think the latter is neater and perhaps more effecient as I posted earlier. I'd appreciate your comments because I'm trying to learn about all this. :?

The thig is the extractorMatches() method is possibly called for all new feed URLs - when Serviio goes through all plugins to find the first that matches. So if you want to parse the feed there it should be inside an if statement, like (pseudo code):

  Code:
boolean matches = match(url)
if(url matches) {
   parseXml()
}
return matches

Re: Pleas help write plugin

PostPosted: Sat Dec 10, 2011 6:07 pm
by jhb50
:roll: Yea, should have thought of that. Thanks. Makings of a good Wiki with all these tips!

Re: Pleas help write plugin

PostPosted: Thu Dec 15, 2011 4:49 pm
by Neoromancer
I realized that I really do not understand anything plugin writing.

Have done a little differently, just picked up a rss web and edited so that the program should understand him. It looks like http://podcast.openbox.info/delfi.xml. I update three times a week.

Sorry for bad English.