It is currently March 29th, 2024, 12:19 am

How to get an image from the internet?

General topics related to Rainmeter.
cd0123
Posts: 9
Joined: December 6th, 2010, 2:20 pm

How to get an image from the internet?

Post by cd0123 »

Hi everyone, i would like to ask a question. I want to write a code to get an image from the internet but I don't know how to write. For example, I want to get the image from http://rainmeter.net/cms/ at top left corner. How to edit the code? Thank you very much :D
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How to get an image from the internet?

Post by jsmorley »

Well, that is a bad example, as the answer is: "you can't". That image is a background image and part of a stylesheet and you can't grab it from the HTML source for the site with WebParser.

But let's take another case. The "CNN" logo image on http://www.cnn.com at the top.

Image

You will want to read about WebParser to understand what this is doing, so you can use the approach in your own skins, but as an example; to get that image I would do:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1

[MeasureCnnSite]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=http://www.cnn.com
RegExp="(?siU)<a id="hdr-banner-title".*<img src="(.*)""
StringIndex=1
Download=1
UpdateRate=86400

[MeterCnnImage]
Meter=Image
MeasureName=MeasureCnnSite
ImageName=%1
http://rainmeter.net/cms/Plugins-WebParser_beta
http://rainmeter.net/cms/Tips-WebParserPrimer
http://rainmeter.net/cms/Rainmeter101-Tutorial4
cd0123
Posts: 9
Joined: December 6th, 2010, 2:20 pm

Re: How to get an image from the internet?

Post by cd0123 »

jsmorley wrote:Well, that is a bad example, as the answer is: "you can't". That image is a background image and part of a stylesheet and you can't grab it from the HTML source for the site with WebParser.

But let's take another case. The "CNN" logo image on http://www.cnn.com at the top.

Image

You will want to read about WebParser to understand what this is doing, so you can use the approach in your own skins, but as an example; to get that image I would do:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1

[MeasureCnnSite]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=http://www.cnn.com
RegExp="(?siU)<a id="hdr-banner-title".*<img src="(.*)""
StringIndex=1
Download=1
UpdateRate=86400

[MeterCnnImage]
Meter=Image
MeasureName=MeasureCnnSite
ImageName=%1
http://rainmeter.net/cms/Plugins-WebParser_beta
http://rainmeter.net/cms/Tips-WebParserPrimer
http://rainmeter.net/cms/Rainmeter101-Tutorial4
thanks for your answer. In my example, I want to get the image which is under the logo of rainmeter. Since this image is changed in definite time, I don't know whether the rainmeter can get it or not. Sorry for my bad description.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How to get an image from the internet?

Post by jsmorley »

Not really. Those images in that slideshow are all preloaded and then displayed one after the other by javascript. You could get all the images by parsing the HTML, but not any given one at a particular time.
cd0123
Posts: 9
Joined: December 6th, 2010, 2:20 pm

Re: How to get an image from the internet?

Post by cd0123 »

jsmorley wrote:Not really. Those images in that slideshow are all preloaded and then displayed one after the other by javascript. You could get all the images by parsing the HTML, but not any given one at a particular time.
you mean the rainmeter cannot read the javascript code from the internet?
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How to get an image from the internet?

Post by jsmorley »

cd0123 wrote: you mean the rainmeter cannot read the javascript code from the internet?
WebParser simply gets the raw HTML that is sent to out when you execute a call to a URL. If there is client-side code like Javascript, it will get the Javascript, but does not execute it. It is not a web browser. Those images are displayed in your browser by your local Firefox or IE or Chrome application executing the script in a local Javascript engine:

It should be clear, so you know what kind of relationship Rainmeter has with the "web" and what it doesn't. WebParser more or less connects to a URL, lies through its teeth and says "Hey, I'm a web browser, send me that page." Then it just captures the raw stream of data the web server sends back, and saves it to a temporary file that can be parsed with Regular Expression to extract strings. It is in no way an actual web browser, cares not a whit about HTML/CSS/Javascript/Flash or anything else used by a web browser to "render" the page, and will treat any text file exactly as it does a page from the web. That's why file:// works just as well as http://. Wtih file:// WebParser in effect just skips the connecting and downloading part and goes straight to opening the file and parsing it with Regular Expression.

It does have one additional capability, and that is once you DO parse the raw stream and extract the location and name of an image file, you can say "Download=1" and WebParser will actually go out again and download the image file using a URL like: http://LocationYouParsed/ImageNameYouParsed.ext and you can then easily display the image.

Our main site returns this Javacript code as part of the stream when you connect to it:

Code: Select all

<script type="text/javascript">
		
			var mygallery=new fadeSlideShow({
				wrapperid: "slideshow", //ID of blank DIV on page to house Slideshow
				dimensions: [320, 280], //width/height of gallery in pixels. Should reflect dimensions of largest image
				imagearray: [
					['./slideshow/Slideshow1.jpg', 'http://toastbrotpascal.deviantart.com/art/ABP-173567354', '_blank', 'ABP by toastbrotpascal'],
					['./slideshow/Slideshow2.jpg', 'http://faradeyua.deviantart.com/art/Kotoko-Suite-v2-0-Updated-177128472', '_blank', 'Kotoko Suite v2.0 -Updated- by FaradeyUA'],
					['./slideshow/Slideshow3.jpg', 'http://alekaldinger.deviantart.com/art/Lady-Gaga-Tribute-179201852', '_blank', 'Lady Gaga Tribute by AlekAldinger'],
					['./slideshow/Slideshow4.jpg', 'http://g00glen00b.deviantart.com/art/Rain2-for-Rainmeter-176794964', '_blank', 'Rain2 for Rainmeter by g00glen00b'],
					['./slideshow/Slideshow5.jpg', 'http://minhtrimatrix.deviantart.com/art/Speed-165136605', '_blank', 'Speed by minhtrimatrix'],
					['./slideshow/Slideshow6.jpg', 'http://vclouds.deviantart.com/art/VClouds-Weather-2-179058977', '_blank', 'VClouds Weather 2 by VClouds'],
					['./slideshow/Slideshow7.jpg', 'http://xeissirk.deviantart.com/art/Crooked-Calendar-172876929', '_blank', 'Crooked Calendar by xeissirK'],
					['./slideshow/Slideshow8.jpg', 'http://ld-jing.deviantart.com/art/Ventuz-172650844', '_blank', 'Ventuz by ld-jing']
				],
				displaymode: {type:'auto', pause:5000, cycles:0, wraparound:false},
				persist: false, //remember last viewed slide and recall within same session?
				fadeduration: 1000, //transition duration (milliseconds)
				descreveal: "ondemand",
				togglerid: ""
			})

	</script>
So although you could parse this code and get the locations of all the images, there is no way to get any particular one at any particular time. You could write a skin that gets all 8 of those images and then displays them in some kind of slideshow or something without too much trouble though. There is a little "gotcha" since the page is using "relative" locations for the images and not a full URL, but that can be addressed using the technique here: http://rainmeter.net/cms/Tips-WebParserImageRelative.

If you want to see what you can and can't get from any particular site, just go to the site in your browser of choice, right click and say "Show source". What you get using that is exactly what WebParser sees. No more, no less.