It is currently October 24th, 2020, 6:52 am

Variable number of items in RSS feed?

Help with creating, editing & fixing problems with skins
agcme
Posts: 4
Joined: August 6th, 2010, 9:50 pm

Variable number of items in RSS feed?

Post by agcme »

I am trying to set up a WebParser RSS feed reader following the various examples on this site but I've gotten stuck with one problem: variable feed number.

Specifically I'm trying to create a feed for the National Hurricane Center's Atlantic feed:
http://www.nhc.noaa.gov/index-at.xml

The problem is that the examples listed for RSS show the five most recent entries on a generic RSS feed. The NHC's hurricane feed has a variable number of entries. On days when there are no storms, there's only two (one announcing no cyclonic disturbances and the other announcing the Atlantic Tropical Weather Outlook). But when a storm pops up, several new items show up in the feed (Advisories, Forecasts, Projections, etc.).

Now, when I have the regex set to display five feeds as per the RSS examples (using StringIndex, etc.), Rainmeter bombs out when the list is too short. The log files show that something has happened and ends up with an index of -1 presumably because the twelve saved patterns (.*) in the regex don't exist (only two item tags instead of five). I verified this by shortening the regex to just four saved patterns and it works but obviously only provides the first two item entries (two saved patterns per entry, one for title and one for link).

Is there a way to display an RSS feed with a variable number of items such that it always works?

The original regex is:

RegExp="(?siU)<title>(.*)</title>.*<link>(.*)</link>.*<item>.*<title>(.*)</title>.*<link>(.*)</link>.*<item>.*<title>(.*)</title>.*<link>(.*)</link>.*<item>.*<title>(.*)</title>.*<link>(.*)</link>.*<item>.*<title>(.*)</title>.*<link>(.*)</link>.*<item>.*<title>(.*)</title>.*<link>(.*)</link>"

This does work when a storm is active and there are more than five items on the feed (there are typically twelve items per storm). But when things turn calm, the updates fail completely and I get a blank meter. Of course, this also doesn't work if there are two active storms because then there are at least 24 items but my regex only has five saved patterns.
User avatar
kenz0
Developer
Posts: 264
Joined: July 31st, 2009, 2:23 pm
Location: Tokyo, JPN

Re: Variable number of items in RSS feed?

Post by kenz0 »

I often use the method that used "Alternation" to solve a similar problem.

For example, this pattern means the one entry (contents of <item> tag), OR nothing

(?:(<item>.*</item>)|)


Then, If you connect these three pattern blocks, this pattern would mean like,
"three entries OR two entries OR one entry OR nothing"

(?:(<item>.*</item>)|)(?:(<item>.*</item>)|)(?:(<item>.*</item>)|)


If this approach is used in your example,
"(?:.*<title>(.*)<.*<link>(.*)<|)" is a one block, so if you need 6 entries,

Code: Select all

RegExp="(?siU)<item>(?:.*<title>(.*)<.*<link>(.*)<|)(?:.*<title>(.*)<.*<link>(.*)<|)(?:.*<title>(.*)<.*<link>(.*)<|)(?:.*<title>(.*)<.*<link>(.*)<|)(?:.*<title>(.*)<.*<link>(.*)<|)(?:.*<title>(.*)<.*<link>(.*)<|)"
This would not cause a matching error even if there is only one entry.


Note that this method may not be orthodox to be exact. but you can use this as easy-to-understand solution for such problem that unfixed number of items.
.
Image
User avatar
jsmorley
Developer
Posts: 21424
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Variable number of items in RSS feed?

Post by jsmorley »

kenz0 wrote:I often use the method that used "Alternation" to solve a similar problem.

For example, this pattern means the one entry (contents of <item> tag), OR nothing

(?:(<item>.*</item>)|)


Then, If you connect these three pattern blocks, this pattern would mean like,
"three entries OR two entries OR one entry OR nothing"

(?:(<item>.*</item>)|)(?:(<item>.*</item>)|)(?:(<item>.*</item>)|)


If this approach is used in your example,
"(?:.*<title>(.*)<.*<link>(.*)<|)" is a one block, so if you need 6 entries,

Code: Select all

RegExp="(?siU)<item>(?:.*<title>(.*)<.*<link>(.*)<|)(?:.*<title>(.*)<.*<link>(.*)<|)(?:.*<title>(.*)<.*<link>(.*)<|)(?:.*<title>(.*)<.*<link>(.*)<|)(?:.*<title>(.*)<.*<link>(.*)<|)(?:.*<title>(.*)<.*<link>(.*)<|)"
This would not cause a matching error even if there is only one entry.


Note that this method may not be orthodox to be exact. but you can use this as easy-to-understand solution for such problem that unfixed number of items.
This would make a very nice "Tips and Tricks" entry on the main site. ;-)
agcme
Posts: 4
Joined: August 6th, 2010, 9:50 pm

Re: Variable number of items in RSS feed?

Post by agcme »

I gave the alternation a try but it didn't work. The log file shows:

DEBUG: (47:01:02.797) Refreshing (Name: "Tranquil\Weather" Ini: "NHC.ini")
DEBUG: (47:01:02.968) WebParser: Fetching URL: http://www.nhc.noaa.gov/index-at.xml
DEBUG: (47:01:03.108) WebParser: Finished URL: http://www.nhc.noaa.gov/index-at.xml
DEBUG: (47:01:03.108) WebParser: Matching error! (-1)

And the regexp is:

RegExp="(?siU)<title>(.*)</title>.*<link>(.*)</link>(?:.*<item>.*<title>(.*)</title>.*<link>(.*)</link>|)(?:.*<item>.*<title>(.*)</title>.*<link>(.*)</link>|)(?:.*<item>.*<title>(.*)</title>.*<link>(.*)</link>|)

The first title and link are always available, it's the title of the feed itself. After that the items are available. I'm sure I've done something wrong but I can't quite figure out the error.
User avatar
kenz0
Developer
Posts: 264
Joined: July 31st, 2009, 2:23 pm
Location: Tokyo, JPN

Re: Variable number of items in RSS feed?

Post by kenz0 »

You are forgetting to put a quotation mark at the end of line.
RegExp="(?siU)<title>(.*)</title>.*<link>(.*)</link>(?:.*<item>.*<title>(.*)</title>.*<link>(.*)</link>|)(?:.*<item>.*<title>(.*)</title>.*<link>(.*)</link>|)(?:.*<item>.*<title>(.*)</title>.*<link>(.*)</link>|)"
.
Image
agcme
Posts: 4
Joined: August 6th, 2010, 9:50 pm

Re: Variable number of items in RSS feed?

Post by agcme »

Like I said, I'm sure I'm doing something wrong. :)

Works great now, thank you all for your help.
agcme
Posts: 4
Joined: August 6th, 2010, 9:50 pm

Re: Variable number of items in RSS feed?

Post by agcme »

One more problem with the variable feed:

The regex is working as far as not failing if it can't find enough pattern matches so that part is solved. The new problem is the way the data is displayed.

In the feed, there is a title and a link for the entire feed (Meter #0) and then a title and a link for each item (Meters #1 through #5). So right now I have six measures and six meters that pull the appropriate StringIndex to display and/or generate the link. Yesterday the storm feed went to its zero storm condition (two items) after the only storm in the Atlantic dissipated. However, the meters didn't update correctly. The title (Meter #0) is fine as are the first two meters (#1 and #2) correct and show just the message about no tropical cyclones and the Tropical Weather Outlook. But meters #3, #4, and #5 show old data instead of showing blank entries because no entries exist. The RSS feed itself has only the main title and two <item> entries.

How do I ensure that the new meters refresh properly to show the blank data?
User avatar
jsmorley
Developer
Posts: 21424
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Variable number of items in RSS feed?

Post by jsmorley »

agcme wrote:One more problem with the variable feed:

The regex is working as far as not failing if it can't find enough pattern matches so that part is solved. The new problem is the way the data is displayed.

In the feed, there is a title and a link for the entire feed (Meter #0) and then a title and a link for each item (Meters #1 through #5). So right now I have six measures and six meters that pull the appropriate StringIndex to display and/or generate the link. Yesterday the storm feed went to its zero storm condition (two items) after the only storm in the Atlantic dissipated. However, the meters didn't update correctly. The title (Meter #0) is fine as are the first two meters (#1 and #2) correct and show just the message about no tropical cyclones and the Tropical Weather Outlook. But meters #3, #4, and #5 show old data instead of showing blank entries because no entries exist. The RSS feed itself has only the main title and two <item> entries.

How do I ensure that the new meters refresh properly to show the blank data?
Coincidentally, this issue with WebParser will be fixed in the next beta 1.3 of Rainmeter.