LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2006, 01:13 AM   #1
stardotstar
Member
 
Registered: Nov 2002
Location: /au/qld/bne/4157
Distribution: Gentoo mactel-linux
Posts: 238

Rep: Reputation: 30
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.*
 
Old 07-12-2006, 03:40 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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".
 
Old 07-12-2006, 03:51 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
And I thought he was talking about a windows box with .NET ...


Cheers,
Tink
 
Old 07-12-2006, 05:26 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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
 
Old 07-12-2006, 05:30 AM   #5
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
@unSpawn: cool! Did you get this script working on a page? Which page? I was trying it out but it did nothing for me ...
 
Old 07-12-2006, 05:47 AM   #6
stardotstar
Member
 
Registered: Nov 2002
Location: /au/qld/bne/4157
Distribution: Gentoo mactel-linux
Posts: 238

Original Poster
Rep: Reputation: 30
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.
 
Old 07-12-2006, 06:10 AM   #7
stardotstar
Member
 
Registered: Nov 2002
Location: /au/qld/bne/4157
Distribution: Gentoo mactel-linux
Posts: 238

Original Poster
Rep: Reputation: 30
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
 
Old 07-12-2006, 06:36 AM   #8
stardotstar
Member
 
Registered: Nov 2002
Location: /au/qld/bne/4157
Distribution: Gentoo mactel-linux
Posts: 238

Original Poster
Rep: Reputation: 30
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
 
Old 07-12-2006, 06:40 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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).

Last edited by unSpawn; 07-12-2006 at 06:42 AM.
 
Old 07-12-2006, 07:05 AM   #10
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
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
 
Old 07-12-2006, 08:01 AM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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; }
 
Old 07-12-2006, 08:54 AM   #12
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
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.
 
Old 07-12-2006, 06:54 PM   #13
stardotstar
Member
 
Registered: Nov 2002
Location: /au/qld/bne/4157
Distribution: Gentoo mactel-linux
Posts: 238

Original Poster
Rep: Reputation: 30
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

Last edited by stardotstar; 07-12-2006 at 07:00 PM.
 
Old 07-12-2006, 07:26 PM   #14
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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)" \
 
Old 07-12-2006, 07:31 PM   #15
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to set the login background image when using KDM PTrenholme Fedora 0 04-15-2006 09:20 AM
How to set up web page background? freekamail Linux - Newbie 2 12-19-2005 09:48 AM
Can't set desktop background image in Gnome TruckStuff Linux - General 1 07-18-2005 03:15 PM
set focus on a text form of a web page? codec Programming 2 12-31-2004 04:32 AM
Kicking off a script form a web page Hazzie Programming 2 04-26-2004 01:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration