simple script to grab an image from a web page and set background
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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".
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 ops:
Will
Last edited by stardotstar; 07-12-2006 at 05:49 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:
<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...
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
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:
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).
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
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
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
Last edited by stardotstar; 07-12-2006 at 07:00 PM.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.