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 08:32 PM

This has me confused because:

Code:

stardotstar@geko ~ $ file -bi /tmp/splash_daily.jpg
image/jpeg

and yet despite changine and/or removing the file section I get no joy.

Code:

stardotstar@geko ~ $ xloadimage -onroot /tmp/img.264c8bb569b018170488a59aa1
Warning: unknown JFIF revision number 0.00
/tmp/img.264c8bb569b018170488a59aa1: unknown or unsupported image type

stardotstar@geko ~ $ file -bi /tmp/img.264c8bb569b018170488a59aa1
application/x-empty

I think I am understanding what the egrep does - detects from the original file name what the extention is and attaches that to the file command. But I can't actually see what this is asking file to do... man file does not tell me about "setting" the file type this way and so:

Code:

stardotstar@geko ~ $ file -bi /tmp/img.264c8bb569b018170488a59aa1 image/jpeg
application/x-empty
cannot open `image/jpeg' (No such file or directory)

so clearly I am not understanding the purpose of the file command in the script - But then again I don't really know how all the escape characters and conditional statements work.

Without the file statement google gif works just fine:
Code:

stardotstar@geko ~ $ ./backgrounder.2 http://www.google.com/ncr
/tmp/img.74b42f74785bd34f07611f44da is a 276x110 GIF image with 256 colors
  Building XImage...done

so somehow the jpeg is not wanting to play at all...

Will

stardotstar 07-12-2006 09:38 PM

Strange, it is not quite doing what I expected. I'll try to summarise my findings and if you can see the benefit of pursuing it I appreciate it - but I understand the effort you have gone to already and don't want to turn this into a new sf project :)

I have removed the file removal so I can compare what is captured. It seems that anything except gifs get 0 file size and are unknown type.

Here is the result of executing the script on several pages and the list of files generated, IBM and Google worked anything that found a png or jpg failed to write any content:

Code:

stardotstar@geko ~ $ sh -x ./backgrounder.1 http://www.kernel.org 2>&1             
+ uri=http://www.kernel.org
+ wget -O - http://www.kernel.org
+ 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 7
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo '<im'
+ grep -qie src
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo sty
+ grep -qie src
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo sol
+ grep -qie src
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo '#bb'
+ grep -qie src
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo src
+ grep -qie src
++ dirname http://www.kernel.org
++ echo '"http://www1.kernel.org/bw-zeus1.png"'
++ tr -d '"'
+ img=http:http://www1.kernel.org/bw-zeus1.png
+ break
++ sha1sum
++ cut -c 1-26
++ date
+ rand=5c9cbd7d07df9a7eb87bf46873
+ wget -q http:http://www1.kernel.org/bw-zeus1.png -O /tmp/img.5c9cbd7d07df9a7eb87bf46873
+ read l
+ exit 0
stardotstar@geko ~ $ ls -l /tmp/img*
-rw-r--r-- 1 stardotstar stardotstar 0 Jul 13 12:10 /tmp/img.5c9cbd7d07df9a7eb87bf46873

Code:

stardotstar@geko ~ $ sh -x ./backgrounder.2 http://www.ibm.org 2>&1
+ uri=http://www.ibm.org
+ tr '[A-Z]' '[a-z]'
+ grep -ie '\<img.*src='
+ head -1
+ sed -e 's/.*<img/<img/g' -e 's/><.*$/>/g'
+ read l
+ wget -O - http://www.ibm.org
+ l=(${l})
++ seq 0 6
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo '<im'
+ grep -qie src
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo hei
+ grep -qie src
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo wid
+ grep -qie src
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo bor
+ grep -qie src
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo alt
+ grep -qie src
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo src
+ grep -qie src
++ dirname http://www.ibm.org
++ echo '"//www.ibm.com/i/v14/t/ibm-logo.gif"'
++ tr -d '"'
+ img=http://www.ibm.com/i/v14/t/ibm-logo.gif
+ break
++ date
++ sha1sum
++ cut -c 1-26
+ rand=9eff1d95e7b8154bd10389f9d4
+ wget -q http://www.ibm.com/i/v14/t/ibm-logo.gif -O /tmp/img.9eff1d95e7b8154bd10389f9d4
+ file -bi /tmp/img.9eff1d95e7b8154bd10389f9d4
+ egrep -qe '^image\/(gi|j[pf]|pn)'
+ xloadimage -onroot /tmp/img.9eff1d95e7b8154bd10389f9d4
/tmp/img.9eff1d95e7b8154bd10389f9d4 is a 110x52 GIF image with 16 colors
  Building XImage...done
+ break
+ exit 0
stardotstar@geko ~ $ ls -l /tmp/img*
-rw-r--r-- 1 stardotstar stardotstar  0 Jul 13 12:10 /tmp/img.5c9cbd7d07df9a7eb87bf46873
-rw-r--r-- 1 stardotstar stardotstar 430 Oct  8  2004 /tmp/img.9eff1d95e7b8154bd10389f9d4

as you can see the IBM gif saved at 430 bytes and xloadimage worked fine.
so too below for google:

Code:

stardotstar@geko ~ $ sh -x ./backgrounder.2 http://www.google.com/ncr 2>&1
+ 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])'
+ grep -qie src
+ echo '<im'
+ 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=29a9da6b4c41a55add118c1f7d
+ wget -q http://www.google.com/intl/en/images/logo.gif -O /tmp/img.29a9da6b4c41a55add118c1f7d
+ file -bi /tmp/img.29a9da6b4c41a55add118c1f7d
+ egrep -qe '^image\/(gi|j[pf]|pn)'
+ xloadimage -onroot /tmp/img.29a9da6b4c41a55add118c1f7d
/tmp/img.29a9da6b4c41a55add118c1f7d is a 276x110 GIF image with 256 colors
  Building XImage...done
+ break
+ exit 0
stardotstar@geko ~ $ ls -l /tmp/img*
-rw-r--r-- 1 stardotstar stardotstar 8558 Jun  8 05:38 /tmp/img.29a9da6b4c41a55add118c1f7d
-rw-r--r-- 1 stardotstar stardotstar    0 Jul 13 12:10 /tmp/img.5c9cbd7d07df9a7eb87bf46873
-rw-r--r-- 1 stardotstar stardotstar  430 Oct  8  2004 /tmp/img.9eff1d95e7b8154bd10389f9d4
-rw-r--r-- 1 stardotstar stardotstar    0 Jul 13 12:06

When I point at a jpg - like the png it always just producds a null file type and content:

Code:

stardotstar@geko ~ $ file -bi /tmp/img*
image/gif
application/x-empty
image/gif
application/x-empty
application/x-empty
application/x-empty

Thanks

Will

UPDATE - I simplified the script further to try and see what was not working:

Code:

stardotstar@geko ~ $ sh -x ./backgrounder.4 http://antwrp.gsfc.nasa.gov/apod/astropix.html
+ uri=http://antwrp.gsfc.nasa.gov/apod/astropix.html
+ wget -O - http://antwrp.gsfc.nasa.gov/apod/astropix.html
+ 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 1
+ 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://antwrp.gsfc.nasa.gov/apod/astropix.html
++ echo '"image/0607/shuttlego_nasa.gif"'
++ tr -d '"'
+ img=http://antwrp.gsfc.nasa.gov/apodimage/0607/shuttlego_nasa.gif
+ break
+ wget -q http://antwrp.gsfc.nasa.gov/apodimage/0607/shuttlego_nasa.gif -O /tmp/img.daily
+ xloadimage -onroot /tmp/img.daily
Warning: unknown JFIF revision number 0.00
/tmp/img.daily: unknown or unsupported image type
+ read l
+ exit 0
stardotstar@geko ~ $ cat backgrounder.4
#!/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
wget -q "$img" -O "/tmp/img.daily";
xloadimage -onroot "/tmp/img.daily" ;
done; exit 0
stardotstar@geko ~ $ ls -l /tmp/img.daily
-rw-r--r-- 1 stardotstar stardotstar 0 Jul 13 15:50 /tmp/img.daily

This is a good site to test because it has daily changing images adn they vary file types... The current gif there is not working but it is an animation so maybe that is that.

unSpawn 07-15-2006 06:03 AM

"File" doesn't set anything, it just reads the MIME tag (in this case, due to -i). Egrep then (silently: -q) tries to match any string starting with "image/" and gi, jp or pn. "application/x-empty" means wget retrieved the file OK, but the file does not contain data. I'll cleanup some other stuff, rewrite in full and add comments. Note you loose PNG because xloadimage doesn't support it. You can of course always subsitute it for a loader that does. Also note you need to provide an URI in the format protocol://domain.tld/file.extension so protocol://domain.tld/ will not do. I could fix it by checking for and counting slashes or TLD position in "${uri:7}" and then not using "dirname" but not now. I also kept in recognition using "file" because I think that's the minimal check to do. I don't want to rely on extension alone.

Code:

#!/bin/sh
# Whatever first parameter is supplied serves as URI to grab.
uri="$1"
# If no arg given or protocol doesn't match that for teh intarweb we exit with a warning.
if [ "$#" != "1" -o "${uri:0:7}" != "http://" ]; then
 echo "${0##*/}: only one parameter needed: (URI + HTML rendered page: http://doma.in/file.ext), exiting." > /dev/stderr
 exit 1
fi
# Grab file and dump on stdout, make all lowercase, grep for HTML image tag, and only use first line.
# (not grep -m1), sed strips off tags before and after. Now dump the string in variable "l" and turn it into array.
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 each element in the array check if it matches the HTML image "src" tag,
 # first found match (break) gets stuffed in the "img" variable.
 for i in $(seq 0 $[${#l[@]}-1]); do
  echo "${l[$i]:0:3}"|grep -qie src
  if [ "$?" = "0" ]; then
    # Make compound of URI base, strip the src= part.
    img="$(dirname "$uri")$(echo ${l[$i]:4}|tr -d "\"")"
    # Decidedly lame way to strip not having a page:
    if [ "${img:0:10}" = "http:http:" ]; then
    img=${img:5}
    fi
    break
  fi
 done
 # Lame way to get a randomised string for temp file.
 rand=$(date|sha1sum|cut -c 1-26)
 # Grab image and output to temp file.
 wget -q "$img" -O "/tmp/img.$rand"
 if [ -s "/tmp/img.$rand" ]; then
 # If size is not nil, check magic and find GIF or JPEG-type image (case insensitive).
  file -bi "/tmp/img.$rand" 2>/dev/null|egrep -qie "^image/(gi|jp)"
  if [ "$?" = "0" ]; then
    loadImgRes=$(xloadimage -onroot "/tmp/img.$rand" 2>&1)
    # If xloadimage fails dump some info to stderr.
    if [ "$?" != "0" ]; then
      echo "${0##*/}: xloadimage failed with message \"${loadImgRes}\"."
      echo "${0##*/}: grabbed image was \"${img}\"."
      echo "${0##*/}: temp image stats:"
      stat "/tmp/img.$rand" 1>&2
      file "/tmp/img.$rand" 1>&2
    fi
  else
    # Issue warning for unsupported image
    echo "${0##*/}: grabbed image is unsupported by xloadimage." > /dev/stderr
  fi
 else
 # Add a warning for zero-sized images.
  echo "${0##*/}: grabbed image was empty." > /dev/stderr
 fi
 # Remove the temporary image regardless.
 rm -f "/tmp/img.$rand"
# End of outer loop.
done
exit 0

BTW, thanks the debug output and for helping make *your* application "better".

stardotstar 07-16-2006 12:22 AM

Wow, thanks heaps. I really appreciate your taking the time to work on this project for me :) I am following the code and it is very informative - one day I hope to be able to tackle stuff like this.

I am still running into the zero size get problem. The astropix site is a good reference because they have only one pic and it changes from gif to jpg regularly which is the situation on my intranet (mainly catalogue pics that could be jpeg straight from a digital camera or gifs needing transparency for inclusion in a website. etc.)

http://antwrp.gsfc.nasa.gov/apod/astropix.html
I am using that for reference whilst at home over weekend at at nights because the intranet is not available.

I find that I am still continually getting zero file sizes but when I point wget directly at the image link it works...

Code:

geko stardotstar # xloadimage -onroot gcenter_2mass_big.jpg
gcenter_2mass_big.jpg is a 620x1214 JPEG image, color space YCbCr, 3 comps, Huffman coding.
  Building XImage...done

works manually but when the backgrounder script is called:

Code:

geko stardotstar # wget http://antwrp.gsfc.nasa.gov/apod/image/0607/gcenter_2mass_big.jpg
--15:20:11--  http://antwrp.gsfc.nasa.gov/apod/image/0607/gcenter_2mass_big.jpg
          => `gcenter_2mass_big.jpg.1'
Resolving antwrp.gsfc.nasa.gov... 128.183.17.121
Connecting to antwrp.gsfc.nasa.gov|128.183.17.121|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 626,425 (612K) [image/jpeg]

100%[====================================>] 626,425      53.18K/s    ETA 00:00

15:20:24 (50.48 KB/s) - `gcenter_2mass_big.jpg.1' saved [626425/626425]

perhaps it is a timeout thing... wget the image works and then the root window allows it to be set.
Code:

geko stardotstar # sh -x /home/stardotstar/backgrounder.4 http://antwrp.gsfc.nasa.gov/apod/astropix.html
+ uri=http://antwrp.gsfc.nasa.gov/apod/astropix.html
+ '[' 1 '!=' 1 -o http:// '!=' http:// ']'
+ wget -O - http://antwrp.gsfc.nasa.gov/apod/astropix.html
+ 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 1
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo '<im'
+ grep -qie src
+ '[' 1 = 0 ']'
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo src
+ grep -qie src
+ '[' 0 = 0 ']'
++ dirname http://antwrp.gsfc.nasa.gov/apod/astropix.html
++ echo '"image/0607/gcenter_2mass.jpg"'
++ tr -d '"'
+ img=http://antwrp.gsfc.nasa.gov/apodimage/0607/gcenter_2mass.jpg
+ '[' http://ant = http:http: ']'
+ break
++ date
++ sha1sum
++ cut -c 1-26
+ rand=b0a9622f6ca5ad0e12610caa41
+ wget -q http://antwrp.gsfc.nasa.gov/apodimage/0607/gcenter_2mass.jpg -O /tmp/img.b0a9622f6ca5ad0e12610caa41
+ '[' -s /tmp/img.b0a9622f6ca5ad0e12610caa41 ']'
+ echo 'backgrounder.4: grabbed image was empty.'
backgrounder.4: grabbed image was empty.
+ rm -f /tmp/img.b0a9622f6ca5ad0e12610caa41
+ read l
+ exit 0

I am not at work now but the symptoms are the same as they were previously - so I havn;t bothered to turn off image cleanup.
BTW I have been prowling around sf and googling this type of solution and found chbg and such but they are not nearly as simple and convenient as this script.

Will

unSpawn 07-16-2006 04:34 AM

getImg.sh 1.4 (final)
 
I am still running into the zero size get problem.
That's because of how the img src links work, sometimes relative, sometimes full paths:
Code:

./getImg-v2.sh http://www.google.com/ncr
base: "http://www.google.com"
img: "/intl/en/images/logo.gif"
./getImg-v2.sh http://www.kernel.org
base: "http:"
img: "http://www1.kernel.org/bw-zeus1.png"
./getImg-v2.sh http://www.ibm.org
base: "http:"
img: "//www.ibm.com/i/v14/t/ibm-logo.gif"
./getImg-v2.sh http://antwrp.gsfc.nasa.gov/apod/astropix.html
base: "http://antwrp.gsfc.nasa.gov/apod"
img: "image/0607/gcenter_2mass.jpg"


I've corrected that and added some checks. After testing with your submitted URI's I consider this somewhat "final" but it still isn't optimised etc, etc. Please respect the License and retain comment headers when using/distributing.
Code:

#!/bin/bash
# getImg.sh 1.4 unspawn (www.linuxquestions.org) for stardotstar
# Purpose: grab web-based image from page (URI)
# License: GPLv2
# Args: 1: http://doma.in/HTML-rendered-page.ext
# Deps: Bash, GNU utils, wget, xloadimage
# Run from: manual or cron

# Checks
# 0. wget, xloadimage
for b in wget xloadimage; do
 which $b 2>&1>/dev/null; case "$?" in
  0) ;;
  *) echo "${0##*/}: $b not found or not in PATH, exiting"\
      > /dev/stderr; exit 127;;
 esac
done

# Whatever first parameter is supplied serves as URI to grab.
uri="$1"
# 2. If no arg given or protocol doesn't match that for teh intarweb we exit with a warning.
if [ "$#" != "1" -o "${uri:0:7}" != "http://" ]; then
 echo "${0##*/}: only one parameter needed: (URI + HTML rendered page: http://doma.in/file.ext), exiting." > /dev/stderr
 exit 1
fi
# Grab file and dump on stdout, make all lowercase, grep for HTML image tag, and only use first line.
# (not grep -m1), sed strips off tags before and after. Now dump the string in variable "l" and turn it into array.
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 each element in the array check if it matches the HTML image "src" tag,
 # first found match (break) gets stuffed in the "img" variable.
 for i in $(seq 0 $[${#l[@]}-1]); do
  echo "${l[$i]:0:3}"|grep -qie src
  if [ "$?" = "0" ]; then
    # Strip the src= part.
    #img="$(echo ${l[$i]:4}|tr -d "\"")"
    img="$(echo ${l[$i]:4}|tr -d "\"")"
    break
  fi
 done

 # Correct wrong approach:
 # 3. Get base
 base=$(dirname "$uri")
 # 4. Correct ifempty
 if [ "${#base}" = "5" -a "${base}" = "http:" ]; then
  base=EMPTY
  # 5. Follows whole URI is contained in img tag
  if [ "${img:0:2}" = "//" ]; then
  img="http://${img:2}"
  fi
 else
  img="${base}/${img}"
 fi
 # Minor checks
 # 6. Bail out if img is empty
 if [ "${#img}" -lt "18" -o -z "${img}" ]; then
  echo "${0##*/}: img URI too short or empty (\""${img}"\")." > /dev/stderr
  exit 1
 fi
 # Lame way to get a randomised string for temp file.
 rand=$(date|sha1sum|cut -c 1-26)
 # Grab image and output to temp file.
 wget -q "$img" -O "/tmp/img.$rand"
 if [ -s "/tmp/img.$rand" ]; then
 # 7. If size is not nil, check magic and find GIF or JPEG-type image (case insensitive).
  file -bi "/tmp/img.$rand" 2>/dev/null|egrep -qie "^image/(gi|jp)"
  if [ "$?" = "0" ]; then
    loadImgRes=$(xloadimage -onroot "/tmp/img.$rand" 2>&1)
    # 8. If xloadimage fails dump some info to stderr.
    if [ "$?" != "0" ]; then
      echo "${0##*/}: xloadimage failed with message \"${loadImgRes}\"."
      echo "${0##*/}: grabbed image was \"${img}\"."
      echo "${0##*/}: temp image stats:"
      stat "/tmp/img.$rand" 1>&2
      file "/tmp/img.$rand" 1>&2
    fi
  else
    # Issue warning for unsupported image
    echo "${0##*/}: grabbed image is unsupported by xloadimage." > /dev/stderr
  fi
 else
 # Add a warning for zero-sized images.
  echo "${0##*/}: grabbed image was empty." > /dev/stderr
 fi
 # Remove the temporary image regardless.
 rm -f "/tmp/img.$rand"
# End of outer loop.
done
exit 0


* If you (stardotstar) want to do something in return (though no pressure, this script is provided for free so if you can't / won't I understand), then post the name of a (GNU/Linux distro-agnostic!) background image loader that does handle PNG and other image formats. That's all. Have fun!

stardotstar 07-16-2006 06:34 AM

Of course I will respect the GPLv2 Licence and I am very appreciative of your efforts and insight into my particular problem. This, to me, is the very heart of the open source community spirit and I revere you for your skill and admire the willingness you exhibit in sharing it.

If I can find a programme which
Quote:

a (GNU/Linux distro-agnostic!) background image loader that does handle PNG and other image formats.
I will most certainly publish it here. Certainly your assistance has assisted me in making some substantial steps toward being able to tackle such things myself as an aspiring programmer.

Best Regards,
Will.*

stardotstar 07-20-2006 05:44 PM

This application is working a treat and I want first of all to thank you despawn for taking the time to develop it for me - and teaching me a new level in scripting. I am proud to say that this has led to my actually being able to demonstrate that I have *tried* to solve the current requirement I need to add to the script to finish it and make it match the windows clients...

The script currently loads the picture of the day perfectly (well I can't get it to work as well with multiple screens as Windows but that is way out of scope :lol: ) but the product or content is accompanied by a part number/name and description.

I am trying to get ImageMagick to print the name of the image on the picture and have so far got it to print static text but can't get it to write the ${img} - which I think is what it should be...

the relevant part is:

Code:

addTitleText=$(convert -fill white -draw 'text 100,100 "${img}"' /tmp/img.$rand /tmp/titled 2>&1)
        loadImgRes=$(xloadimage -onroot -center -border black -fullscreen "/tmp/titled" 2>&1)

All this does is write ${img} on the image in white at 100,100

I have also not been able to get convert to output over the top of the img.$rand or write *to* titled.$rand because of syntax problems (I must get a good tutorial on scripting). I also tried to echo the output of the file name to a text file with:

'text 100,100 ${img}'

appended and then call that in the script with convert using the @ modifier. to no avail...

I'm sure all this is just knowledge about how to insert variables in scripts with respect to "'\/ etc - makes my head spin :)

Ideally if I can strip the .jpg or .gif or whatever off the name and print it on the image it will be adequete - I will work out how to add the comments later.

Can I get some assistance with this please?

This is the current full listing (you will see that I have butchered some of it ;) by writing the daily randomised file name to a wallpaper directory so the screensaver uses it, and removal of the titled temp file as well as the img.$rand at the end.

Oh, and root tail since I can now use it with nautilus background management turned off :grin:

Finally, can we not just use convert/ImageMagick to handle any file types that are not compatible with xloadimage??? That would satisfy the request you made in your last post? (just a thought>)


Code:

#!/bin/bash
# getImg.sh 1.4 unspawn (www.linuxquestions.org) for stardotstar
# Purpose: grab web-based image from page (URI)
# License: GPLv2
# Args: 1: http://doma.in/HTML-rendered-page.ext
# Deps: Bash, GNU utils, wget, xloadimage
# Run from: manual or cron

# Checks
# 0. wget, xloadimage
for b in wget xloadimage; do
 which $b 2>&1>/dev/null; case "$?" in
  0) ;;
  *) echo "${0##*/}: $b not found or not in PATH, exiting"\
      > /dev/stderr; exit 127;;
 esac
done

# Set a pretty repeat pattern while we wait for the image to load.

xloadimage -onroot /home/stardotstar/wallpaper.png

# Whatever first parameter is supplied serves as URI to grab.
uri="$1"
# 2. If no arg given or protocol doesn't match that for teh intarweb we exit with a warning.
if [ "$#" != "1" -o "${uri:0:7}" != "http://" ]; then
 echo "${0##*/}: only one parameter needed: (URI + HTML rendered page: http://doma.in/file.ext), exiting." > /dev/stderr
 exit 1
fi
# Grab file and dump on stdout, make all lowercase, grep for HTML image tag, and only use first line.
# (not grep -m1), sed strips off tags before and after. Now dump the string in variable "l" and turn it into array.
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 each element in the array check if it matches the HTML image "src" tag,
 # first found match (break) gets stuffed in the "img" variable.
 for i in $(seq 0 $[${#l[@]}-1]); do
  echo "${l[$i]:0:3}"|grep -qie src
  if [ "$?" = "0" ]; then
    # Strip the src= part.
    #img="$(echo ${l[$i]:4}|tr -d "\"")"
    img="$(echo ${l[$i]:4}|tr -d "\"")"
    break
  fi
 done

 # Correct wrong approach:
 # 3. Get base
 base=$(dirname "$uri")
 # 4. Correct ifempty
 if [ "${#base}" = "5" -a "${base}" = "http:" ]; then
  base=EMPTY
  # 5. Follows whole URI is contained in img tag
  if [ "${img:0:2}" = "//" ]; then
  img="http://${img:2}"
  fi
 else
  img="${base}/${img}"
 fi
 # Minor checks
 # 6. Bail out if img is empty
 if [ "${#img}" -lt "18" -o -z "${img}" ]; then
  echo "${0##*/}: img URI too short or empty (\""${img}"\")." > /dev/stderr
  exit 1
 fi
 # Lame way to get a randomised string for temp file.
 rand=$(date|sha1sum|cut -c 1-26)
 # Grab image and output to temp file.
 wget -q "$img" -O "/tmp/img.$rand"
 if [ -s "/tmp/img.$rand" ]; then
 # 7. If size is not nil, check magic and find GIF or JPEG-type image (case insensitive).
  file -bi "/tmp/img.$rand" 2>/dev/null|egrep -qie "^image/(gi|jp)"
  if [ "$?" = "0" ]; then
#    loadImgRes=$(xloadimage -onroot -at 5,30 -border black "/tmp/img.$rand" 2>&1)
        addTitleText=$(convert -fill white -draw 'text 100,100 "${img}"' /tmp/img.$rand /tmp/titled 2>&1)
        loadImgRes=$(xloadimage -onroot -center -border black -fullscreen "/tmp/titled" 2>&1)
    # 8. If xloadimage fails dump some info to stderr.
    if [ "$?" != "0" ]; then
      echo "${0##*/}: xloadimage failed with message \"${loadImgRes}\"."
      echo "${0##*/}: grabbed image was \"${img}\"."
      echo "${0##*/}: temp image stats:"
      stat "/tmp/img.$rand" 1>&2
      file "/tmp/img.$rand" 1>&2
    fi
  else
    # Issue warning for unsupported image
    echo "${0##*/}: grabbed image is unsupported by xloadimage." > /dev/stderr
  fi
 else
 # Add a warning for zero-sized images.
  echo "${0##*/}: grabbed image was empty." > /dev/stderr
 fi
 # Remove the temporary image regardless.
 cp "/tmp/img.$rand" /home/wparker/AV/dailywallpaper/
 rm -f "/tmp/img.$rand"
 rm -f /tmp/titled
# End of outer loop.
done
# Call root-tail to finish screen setup
root-tail -fn -misc-fixed-*-*-*-*-10 --wordwrap -g 1000x150+5+610 /var/log/acpid,darkred /var/log/messages,purple &
exit 1


stardotstar 07-20-2006 06:16 PM

OK by hacking around and talking to the windows developer I have made some progress:

I reset img early to newimg and use that to title the image with convert:

Code:

    # Strip the src= part.
    #img="$(echo ${l[$i]:4}|tr -d "\"")"
    img="$(echo ${l[$i]:4}|tr -d "\"")"
    newimg="${img}"

...

addTitleText=$(convert -fill white -draw 'text 100,100 '${newimg} /tmp/img.$rand /tmp/titled 2>&1)

...so now the titling of the picture is "image/0607/pictureoday.jpg" - perhaps I should be grepping the short image description from the web page...

konsolebox 07-20-2006 09:11 PM

Code:

#!/bin/bash
# getImg.sh 1.4 unspawn (www.linuxquestions.org) for stardotstar
# Purpose: grab web-based image from page (URI)
# License: GPLv2
# Args: 1: http://doma.in/HTML-rendered-page.ext
# Deps: Bash, GNU utils, wget, xloadimage
# Run from: manual or cron

# konsolebox mod 1.4.1
# please tell me if you find bugs

# i suggest you use some variables
#XLOADIMAGE=/home/stardotstar/wallpaper.png
#DEBUG=1 OR novalue

# sends error message and exit
error() { echo "$@" >&2; exit 1; }
# you'll find this helpful when debugging
#debug() { [ "$DEBUG" ] && echo "$@" >&2; }

# Checks for wget and xloadimage
for b in wget xloadimage; do
 if ! which $b 2>&1>/dev/null; then
  error "$b not found or not in PATH"
 fi
done

# Set a pretty repeat pattern while we wait for the image to load.
xloadimage -onroot /home/stardotstar/wallpaper.png

# Whatever first parameter is supplied serves as URI to grab.
uri="$1"
# 2. If no arg given or protocol doesn't match that for teh intarweb we exit with a warning.
if [ "$#" != "1" -o "${uri:0:7}" != "http://" ]; then
 error "${0##*/}: only one parameter needed: (URI + HTML rendered page: http://doma.in/file.ext), exiting."
fi

# Grab file and dump on stdout and grep for HTML image tags and make HtTp: lowercase
declare -a imgs
declare -i imgcount=0
for a in $(wget -O - "$uri" 2>/dev/null | grep -oi "<img src=[^ ]*" | sed -e s/"<img src="// -e s/"http:\/\/"/"http:\/\/"/i); do
 imgs[$((++imgcount))]="$a"
done

if [ -z "${imgs[1]}" ]; then
 error "no image tag found from $uri"
fi

# Print the results
for ((a=1; a<=imgcount; a++)); do
 echo "[$a] ${imgs[a]}"
done

while read l; do
 # Pick the selected image
 img="${imgs[l]}"
 if [ -z "$img" ]; then
  error "invalid image number"
  continue
 fi
 
 # Correct wrong approach:
 if ! [ "${img:0:5}" = "http:" ]; then
  # 3. Get base
  base=$(dirname "$uri")
  # appends img to base with // cleared
  img="${base}/${img/*\/\/}"
 fi
 
 # Minor checks
 # 6. Bail out if img is empty
 if [ "${#img}" -lt "18" -o -z "${img}" ]; then
  error "${0##*/}: img URI too short or empty (\""${img}"\")."
 fi
 
 # Still a lame way to get a randomised string for temp file.
 # We can also do some checks like unused files if we like but not for now
 declare -i rand=0
 until ((rand++ )); [ ! -e "/tmp/img.${rand}" ]; do
  :
 done
 touch /tmp/img.$rand 2>&1 >/dev/null || error "we can't create a new temp file"
 
 # Grab image and output to temp file.
 wget -q "$img" -O "/tmp/img.$rand"
 
 # no modifications from here and not yet tested
 
 if [ -s "/tmp/img.$rand" ]; then
 # 7. If size is not nil, check magic and find GIF or JPEG-type image (case insensitive).
  file -bi "/tmp/img.$rand" 2>/dev/null | egrep -qie "^image/(gi|jp)"
  if [ "$?" = "0" ]; then
  # loadImgRes=$(xloadimage -onroot -at 5,30 -border black "/tmp/img.$rand" 2>&1)
  addTitleText=$(convert -fill white -draw 'text 100,100 "${img}"' /tmp/img.$rand /tmp/titled 2>&1)
  loadImgRes=$(xloadimage -onroot -center -border black -fullscreen "/tmp/titled" 2>&1)
  # 8. If xloadimage fails dump some info to stderr.
  if [ "$?" != "0" ]; then
    echo "${0##*/}: xloadimage failed with message \"${loadImgRes}\"."
    echo "${0##*/}: grabbed image was \"${img}\"."
    echo "${0##*/}: temp image stats:"
    stat "/tmp/img.$rand" 1>&2
    file "/tmp/img.$rand" 1>&2
  fi
  else
  # Issue warning for unsupported image
  echo "${0##*/}: grabbed image is unsupported by xloadimage." > /dev/stderr
  fi
 else
  # Add a warning for zero-sized images.
  echo "${0##*/}: grabbed image was empty." > /dev/stderr
 fi
 
 # Remove the temporary image regardless.
 cp "/tmp/img.$rand" /home/wparker/AV/dailywallpaper/
 rm -f "/tmp/img.$rand"
 
 rm -f /tmp/titled
 # End of outer loop.
done

# Call root-tail to finish screen setup
root-tail -fn -misc-fixed-*-*-*-*-10 --wordwrap -g 1000x150+5+610 /var/log/acpid,darkred /var/log/messages,purple &
exit 1


konsolebox 07-20-2006 09:13 PM

sorry i immediately pressed the submit button without even making an intro. i just made some modifications to the script above. hope you'll find it useful. nice script by the way unspawn.

stardotstar 07-20-2006 11:40 PM

Hi Konsolebox;

there seem to be some problems with this version:

Code:

geko stardotstar # chmod a+x /usr/bin/getImg.sh
geko stardotstar # getImg.sh http://antwrp.gsfc.nasa.gov/apod/astropix.html
/home/stardotstar/wallpaper.png is 14x14 PNG image, color type RGB, 8 bit
  Building XImage...done
[1] <IMG
[2] SRC="image/0607/PIA08576marsmeteorites45.jpg"
2
getImg.sh: grabbed image was empty.

invalid image number
geko stardotstar # getImg.sh http://antwrp.gsfc.nasa.gov/apod/astropix.html
/home/stardotstar/wallpaper.png is 14x14 PNG image, color type RGB, 8 bit
  Building XImage...done
[1] <IMG
[2] SRC="image/0607/PIA08576marsmeteorites45.jpg"
1
getImg.sh: grabbed image was empty.

invalid image number
geko stardotstar # gvim /usr/bin/getImg.sh
(vim:1296): GnomeUI-WARNING **: While connecting to session manager:
Authentication Rejected, reason : None of the authentication protocols specified are supported and host-based authentication failed.
geko stardotstar # getImg.sh http://antwrp.gsfc.nasa.gov/apod/astropix.html
/home/stardotstar/wallpaper.png is 14x14 PNG image, color type RGB, 8 bit
  Building XImage...done
[1] <IMG
[2] SRC="image/0607/PIA08576marsmeteorites45.jpg"

invalid image number
geko stardotstar # getImg.sh http://antwrp.gsfc.nasa.gov/apod/astropix.html
/home/stardotstar/wallpaper.png is 14x14 PNG image, color type RGB, 8 bit
  Building XImage...done
[1] <IMG
[2] SRC="image/0607/PIA08576marsmeteorites45.jpg"
2
getImg.sh: grabbed image was empty.

invalid image number

I am inexperienced and so can't put my finger on it but it always presents an incorrect image selection and then either gets a zero size file or reports an invalid image number.

Although this could be useful since there are applications that require selecting the correct image in this case I don't want any interactivity and the page I am grabbing from has only one image. (well it has been known to have mouseovers and the second one is desirable but that is out of scope for now :) )

Thank you for ocntributing I will continue to try and grok your additions and integrate them if I can ...

Further to the zero image size problem the jpg in the above instance is also downloading as zero size... and yet it can be grabbed happily from browser etc...

Code:

geko stardotstar # wget http://antwrp.gsfc.nasa.gov/apod/image/0607/PIA08576marsmeteorites45.jpg -O thisimage
--14:42:41--  http://antwrp.gsfc.nasa.gov/apod/image/0607/PIA08576marsmeteorites45.jpg
          => `thisimage'
Resolving antwrp.gsfc.nasa.gov... 128.183.17.121
Connecting to antwrp.gsfc.nasa.gov|128.183.17.121|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 199,174 (195K) [image/jpeg]

100%[====================================>] 199,174      53.22K/s    ETA 00:00

14:42:46 (47.33 KB/s) - `thisimage' saved [199174/199174]

geko stardotstar # xloadimage thisimage
thisimage is a 727x540 JPEG image, color space YCbCr, 3 comps, Huffman coding.
  Building XImage...done

I wonder if it is the // in this example:

Code:


geko stardotstar # sh -x getImg.sh http://www.google.com/ncr
+ for b in wget xloadimage
+ which wget
+ case "$?" in
+ for b in wget xloadimage
+ which xloadimage
+ case "$?" in
+ xloadimage -onroot /home/stardotstar/wallpaper.png
/home/stardotstar/wallpaper.png is 14x14 PNG image, color type RGB, 8 bit
  Building XImage...done
+ uri=http://www.google.com/ncr
+ '[' 1 '!=' 1 -o http:// '!=' http:// ']'
+ 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
+ '[' 1 = 0 ']'
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo src
+ grep -qie src
+ '[' 0 = 0 ']'
++ echo '"/intl/en/images/logo.gif"'
++ tr -d '"'
+ img=/intl/en/images/logo.gif
+ newimg=/intl/en/images/logo.gif
+ break
++ dirname http://www.google.com/ncr
+ base=http://www.google.com
+ '[' 21 = 5 -a http://www.google.com = http: ']'
+ img=http://www.google.com//intl/en/images/logo.gif
+ '[' 46 -lt 18 -o -z http://www.google.com//intl/en/images/logo.gif ']'
++ date
++ sha1sum
++ cut -c 1-26
+ rand=cb0894b63afc2d1d663abe40fa
+ wget -q http://www.google.com//intl/en/images/logo.gif -O /tmp/img.cb0894b63afc2d1d663abe40fa
+ '[' -s /tmp/img.cb0894b63afc2d1d663abe40fa ']'
+ echo 'getImg.sh: grabbed image was empty.'
getImg.sh: grabbed image was empty.
+ cp /tmp/img.cb0894b63afc2d1d663abe40fa /home/wparker/AV/dailywallpaper/
+ rm -f /tmp/img.cb0894b63afc2d1d663abe40fa
+ rm -f /tmp/titled
+ read l
+ root-tail -fn '-misc-fixed-*-*-*-*-10' --wordwrap -g 1000x150+5+610 /var/log/acpid,darkred /var/log/messages,purple
+ exit 0

again it seems to be related to relative and explicit paths...


Will

konsolebox 07-21-2006 12:10 AM

sorry about that. this should fix the problem now

Code:

#!/bin/bash
# getImg.sh 1.4 unspawn (www.linuxquestions.org) for stardotstar
# Purpose: grab web-based image from page (URI)
# License: GPLv2
# Args: 1: http://doma.in/HTML-rendered-page.ext
# Deps: Bash, GNU utils, wget, xloadimage
# Run from: manual or cron

# konsolebox mod 1.4.2
# please tell me if you find bugs

# i suggest you use some variables
#XLOADIMAGE=/home/stardotstar/wallpaper.png
#DEBUG=1 OR novalue

# sends error message and exit
error() { echo "$@" >&2; exit 1; }
# you'll find this helpful when debugging
#debug() { [ "$DEBUG" ] && echo "$@" >&2; }

# Checks for wget and xloadimage
for b in wget xloadimage; do
 if ! which $b 2>&1>/dev/null; then
  error "$b not found or not in PATH"
 fi
done

# Set a pretty repeat pattern while we wait for the image to load.
xloadimage -onroot /home/stardotstar/wallpaper.png

# Whatever first parameter is supplied serves as URI to grab.
uri="$1"
# 2. If no arg given or protocol doesn't match that for teh intarweb we exit with a warning.
if [ "$#" != "1" -o "${uri:0:7}" != "http://" ]; then
 error "${0##*/}: only one parameter needed: (URI + HTML rendered page: http://doma.in/file.ext), exiting."
fi

# Grab file and dump on stdout and grep for HTML image tags and make HtTp: lowercase
declare -a imgs
declare -i imgcount=0
for a in $(wget -O - "$uri" 2>/dev/null | grep -oi "<img src=[^ ]*" | sed -e s/"<img src="//i -e s/"http:\/\/"/"http:\/\/"/i); do
 eval "imgs[$((++imgcount))]=$a"
done

if [ -z "${imgs[1]}" ]; then
 error "no image tag found from $uri"
fi

# Print the results
for ((a=1; a<=imgcount; a++)); do
 echo "[$a] ${imgs[a]}"
done

while read l; do
 # Pick the selected image
 img="${imgs[l]}"
 if [ -z "$img" ]; then
  error "invalid image number"
  continue
 fi
 
 # Correct wrong approach:
 if ! [ "${img:0:5}" = "http:" ]; then
  # 3. Get base
  base=$(dirname "$uri")
  # appends img to base with // cleared
  img="${base}/${img/*\/\/}"
 fi
 
 # Minor checks
 # 6. Bail out if img is empty
 if [ "${#img}" -lt "18" -o -z "${img}" ]; then
  error "${0##*/}: img URI too short or empty (\""${img}"\")."
 fi
 
 # Still a lame way to get a randomised string for temp file.
 # We can also do some checks like unused files if we like but not for now
 declare -i rand=0
 until ((rand++ )); [ ! -e "/tmp/img.${rand}" ]; do
  :
 done
 touch /tmp/img.$rand 2>&1 >/dev/null || error "we can't create a new temp file"
 
 # Grab image and output to temp file.
 wget -q "$img" -O "/tmp/img.$rand"

 # no modifications from here and not yet tested
 
 if [ -s "/tmp/img.$rand" ]; then
 # 7. If size is not nil, check magic and find GIF or JPEG-type image (case insensitive).
  file -bi "/tmp/img.$rand" 2>/dev/null | egrep -qie "^image/(gi|jp)"
  if [ "$?" = "0" ]; then
  # loadImgRes=$(xloadimage -onroot -at 5,30 -border black "/tmp/img.$rand" 2>&1)
  addTitleText=$(convert -fill white -draw 'text 100,100 "${img}"' /tmp/img.$rand /tmp/titled 2>&1)
  loadImgRes=$(xloadimage -onroot -center -border black -fullscreen "/tmp/titled" 2>&1)
  # 8. If xloadimage fails dump some info to stderr.
  if [ "$?" != "0" ]; then
    echo "${0##*/}: xloadimage failed with message \"${loadImgRes}\"."
    echo "${0##*/}: grabbed image was \"${img}\"."
    echo "${0##*/}: temp image stats:"
    stat "/tmp/img.$rand" 1>&2
    file "/tmp/img.$rand" 1>&2
  fi
  else
  # Issue warning for unsupported image
  echo "${0##*/}: grabbed image is unsupported by xloadimage." > /dev/stderr
  fi
 else
  # Add a warning for zero-sized images.
  echo "${0##*/}: grabbed image was empty." > /dev/stderr
 fi
 
 # Remove the temporary image regardless.
 cp "/tmp/img.$rand" /home/wparker/AV/dailywallpaper/
 rm -f "/tmp/img.$rand"
 
 rm -f /tmp/titled
 # End of outer loop.
done

# Call root-tail to finish screen setup
root-tail -fn -misc-fixed-*-*-*-*-10 --wordwrap -g 1000x150+5+610 /var/log/acpid,darkred /var/log/messages,purple &
exit 1

regards :)

stardotstar 07-21-2006 12:12 AM

Ahhhhh, big problem we have been having here is that some of the files that are not making it down have mixed lower and upper case in file name...

so:

Code:

# Grab file and dump on stdout, make all lowercase, grep for HTML image tag, and only use first line.
# (not grep -m1), sed strips off tags before and after. Now dump the string in variable "l" and turn it into array.

#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})

# Here I allowed the file name to preserve its case...
wget -O - "$uri" 2>/dev/null|grep -ie "\<img.*src=" 2>/dev/null|head -1\
|sed -e "s/.*<img/<img/g" -e "s/><.*$/>/g"|while read l; do l=(${l})

and this worked fine for the image file that was complaining before.

:)

konsolebox 07-21-2006 12:29 AM

Good job there. :) Have you already tried my new mod? I've already tested the script and was able to download the image files properly. The reason why the script didn't work before is because of the uppercase IMG and SRC. And i have to use eval to reconstruct the statements removing the double quotes from being included in the variable's value.

But anyways. We already have two scripts here. So I hope you'll be successful building the rpm script. Best luck then. :)

Edit: batch script I mean

stardotstar 07-21-2006 01:41 AM

OK I am forging ahead with the title grabbing;

By modifying unspawn's greps and parsing I have managed to come up with a separate script that successfully looks for the first HTML <b> tag and head -1's that output from wget; then writes it to a text file which I try to strip the tags off...

Code:

#!/bin/bash

# Checks
# 0. wget, xloadimage
for b in wget xloadimage; do
 which $b 2>&1>/dev/null; case "$?" in
  0) ;;
  *) echo "${0##*/}: $b not found or not in PATH, exiting"\
      > /dev/stderr; exit 127;;
 esac
done

# Whatever first parameter is supplied serves as URI to grab.
uri="$1"
# 2. If no arg given or protocol doesn't match that for the intarweb we exit with a warning.
if [ "$#" != "1" -o "${uri:0:7}" != "http://" ]; then
 echo "${0##*/}: only one parameter needed: (URI + HTML rendered page: http://doma.in/file.ext), exiting." > /dev/stderr
 exit 1
fi


# Grab file and dump on stdout, make all lowercase, grep for HTML image tag, and only use first line.
# (not grep -m1), sed strips off tags before and after. Now dump the string in variable "l" and turn it into array.
#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})
wget -O - "$uri" 2>/dev/null|grep -ie "<b>" 2>/dev/null|head -1 > /tmp/title.txt
cat /tmp/title.txt
 # for each element in the array check if it matches the HTML image "src" tag,
 # first found match (break) gets stuffed in the "img" variable.
cat /tmp/title.txt|tr -d "<b>/" > /tmp/title.txt
cat /tmp/title.txt
    # Strip the src= part.
    #title="$(echo ${l[$i]:4}|tr -d "\"")"
    #title="$(echo ${l[$i]:4})"
    break
echo "$title" > /dev/stderr
exit 0

so what I get when I run it is *nearly* the title of my picture:

Code:

geko stardotstar # sh -x ./greptitle.sh http://antwrp.gsfc.nasa.gov/apod/astropix.html
+ for b in wget xloadimage
+ which wget
+ case "$?" in
+ for b in wget xloadimage
+ which xloadimage
+ case "$?" in
+ uri=http://antwrp.gsfc.nasa.gov/apod/astropix.html
+ '[' 1 '!=' 1 -o http:// '!=' http:// ']'
+ wget -O - http://antwrp.gsfc.nasa.gov/apod/astropix.html
+ grep -ie '<b>'
+ head -1
+ cat /tmp/title.txt
<b> Strangers on Mars </b> <br>
+ cat /tmp/title.txt
+ tr -d '<b>/'
+ cat /tmp/title.txt
 Strangers on Mars  r
+ break
+ echo ''

+ exit 0

You can see that the

<b> Strangers on Mars </b> <br>

almost gets properly formed - removing <b> and </b> <b > using tr (badly :lol: )
but I am left with the r which if I delete it ruins the text.

What I need is a way now of stripping leading white space and *all* the tags after the first <

I will keep hacking at it but I would appreciate some gentle nudges in the right direction.

Because our team formats the intranet page that displays the pic of the day (actually based on a very similar layout to that astronomy page - because it is so simple - image, title and description) I am using the astronomy page as a reference - because it can be tested naturally.

Will


All times are GMT -5. The time now is 04:48 AM.