WebParser: Relative Paths


There is an issue with retrieving and displaying images from web sites when the source HTML has them referred to as a "relative path" to the image.

WebParser can download and display images by doing a WebParser Measure which gets the full URL and name of the image, and setting "Download=1" on the Measure. Then a Meter using Meter=Image with a MeasureName=xxxx referring to the Measure which downloaded the image will display the downloaded image.

Problem

The problem came in when the image was referred to in the web site HTML using a relative path to the image. An example is the logo at the top of the Rainmeter forums:

<a href="./index.php" title="Board index" id="logo"><img src="./styles/saphic/imageset/site_logo.png"/></a>

As you can see, the full URL is not included in anything that WebParser can search for, so Download=1 fails...

Solution

A WebParser Measure can append the RESULTS of another WebParser Measure in the "URL" line by referencing the Measure in square brackets. This means you can use the first measure to return the name and location of the image you want, and use this information in a second Measure where you hard code the URL and then pass the result of the search for the image to the second Measure.

In order to have the two measures work in the correct "order", so the second one isn't looking for the relative URL before the first one is done getting it, you should make the second measure "dynamic", and use [!CommandMeasure SecondMeasure "Update"] as a FinishAction on the first measure. That way the first measure will "drive" the second one.

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
[MeasureImageLocation]
Measure=Plugin
Plugin=WebParser
UpdateRate=1800
Url=http://forum.rainmeter.net/
RegExp=(?siU)&lt;img src="(.*)"
StringIndex=1
FinishAction=[!CommandMeasure MeasureImageDownload "Update"]
[MeasureImageDownload]
Measure=Plugin
Plugin=WebParser
UpdateRate=1800
Url=http://forum.rainmeter.net/[&MeasureImageLocation]
StringIndex=1
DynamicVariables=1
Download=1
[MeterDisplayImage]
Meter=IMAGE
MeasureName=MeasureImageDownload
W=200
PreserveAspectRatio=1

Here we are using the first Measure [MeasureImageLocation] to get the relative path and filename of the image.

Then we use the second Measure [MeasureImageDownload] to combine the known URL and the information in a dynamic way [&MeasureImageLocation] to create a fully loaded Measure which will succeed in downloading the image... Then we just display it.