FAQ  •  Register  •  Login

[Solved] Groovy .each match Problem

<<

jhb50

DLNA master

Posts: 2854

Joined: Thu Jun 30, 2011 9:32 pm

Post Mon Nov 07, 2011 1:14 pm

[Solved] Groovy .each match Problem

I'm new to Java and Groovy so while this may be a trivial question it has me stumped. (I have shortened things for purposes of this post)

In the online.groovy I am working on I have "linkUrl" as set by :

  Code:
Map videoLinks = ['default': new URL('http://www.johncleesepodcast.co.uk/cleeseblog/2008/10/john-cleese-podcast-34-headmaster.html')]
      ContentURLContainer result = extractor.extractUrl(videoLinks, PreferredQuality.MEDIUM)


ContentURLContainer extractUrl(Map links, PreferredQuality requestedQuality) {
      def linkUrl = links.default 
and I have an array of 3 items "itemNode" created by XmlParser each of the form:

  Code:
item[attributes={}; value=[
title[attributes={}; value=[John Cleese Podcast #34: The Headmaster]],
link[attributes={}; value=[http://www.johncleesepodcast.co.uk/cleese-podcast-34-headmaster.html]],
{http://purl.org/syndication/thread/1.0}total[attributes={}; value=[4]]]],
when I execute the statements

  Code:
itemNode.each { it -> if(it.link.text() == linkUrl) matchedItem = it else println "x"+it.link.text()+"x"+linkUrl+"x" } 
println matchedItem
the result is:

  Code:
xhttp://www.johncleesepodcast.co.uk/advert-for-headcast.htmlxhttp://www.johncleesepodcast.co.uk/ccleese-podcast-34-headmaster.htmlx
xhttp://www.johncleesepodcast.co.uk/podcast-34-headmaster.htmlxhttp://www.johncleesepodcast.co.uk/podcast-34-headmaster.htmlx
xhttp://www.johncleesepodcast.co.uk/podcast-33-brain-explained.htmlxhttp://www.johncleesepodcast.co.uk/podcast-34-headmaster.htmlx
matchedItem = null
which shows that groovy did not detect the match even though the values print as identical.

Can anyone explain what I have done wrong?
Last edited by jhb50 on Mon Nov 07, 2011 10:49 pm, edited 2 times in total.
<<

zip

User avatar

Serviio developer / Site Admin

Posts: 16740

Joined: Sat Oct 24, 2009 12:24 pm

Location: London, UK

Post Mon Nov 07, 2011 2:14 pm

Re: Groovy .each match Problem

Not sure I understand. Serviio parses the actual RSS for you and will feed 'links' to the method.

This is mocking up Serviio's behaviour and call to the plugin as a test:

  Code:
Map videoLinks = ['default': new URL('http://www.johncleesepodcast.co.uk/cleeseblog/2008/10/john-cleese-podcast-34-headmaster.html')]
ContentURLContainer result = extractor.extractUrl(videoLinks, PreferredQuality.MEDIUM)


In your method you're rightly retrieving link with 'default' key (as setup above):

  Code:
ContentURLContainer extractUrl(Map links, PreferredQuality requestedQuality) {
def linkUrl = links.default


So in your case you now have to read the HTML source for that page:

  Code:
String htmlPage = linkUrl.getText()


And then use some RegEx on the HTML text to retrieve the file URL.

I don't think XmlParser is useful in this scenario as you have no XML to parse.
<<

jhb50

DLNA master

Posts: 2854

Joined: Thu Jun 30, 2011 9:32 pm

Post Mon Nov 07, 2011 3:40 pm

Re: Groovy .each match Problem

Yes, you misunderstand..this is not my complete .groovy. Its only a test running the groovy standalon and I've only shown the relelevant parts to illustrate the problem. I created the links array using XmlParser as I said and I can execute an .each command to print out each element of the array, but the match command does not work, as I have posted.

I am only testing various things before I write my actual groovy. I should be able to match values in array elements.
<<

zip

User avatar

Serviio developer / Site Admin

Posts: 16740

Joined: Sat Oct 24, 2009 12:24 pm

Location: London, UK

Post Mon Nov 07, 2011 5:00 pm

Re: Groovy .each match Problem

ok.

What exactly are you trying to do? Find item whose link equals linkUrl? Then I'd look at find closure, something like:

  Code:
def matchedNode = itemNode.find { it -> it.link.text() == linkUrl }
<<

jhb50

DLNA master

Posts: 2854

Joined: Thu Jun 30, 2011 9:32 pm

Post Mon Nov 07, 2011 5:36 pm

Re: Groovy .each match Problem

Yes that's what I'm trying to test, but your suggestion does not work either.

Attached is the test groovy, showing all 3 statements fail. Remember all I'm trying to do at this stage is test various language constructs with rss links as part of learning groovy before writing a real one.

I found this ".each ( it -> if" statement in the iPlayer.groovy and was trying it out. It works there but not in my test.
  Code:
linkNode.'media:content'[0].'media:thumbnail'.each { it -> if( selectedThumbNode == null || (it.'@width'.toInteger() > selectedThumbNode.'@width'.toInteger() && it.'@width'.toInteger() <= 160) ) selectedThumbNode = it }
Attachments
Cleeseblog2.groovy
(2.67 KiB) Downloaded 202 times
<<

zip

User avatar

Serviio developer / Site Admin

Posts: 16740

Joined: Sat Oct 24, 2009 12:24 pm

Location: London, UK

Post Mon Nov 07, 2011 7:01 pm

Re: Groovy .each match Problem

Right, got it.

The problem is that linkUrl is instance of URL class, not String, therefore when you later do the equals (==) it won't match as one is String and the other is URL. That's one of the joys of using deferred typing of Groovy ;-)

You have 2 choices:

1)

Type linkUrl to be String

  Code:
def linkUrl = links.default


becomes

  Code:
String linkUrl = links.default


2) convert the URL to String before comparison:

  Code:
def matchedNode = itemNode.find { it -> it.link.text() == linkUrl.toString() }
<<

jhb50

DLNA master

Posts: 2854

Joined: Thu Jun 30, 2011 9:32 pm

Post Mon Nov 07, 2011 9:27 pm

Re: Groovy .each match Problem

Ahha .. I had thought that was the problem so I tried using $linkUrl but it threw an exception. That worked earlier with
  Code:
saveUrl = "$feedUrl"  //save rss url in string format for getting the feed later
.toString() works great. Thanks for your help.

Return to Plugin development

Who is online

Users browsing this forum: No registered users and 2 guests

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.