LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   simple script to grab an image from a web page and set background (https://www.linuxquestions.org/questions/programming-9/simple-script-to-grab-an-image-from-a-web-page-and-set-background-463192/)

stardotstar 07-12-2006 01:13 AM

simple script to grab an image from a web page and set background
 
I have a need to set the desktop to an image that appears as the only img tag on an html webpage.

The windows system being used for those clients is:

go to the page
find the image by identifying the img tags in the source
copy file to location
set desktop background

call this prog on startup and execute and exit

This has been done in dotnet I think.

Someone must have done this - know any links or what approach would be best - I am not very used to tackling scripting problems beyond basic local bash stuff.

TIA
Will.*

unSpawn 07-12-2006 03:40 AM

I am not very used to tackling scripting problems beyond basic local bash stuff.
Still it would have been cool to post whatever you tried, just to show us you did *try*.
Also look at Freshmeat.net and Sourceforge.org. There's a true cornucopia of tools waiting to be discovered...

go to the page, find the image by identifying the img tags in the source, copy file to location
Ayway, since I feel like reinventing some wheels today, maybe something along the lines of:
Code:

#!/bin/sh
uri="$1"; wget -O - "$uri" 2>/dev/null|grep "\<img.*src=" 2>/dev/null|\
head -1|sed -e "s/.*><img/<img/g" -e "s/><.*$/>/g"|while read l; do
l=(${l}); for i in $(seq 0 $[${#l[@]}-1]); do if [ "${l[$i]:0:3}" = "src" ]; then
img="$(dirname "$uri")$(echo ${l[$i]:4}|tr -d "\"")"; fi; done;
wget -q "$img"; done

YMMV(VM)


set desktop background
You figure that out using something like "xloadimage -onroot file.ext".

Tinkster 07-12-2006 03:51 AM

And I thought he was talking about a windows box with .NET ...


Cheers,
Tink

unSpawn 07-12-2006 05:26 AM

And I thought he was talking about a windows box with .NET
Was he? Hmm. Shame. I'll go find some other wheels to reinvent, then ;-p

muha 07-12-2006 05:30 AM

@unSpawn: cool! Did you get this script working on a page? Which page? I was trying it out but it did nothing for me ...

stardotstar 07-12-2006 05:47 AM

Thanks guys, I actually didn't even try - I didn't know where to begin - so I asked the question here; My scripts start and stop networking processes with simple config changes and do things to VirtualMachines, launch rsyncs and what not so I am at a loss with real programming.

Thank you for the links and example script - you are most generous and I appreciate the effort you put in.

And no it is not for a windows box - linux systems that need to coexist on a windows centric network...
I can see that the original post was very unclear on that score :oops:
Will

stardotstar 07-12-2006 06:10 AM

that script is great and I think I get the gist of it - I can't make it actually get an image but maybe that is because the tagging of the image is difficult...

I added some echoing so i could see if the $img was correctly populated but it never output... The $uri is correctly set.

here is the source of the image definition:
here is the properties of the image in the browser:
Code:

http://intrasv01.homx.casga/picup/image/cata/splash_12_07_06_lr.jpg
and here is the definitions in the html:

Code:

<br>
<a href="image/cata/splash_12_07_06_hr.jpg">
<IMG SRC="image/cata/splash_12_07_06_lr.jpg"
alt="Splash Group Nd.  Click for Hi_Res for Backgrounds Splash on cata relief."></a>
</td>

<td VALIGN="TOP">
<br>
<center>

Does this make it clearer.

I understand you have made a substantial effort to assist and I will work to understand what you have done better. I am very new to this level of scripting and I appreciate your time.

I basically need to automate the grabbing and saving of the high res version of the single image displayed on that intranet page...

Will

stardotstar 07-12-2006 06:36 AM

BTW thanks for going the extra mile for me - don't feel like you have to reinvent the reinvention - I will be scavanging for those other resources as well. Just need to accomplish this as its a part of local intranet compliance on machines and several of us run linux in a windows world which is quite unforgiving. Thanks for your help :)

unSpawn 07-12-2006 06:40 AM

And no it is not for a windows box - linux systems that need to coexist on a windows centric network
That's cool. Let's support that effort by finishing off the script and add some minor checking:
Code:

#!/bin/sh
uri="$1"; wget -O - "$uri" 2>/dev/null|tr [A-Z] [a-z]|grep -ie "\<img.*src=" 2>/dev/null|head -1\
|sed -e "s/.*<img/<img/g" -e "s/><.*$/>/g"|while read l; do l=(${l})
 for i in $(seq 0 $[${#l[@]}-1]); do
  echo "${l[$i]:0:3}"|grep -qie src && { img="$(dirname "$uri")$(echo ${l[$i]:4}|tr -d "\"")"; break; };
 done
 rand=$(date|sha1sum|cut -c 1-26); wget -q "$img" -O "/tmp/img.$rand" && \
 { file -bi /tmp/setimgfile|egrep -qe "^image\/(gi|jp|pn)" \
  && xloadimage -onroot "/tmp/img.$rand" && rm -f "/tmp/img.$rand"; };
done; exit 0

If saved as "getImg.sh" and testrun like "./getImg.sh http://www.google.com/ncr" it fills your desktop with that abomination ;-p If it doesn't work run as "sh -x ./getImg.sh uri-of-choice 2>&1 | tee getImg.tee" and post the contents of getImg.tee (you're invited to do so as well muha).

muha 07-12-2006 07:05 AM

Hmm, it grabs the image like you said. (i did not realize it would be in the /tmp dir)
It doesn't manage to stick it to my desktop though. Which command in your script would do that?
Quote:

$: sh -x ./getImg.sh http://www.google.com/ncr 2>&1 | tee getImg.tee
+ uri=http://www.google.com/ncr
+ wget -O - http://www.google.com/ncr
+ tr '[A-Z]' '[a-z]'
+ grep -ie '\<img.*src='
+ head -1
+ sed -e 's/.*<img/<img/g' -e 's/><.*$/>/g'
+ read l
+ l=(${l})
++ seq 0 4
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo '<im'
+ grep -qie src
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo src
+ grep -qie src
++ dirname http://www.google.com/ncr
++ echo '"/intl/en/images/logo.gif"'
++ tr -d '"'
+ img=http://www.google.com/intl/en/images/logo.gif
+ break
++ date
++ sha1sum
++ cut -c 1-26
+ rand=e0e755ffc570f6f48ed2f50aa7
+ wget -q http://www.google.com/intl/en/images/logo.gif -O /tmp/img.e0e755ffc570f6f48ed2f50aa7
+ file -bi /tmp/setimgfile
+ egrep -qe '^image\/(gi|jp|pn)'
+ read l
+ exit 0

unSpawn 07-12-2006 08:01 AM

It doesn't manage to stick it to my desktop though.
Yes, that's because I know just enough scripting to fsck things up ;-p
Replace the lines:
Code:

{ file -bi /tmp/setimgfile|egrep -qe "^image\/(gi|jp|pn)" \
  && xloadimage -onroot "/tmp/img.$rand" && rm -f "/tmp/img.$rand"; };

with:
Code:

{ file -bi "/tmp/img.$rand"|egrep -qe "^image\/(gi|jp|pn)" \
  && xloadimage -onroot "/tmp/img.$rand" && rm -f "/tmp/img.$rand"; break; }


muha 07-12-2006 08:54 AM

Quote:

./getImg.sh: line 18: xloadimage: command not found
Which probably makes sense since i don't have it installed on my suse 10.0
Thanks for the script, might come in handy later on.

stardotstar 07-12-2006 06:54 PM

Fantastic - thank you works exactly as promised with one problem which I have identified as something to do with the line where the file type is set - it seems to wrk fine with gifs like the google one but as soon as I point to a jpg xloadimage fails:

Code:

tardotstar@geko ~ $ xloadimage -onroot  -center /tmp/img.7df45b6f2058c9d840ee211413
Warning: unknown JFIF revision number 0.00
/tmp/img.7df45b6f2058c9d840ee211413: unknown or unsupported image type
  Building XImage...done

Code:

{ file -bi "/tmp/img.$rand"|egrep -qe "^image\/(gi|jp|pn)" \
Code:


stardotstar@geko ~ $ file /tmp/daily_splash.jpg
/tmp/daily_splash.jpg: JPEG image data, JFIF standard 1.02, comment: "Adobe ImageReady\377"
stardotstar@geko ~ $ file /tmp/img.               
img.7df45b6f2058c9d840ee211413  img.dbd44c6e7596033f76f3794d42
img.ceb3972fea047f409ebec9323d  img.e464b9f0e3baf5821aaa286bc9
stardotstar@geko ~ $ file /tmp/img.7df45b6f2058c9d840ee211413
/tmp/img.7df45b6f2058c9d840ee211413: empty
stardotstar@geko ~ $ file /tmp/img.dbd44c6e7596033f76f3794d42
/tmp/img.dbd44c6e7596033f76f3794d42: empty
stardotstar@geko ~ $ file /tmp/img.e464b9f0e3baf5821aaa286bc9
/tmp/img.e464b9f0e3baf5821aaa286bc9: empty
stardotstar@geko ~ $ file /tmp/img.ceb3972fea047f409ebec9323d
/tmp/img.ceb3972fea047f409ebec9323d: GIF image data, version 89a, 276 x 110
stardotstar@geko ~ $

So the gif is properly populated with file information but the jpgs are not...

I am researching the way that file works and how it relates to "magic" and will report back if I can hack it out myself :)

Since I am so new to the pipes and management of so many bash commands I have unfortunately been unable to fix this and present an answer. I'm sure it is simple since xloadimage clearly works on the instance of the file that is saved from the browser as a jpg.

Thanks again for your help :)
Will

unSpawn 07-12-2006 07:26 PM

If you can't be bothered with the "file" check just replace
Code:

rand=$(date|sha1sum|cut -c 1-26); wget -q "$img" -O "/tmp/img.$rand" && \
 { file -bi /tmp/setimgfile|egrep -qe "^image\/(gi|jp|pn)" \
  && xloadimage -onroot "/tmp/img.$rand" && rm -f "/tmp/img.$rand"; };
done; exit 0

with
Code:

rand=$(date|sha1sum|cut -c 1-26); wget -q "$img" -O "/tmp/img.$rand" && \
 { xloadimage -onroot "/tmp/img.$rand" && rm -f "/tmp/img.$rand"; };
done; exit 0


stardotstar@geko ~ $ file /tmp/daily_splash.jpg
If you want to fix it you need to add output from file -bi, not just "file".
If the outcome is "image/jfif" then it'll prolly look like
Code:

egrep -qe "^image\/(gi|j[pf]|pn)" \

unSpawn 07-12-2006 07:31 PM

BTW: i did not realize it would be in the /tmp dir
Yes, that is a flaw. While it would be not so easy under normal(?) circumstances to predict the value of $rand (and for instance symlink it to a file to overwrite if the script was run as root) it should use "mktemp" instead of the $rand kludge to be safer.


All times are GMT -5. The time now is 10:37 AM.