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-21-2006 01:56 AM

OK I got my title in a text file with an evil use of sed:

Code:

cat /tmp/title.txt|tr -d "<b>/" > /tmp/title.txt
cat /tmp/title.txt|sed 's/^[ \t]*//;s/[ \t]*$//' > /tmp/title.txt
cat /tmp/title.txt|sed 's/r*$//' > /tmp/title.txt
cat /tmp/title.txt

now I will try to get it to work as the title in the image script...

konsolebox 07-21-2006 02:00 AM

hey you can try this one:

echo "<b> Strangers on Mars </b> <br" | grep -o "[[:alpha:]][ a-zA-Z]\+[[:alpha:]]"

Edit: or this one. this is the most perfect i can suggest
echo "<b> Strangers on Mars </b> <br" | sed -e s/".*<b> \{,1\}"//i -e s/" \{,1\}<\/b>.*"//i

stardotstar 07-21-2006 06:28 AM

Quote:

Originally Posted by konsolebox
hey you can try this one:

echo "<b> Strangers on Mars </b> <br" | grep -o "[[:alpha:]][ a-zA-Z]\+[[:alpha:]]"

Edit: or this one. this is the most perfect i can suggest
echo "<b> Strangers on Mars </b> <br" | sed -e s/".*<b> \{,1\}"//i -e s/" \{,1\}<\/b>.*"//i

Thank you konsolebox, I appreciate your help and will work with your solutions after I sleep:)

In the mean time I am really proud to display my (clunky and primitive) working script - I have hacked the original around and learnt heaps and managed to title the image when it is run against this page:

Code:

#!/bin/bash
# getImg.sh 1.4 unspawn (www.linuxquestions.org) for stardotstar
# expanded on by stardotstar 1.4.1 2getImg.sh
# 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 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})

# Here I retain case for filename
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})
 
 # 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 "\"")"
    newimg="${img}"
break
fi
done
# grep and write the title to a text file and then sed out the unwanted stuff
wget -O - "$uri" 2>/dev/null|grep -ie "<b>" 2>/dev/null|head -1 > /tmp/title.txt
cat /tmp/title.txt|tr -d "<b>/" > /tmp/title.txt
cat /tmp/title.txt|sed 's/^[ \t]*//;s/[ \t]*$//' > /tmp/title.txt
cat /tmp/title.txt|sed 's/r*$//' > /tmp/title.txt
cat /tmp/title.txt|sed 's/[ \t]*$//' > /tmp/title.txt
title=`< /tmp/title.txt`
# 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 -pointsize 24 -draw "text 300,20 '$title'" /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/titled" "/home/wparker/AV/dailywallpaper/img.$rand"
 rm -f "/tmp/img.$rand"
 rm -f /tmp/titled
 rm -f /tmp/title.txt
# 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

This works well, although doesn't have quite the smarts it will need to place the text properly etc - but at least all features work. I will apply all suggested optimisations over the next few days or so and truly appreciate all the help and suggestions. I have really been bitten by bashing - I must find a good book or source to teach me the fundamentals!

Will

konsolebox 07-21-2006 06:42 AM

you're welcome and thank you too :)

if you want to learn more about bash i suggest:

The Bash Beginner's Guide http://www.tldp.org/LDP/Bash-Beginne...tml/index.html

or the Advanced Bash-Scripting Guide
http://www.tldp.org/LDP/abs/html/

that should help you a lot

stardotstar 07-21-2006 11:25 PM

BTW Consolebox your sed and grep worked beautifully (I settled on the sed) - thankyou for the optimisation. I have also make the temp tile a variable and used your debug section.

Just got to work out more about convert to centre the text and get it all nice and tidy.

I have written date onto the title too - seemed good for a pic'o'day

Code:

# set the date of the picoday
date +%D > /tmp/date.txt
date=`< /tmp/date.txt`
rm -f /tmp/date.txt
...
        addTitleText=$(convert -fill white -pointsize 18 -draw "text 50 50 '$date $title'" /tmp/img.$rand /tmp/titled 2>&1)

Will

stardotstar 07-23-2006 04:30 AM

OK guys, for the sake of completeness and to show off how proud I am of my finished product and thank you both for your help here is the script.

It is still a total mess from the POV of anyone with experience in programming and I can see that but I am happy that the final result works well and reliably for me for my application.

Code:

geko dailywallpaper # cat /usr/bin/2getImg.sh
#!/bin/bash
# 2getImg.sh 2.0 stardotstar and unspawn (www.linuxquestions.org)
# adapted by stardotstar from an original script by unspawn getImg.sh 1.4.1
# Purpose: grab web-based image from page (URI) and title
# License: GPLv2
# Args: 1: http://doma.in/HTML-rendered-page.ext
# Deps: Bash, GNU utils, wget, xloadimage, convert
# Run from: manual or cron

# Checks
# 0. wget, xloadimage, convert
for b in convert 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

debug=1

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

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

# 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|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 "\"")"
    newimg="${img}"
break
fi
done

# grep and write the title to a text file and then sed out the unwanted stuff - this is really clunky - almost to the point of being pure evil ;)
wget -O - "$uri" 2>/dev/null|grep -ie "<b>" 2>/dev/null|head -1 > /tmp/title.txt
cat /tmp/title.txt|sed -e s/".*<b> \{,1\}"//i -e s/" \{,1\}<\/b>.*"//i > /tmp/title.txt
title=`< /tmp/title.txt`

# set the date of the picoday
date +%d/%m/%y > /tmp/date.txt
date=`< /tmp/date.txt`

# 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

# make a white canvas for the title block and append it to the grabbed pic
convert xc:white -resize 1x18! /tmp/blank.ppm 2>&1
convert -append /tmp/blank.ppm /tmp/img.$rand /tmp/intermediate_file.jpg 2>&1

# add the title text centered with the date, write as a jpg to maintain quality and xloadimage to root window
#addTitleText=$(convert -fill black -font Dragonwick-Regular -gravity "North Center" -pointsize 14 -draw "text 0 0 'Astronomy Picture of the day for $date \"$title\"'" /tmp/intermediate_file.jpg /tmp/titled.jpg 2>&1)
addTitleText=$(convert -fill black -font Verdana-Regular -gravity "North Center" -pointsize 12 -draw "text 0 0 'Astronomy Picture of the day for $date \"$title\"'" /tmp/intermediate_file.jpg /tmp/titled.jpg 2>&1)

loadImgRes=$(xloadimage -onroot -center -border black -fullscreen "/tmp/titled.jpg" 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 images and text files regardless but copy titled file to wallpaper directory for screensaver with date and title with whitespace and irregular characters removed.
 cat /tmp/title.txt|tr " :\/" "_" > /tmp/title.txt
 cat /tmp/date.txt|tr -d "/" > /tmp/date.txt
 title=`< /tmp/title.txt`
 date=`< /tmp/date.txt`
 name="$title-$date.jpg"
 cp "/tmp/titled.jpg" "/home/wparker/AV/wallpapers/dailywallpaper/$name"
 rm -f "/tmp/img.$rand"
 rm -f /tmp/titled
 rm -f /tmp/title.txt
 rm -f /tmp/date.txt
 rm -f /tmp/blank.ppm
 rm -f /tmp/intermediate.jpg
# End of outer loop.
done
exit 1

It does the titling using ImageMagick and so I have added convert to the required section and I have attempted to use variables where previously I had hard coded. I also did some renaming of the final files and copy it with a properly titled and dated name to another directory when the cleanup happens. I wanted a fixed name also because I will cron this job to run and if I am not on the network I don't want the program writing a new file each time it succeeds to the wallpapers directory. This way the name will always be the same - as long as the day is the same when the cron runs and the image changes etc...

Thanks for all your help - especially despawn - I am definitely on the way to knowing how to research my bash needs myself - this is what Linux Questions is all about.

PS UNSPAWN: I hope you don't mind my changing of the comments - I still attribute to you for the original script but I am so stoked on the outcome and the time I have spent getting it just right I wanted to claim the latest version for myself - I could use a short lecture on the ettiquette of this if you dont mind - because if I am out of line from an opensource POV I would like to know! :) Thanks again. I am indebted to you.
Also, I use convert to rewrite the file as a jpg and this seems not only to improve the quality of the display when using xloadimage but may also mean we can use other image formats not natively supported ??

Will

konsolebox 07-23-2006 04:42 AM

that's so nice to hear from you.
hey i wonder why unspawn's no longer making a reply. i hope he's not mad at me for modifying his code.

anyway congratulations dude. nice work there. :)

stardotstar 07-24-2006 04:09 AM

OK in an effort to get the desktop in Gnome/Nautilus back I dug around in the gconftool-2 and the graphic configuration editor.

So if anyone stumbles on this some time in the future and wants to run this as a native Gnome background I have added these lines instead of using xloadimage:

Code:

gconftool-2 --type string  --set /desktop/gnome/background/color_shading_type "vertical-gradient"
gconftool-2 --type string  --set /desktop/gnome/background/picture_filename /tmp/titled.jpg
gconftool-2 --type string  --set /desktop/gnome/background/picture_options "centered"
gconftool-2 --type string  --set /desktop/gnome/background/primary_color "#0B0685"
gconftool-2 --type string  --set /desktop/gnome/background/secondary_color "#000000"

this basically set the necessary keys in the gnome desktop wallpaper configuration and puts a vertial-gradient from black to dark blue.

I have run into a major show stopper at this point for three reasons:

1) gnome config editing seems to be more magic, smoke, mirrors and here's hoping (what with gnome settigns daemon restarting and erroring all the time on restarting sessions :roll: ) so I can't confirm that the keys are perfect - for example:
the configuration editor make the following comment about setting the picture options:
Quote:

Originally Posted by /desktop/gnome/background/picture_options 'long description'
Determines how the image set by wallpaper_filename is rendered. Possible values are "none", "wallpaper", "centered", "scaled", "stretched".

Notice that the comment refers to wallpaper_filename but the only valid existing key is picture_filename

Quote:

/desktop/gnome/background/picture_filename
File to use for the background image
anyway that may be a simple matter of interpretation but worth noting.

2) I found that the cron system can't open the display - so this script is not going to cut it when using xloadimage from a crontab call....

3) This appears to be fixed by using the gnome xml calls and punching the values in that way but for some reason I can't get it to work yet...

Code:

stardotstar@geko ~ $ cat /etc/crontab
# for vixie cron
#
# $Header: /var/cvsroot/gentoo-x86/sys-process/vixie-cron/files/crontab-3.0.1-r4,v 1.1 2005/03/04 23:59:48 ciaranm Exp $
#
#

# Global variables
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# check scripts in cron.hourly, cron.daily, cron.weekly and cron.monthly
0  *  * * *    root    rm -f /var/spool/cron/lastrun/cron.hourly
1  3  * * *    root    rm -f /var/spool/cron/lastrun/cron.daily
15 4  * * 6    root    rm -f /var/spool/cron/lastrun/cron.weekly
30 5  1 * *    root    rm -f /var/spool/cron/lastrun/cron.monthly
*/10  *  * * *  root    test -x /usr/sbin/run-crons && /usr/sbin/run-crons
4 15 * * *      root    /usr/bin/2getImg.sh http://antwrp.gsfc.nasa.gov/apod/astropix.html
27 17 * * *    stardotstar sh -x /usr/bin/2getImg.sh http://antwrp.gsfc.nasa.gov/apod/astropix.html > /var/log/getimg.log

this is still problematic when run as my user or as root. and it outputs nothing (or at best nothing useful)

Also, by writing things this way we are no longer in need of a random string - or it can be completely revised and better checks are needed to prevent wierd filenames being generated for the wallpaper directory (for use by the screensaver) so I am inclined to just get it running and then when I have more skills do a complete rewrite so that it works this way better.

I prefer to use the system this way - writing to the gnome desktop - because I can get my desktop icons and possibly call it from cron...

thoughts and contributions naturally appreciated but if this thread turns into a personal blog then that is just as cool :)

Will

stardotstar 07-24-2006 09:41 PM

OK here is the current Gnome Version - still messy but does a good job when incorporated with the cron call I have also copied:

Code:

stardotstar@geko ~ $ cat /usr/bin/2getImg.sh
#!/bin/bash
# 2getImg.sh 2.0 stardotstar and unspawn (www.linuxquestions.org)
# adapted by stardotstar from an original script by unspawn getImg.sh 1.4.1
# Purpose: grab web-based image from page (URI) and title
# License: GPLv2
# Args: 1: http://doma.in/HTML-rendered-page.ext
# Deps: Bash, GNU utils, wget, xloadimage, convert
# Run from: manual or cron

# Checks
# 0. wget, xloadimage, convert
for b in convert 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

debug=1

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

# Set a pretty repeat pattern while we wait for the image to load.
temptile=/home/stardotstar/wallpaper.png
#xloadimage -onroot $temptile
rm -f /tmp/titled.jpg
gconftool-2 --type string  --set /desktop/gnome/background/color_shading_type "solid"
gconftool-2 --type string  --set /desktop/gnome/background/picture_filename $temptile
gconftool-2 --type string  --set /desktop/gnome/background/picture_options "wallpaper"
gconftool-2 --type string  --set /desktop/gnome/background/primary_color "#000000"
gconftool-2 --type string  --set /desktop/gnome/background/secondary_color "#000000"

# 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|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 "\"")"
    newimg="${img}"
break
fi
done

# grep and write the title to a text file and then sed out the unwanted stuff - this is really clunky - almost to the point of being pure evil ;)
wget -O - "$uri" 2>/dev/null|grep -ie "<b>" 2>/dev/null|head -1 > /tmp/title.txt
cat /tmp/title.txt|sed -e s/".*<b> \{,1\}"//i -e s/" \{,1\}<\/b>.*"//i > /tmp/title.txt
title=`< /tmp/title.txt`

# set the date of the picoday
date +%d/%m/%y > /tmp/date.txt
date=`< /tmp/date.txt`

# 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

# make a white canvas for the title block and append it to the grabbed pic
convert xc:white -resize 1x18! /tmp/blank.ppm 2>&1
convert -append /tmp/blank.ppm /tmp/img.$rand /tmp/intermediate_file.jpg 2>&1

# add the title text centered with the date, write as a jpg to maintain quality and xloadimage to root window
#addTitleText=$(convert -fill black -font Dragonwick-Regular -gravity "North Center" -pointsize 14 -draw "text 0 0 'Astronomy Picture of the day for $date \"$title\"'" /tmp/intermediate_file.jpg /tmp/titled.jpg 2>&1)
addTitleText=$(convert -fill black -font Verdana-Regular -gravity "North Center" -pointsize 12 -draw "text 0 0 'Astronomy Picture of the day for $date \"$title\"'" /tmp/intermediate_file.jpg /tmp/titled.jpg 2>&1)

#loadImgRes=$(xloadimage -onroot -center -border black "/tmp/titled.jpg" 2>&1)
#gconftool-2 --type string  --set /desktop/gnome/background/wallpaper_filename /tmp/titled.jpg
gconftool-2 --type string  --set /desktop/gnome/background/color_shading_type "vertical-gradient"
gconftool-2 --type string  --set /desktop/gnome/background/picture_filename /tmp/titled.jpg
gconftool-2 --type string  --set /desktop/gnome/background/picture_options "centered"
gconftool-2 --type string  --set /desktop/gnome/background/primary_color "#0B0685"
gconftool-2 --type string  --set /desktop/gnome/background/secondary_color "#000000"


# 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 images and text files regardless but copy titled file to wallpaper directory for screensaver with date and title with whitespace and irregular characters removed.
 cat /tmp/title.txt|tr " :\/" "_" > /tmp/title.txt
 cat /tmp/date.txt|tr -d "/" > /tmp/date.txt
 title=`< /tmp/title.txt`
 date=`< /tmp/date.txt`
 name="$title-$date.jpg"
 cp "/tmp/titled.jpg" "/home/wparker/AV/wallpapers/dailywallpaper/$name"
 rm -f "/tmp/img.$rand"
 #rm -f /tmp/titled.jpg
 rm -f /tmp/title.txt
 rm -f /tmp/date.txt
 rm -f /tmp/blank.ppm
 rm -f /tmp/intermediate.jpg
# End of outer loop.
 done
exit 1

yes there is lots of redundant stuff in there now but it does work and as long as it is called from cron as your gnome user it should automatically update the background without intervention. This method also supports all the nautilus icons and desktop behaviours like transparency in terminals etc

Code:

39 22 * * *    stardotstar    sh -x /usr/bin/2getImg.sh http://antwrp.gsfc.nasa.gov/apod/astropix.html > /tmp/getimg.log
:)

stardotstar 07-25-2006 07:11 AM

Yesterday and today's pics on the astronomy site demonstrated that the image on the main page is not going to cut it and I set about grabbing the larger image. I also sorted out the problem with ' in the titles and subsequent script lines and file names.

At consolebox's suggestion I made many things variables - though imperfect for sure.

I couldn't get my if then else loop to work with a flag set for big=0/1 so that one could specify if you want the big image or the one from the main page... I have some work and research to do :)

anyway it works pretty well and as a cron job too so here is the current and relatively finished gnome version:

Code:

cat /usr/X11R6/bin/2getImg.sh
#!/bin/bash
# 2getImg.sh 2.0 stardotstar and unspawn (www.linuxquestions.org)
# adapted by stardotstar from an original script by unspawn getImg.sh 1.4.1
# Purpose: grab web-based image from page (URI) and title
# License: GPLv2
# Args: 1: http://doma.in/HTML-rendered-page.ext
# Deps: Bash, GNU utils, wget, xloadimage, convert
# Run from: manual or cron

# Checks
# 0. wget, xloadimage, convert
for b in convert wget; 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 some variables:

debug=1
big=1 # if true the program will grab the larger version of the image otherwise only the one off the page itself
ibcst="solid" # initial background color shading type
ibpf=/home/stardotstar/wallpaper.png # tile to load as temp background
ibpo="wallpaper" # initial temp background image style/option
ibpc="#000000" # initial background primary color
ibsc="#000000" # initial background secondary color - I am using black but it would be useful to change these if we want a gradient and centered image that says please wait or something
tmpdir=/tmp # specify the temporary directory

bcst="vertical-gradient" # background color shading type
bpo="scaled" # background image style/option
bpc="#0B0685" # background primary color
bsc="#000000" # background secondary color - I am using black but it would be useful to change these if we want a gradient and centered image that says please wait or something
slideshwdir=/home/stardotstar/AV/wallpapers/dailywallpaper # specify the output directory for the renamed copies of the images

# remove yesterday's temporary image
rm -f $tmpdir/apod.jpg

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

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

gconftool-2 --type string  --set /desktop/gnome/background/color_shading_type "$ibcst"
gconftool-2 --type string  --set /desktop/gnome/background/picture_filename $ibpf
gconftool-2 --type string  --set /desktop/gnome/background/picture_options "$ibpo"
gconftool-2 --type string  --set /desktop/gnome/background/primary_color "$ibpc"gconftool-2 --type string  --set /desktop/gnome/background/secondary_color "$ibsc"

# 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, 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.
# the following wget is to grab the image directly off the page - which is set with big=0

#if $big=0; then
#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})
# 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 href= part.
    #img="$(echo ${l[$i]:4}|tr -d "\"")"
#    img="$(echo ${l[$i]:5}|tr -d "\"")"
#    newimg="${img}"
#    echo $newimg > /tmp/img2get.txt
#break
#fi
#  done
#else
#if $big=1; then
wget -O - "$uri" 2>/dev/null|grep -ie "\<a.*href=\"image" 2>/dev/null|head -1 |sed -e "s/.*<a/<a/g" -e "s/>.*$//g"|while read l; do l=(${l})

 # for each element in the array check if it matches the HTML image "href" tag,
 # first found match (break) gets stuffed in the "img" variable.
 for i in $(seq 0 $[${#l[@]}-1]); do
  echo "${l[$i]:0:4}"|grep -qie href
  if [ "$?" = "0" ]; then
    # Strip the href= part.
    #img="$(echo ${l[$i]:4}|tr -d "\"")"
    img="$(echo ${l[$i]:5}|tr -d "\"")"
    newimg="${img}"
    echo $newimg > /tmp/img2get.txt
break
fi
done
# grep and write the title to a text file and then sed out the unwanted stuff - contributed by konsolebox and update by stardotstar to handle ' ;)
wget -O - "$uri" 2>/dev/null|grep -ie "<b>" 2>/dev/null|head -1 > /tmp/title.txtcat /tmp/title.txt|sed -e s/".*<b> \{,1\}"//i -e s/" \{,1\}<\/b>.*"//i -e s/"'"/"\\\'"/i > $tmpdir/title.txt
title=`< $tmpdir/title.txt`
cp $tmpdir/title.txt $tmpdir/titletext.txt
# set the date of the picoday
date +%d/%m/%y > /tmp/date.txt
date=`< $tmpdir/date.txt`

# 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}"\")." 2>&1
  exit 1
 fi

# Grab image and output to temp file.
 wget -q "$img" -O "$tmpdir/apod_orig"
 if [ -s "$tmpdir/apod_orig" ]; then

# 7. If size is not nil, check magic and find GIF or JPEG-type image (case insensitive).
  file -bi "$tmpdir/apod_orig" 2>/dev/null|egrep -qie "^image/(gi|jp)"
  if [ "$?" = "0" ]; then

# make a white canvas for the title block and append it to the grabbed pic
convert xc:white -resize 1x18! $tmpdir/canvas.ppm 2>&1
convert -append $tmpdir/canvas.ppm $tmpdir/apod_orig $tmpdir/intermediate_file.jpg 2>&1

# add the title text centered with the date, write as a jpg to maintain quality

addTitleText=$(convert -fill black -font Verdana-Regular -gravity "North Center" -pointsize 12 -draw "text 0 0 'Astronomy Picture of the day for $date \"$title\"'" $tmpdir/intermediate_file.jpg $tmpdir/apod.jpg 2>&1)

# Poke the necessary stuff into the gnomeconftool-2

gconftool-2 --type string  --set /desktop/gnome/background/color_shading_type "$bcst"
gconftool-2 --type string  --set /desktop/gnome/background/picture_filename $tmpdir/apod.jpg
gconftool-2 --type string  --set /desktop/gnome/background/picture_options "$bpo"
gconftool-2 --type string  --set /desktop/gnome/background/primary_color "$bpc"
gconftool-2 --type string  --set /desktop/gnome/background/secondary_color "$bsc"

# Issue warning for unsupported image
    echo "${0##*/}: grabbed image is unsupported." >> /tmp/2getImg.sh
  fi
 else

# Add a warning for zero-sized images.
  echo "${0##*/}: grabbed image was empty." >> /tmp/2getImg.log
 fi

# Remove the temporary images and text files regardless but copy titled file to wallpaper directory for screensaver with date and title with whitespace and irregular characters removed.
 cat $tmpdir/title.txt|tr " :\/" "_" > $tmpdir/title.txt
 cat $tmpdir/date.txt|tr -d "/" > $tmpdir/date.txt
 title=`< $tmpdir/title.txt`
 date=`< $tmpdir/date.txt`
 name="$title-$date.jpg"
 cp "$tmpdir/apod.jpg" "$slideshwdir/$name"
 rm -f "$tmpdir/apod_orig"
 rm -f $tmpdir/title.txt
 rm -f $tmpdir/date.txt
 rm -f $tmpdir/blank.ppm
 rm -f $tmpdir/intermediate.jpg
# End of outer loop.
done
exit 1

I have removed the random string and image names in favor of variables and the script tidying up after itself as well as possible. I am sure there will still be problems with error catching, filetypes and filenames but I will continue to work on it as a learning project.

Will

stardotstar 09-10-2006 02:37 AM

Need to raise the dead on this one:

I am finding that the script fails to grab the title from the page reliably. I can't fathom why but mostly I find that the file fails to get a title (and subsequently save with the correct file name) but I always get the daa te and other text. Something must be going on in the sed of the title section.


I don't know what it is - often it works second time around.

Can someone please have a quick look at this statement and suggest why:
Code:


# grep and write the title to a text file and then sed out the unwanted stuff - contributed by konsolebox and update by stardotstar to handle ' ;)
wget -O - "$uri" 2>/dev/null|grep -ie "<b>" 2>/dev/null|head -1 > $tmpdir/title.txt
cat $tmpdir/title.txt|sed -e s/".*<b> \{,1\}"//i -e s/" \{,1\}<\/b>.*"//i -e s/":"/"\:"/i -e s/"\""/"\\\""/i -e s/"'"/"\\\'"/i > $tmpdir/title.txt
title=''
title=`< $tmpdir/title.txt`
cat $tmpdir/title.txt
cp $tmpdir/title.txt $tmpdir/titletext.txt
cat $tmpdir/titletxt.txt
# set the date of the picoday
date +%d/%m/%y > /tmp/date.txt
date=`< $tmpdir/date.txt`

this is the debugging output:

Code:

wget -O - http://antwrp.gsfc.nasa.gov/apod/astropix.html
+ grep -ie '<b>'
+ head -1
+ cat /tmp/title.txt
+ sed -e 's/.*<b> \{,1\}//i' -e 's/ \{,1\}<\/b>.*//i' -e 's/:/\:/i' -e 's/"/\"/i' -e 's/'\''/\\'\''/i'
+ title=
+ title=
+ cat /tmp/title.txt
+ cp /tmp/title.txt /tmp/titletext.txt
+ cat /tmp/titletxt.txt
cat: /tmp/titletxt.txt: No such file or directory
+ date +%d/%m/%y
+ date=10/09/06

The whole script and entire debug output:

Code:

stardotstar@spitfire /tmp $ cat /usr/local/bin/2getImg.sh
#!/bin/bash
# 2getImg.sh 2.0 stardotstar and unspawn (www.linuxquestions.org)
# adapted by stardotstar from an original script by unspawn getImg.sh 1.4.1
# Purpose: grab web-based image from page (URI) and title
# License: GPLv2
# Args: 1: http://doma.in/HTML-rendered-page.ext
# Deps: Bash, GNU utils, wget, xloadimage, convert
# Run from: manual or cron

# Checks
# 0. wget, xloadimage, convert
for b in convert wget; 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 some variables:

#debug=1
big=1 # if true the program will grab the larger version of the image otherwise only the one off the page itself
ibcst="solid" # initial background color shading type
ibpf=/usr/local/share/images/wallpaper.png # tile to load as temp background
ibpo="wallpaper" # initial temp background image style/option
ibpc="#000000" # initial background primary color
ibsc="#000000" # initial background secondary color - I am using black but it would be useful to change these if we want a gradient and centered image that says please wait or something
tmpdir=/tmp # specify the temporary directory

bcst="vertical-gradient" # background color shading type
bpo="scaled" # background image style/option
bpc="#0B0685" # background primary color
bsc="#000000" # background secondary color - I am using black but it would be useful to change these if we want a gradient and centered image that says please wait or something
slideshwdir=/home/stardotstar/AV/Images/wallpapers/dailywallpaper # specify the output directory for the renamed copies of the images

# remove yesterday's temporary image
rm -f $tmpdir/apod.jpg

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

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

gconftool-2 --type string  --set /desktop/gnome/background/color_shading_type "$ibcst"
gconftool-2 --type string  --set /desktop/gnome/background/picture_filename $ibpf
gconftool-2 --type string  --set /desktop/gnome/background/picture_options "$ibpo"
gconftool-2 --type string  --set /desktop/gnome/background/primary_color "$ibpc"
gconftool-2 --type string  --set /desktop/gnome/background/secondary_color "$ibsc"

# 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, 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.
# the following wget is to grab the image directly off the page - which is set with big=0

#if $big=0; then
#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})
# 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 href= part.
    #img="$(echo ${l[$i]:4}|tr -d "\"")"
#    img="$(echo ${l[$i]:5}|tr -d "\"")"
#    newimg="${img}"
#    echo $newimg > /tmp/img2get.txt
#break
#fi
#  done
#else
#if $big=1; then
wget -O - "$uri" 2>/dev/null|grep -ie "\<a.*href=\"image" 2>/dev/null|head -1 |sed -e "s/.*<a/<a/g" -e "s/>.*$//g"|while read l; do l=(${l})

 # for each element in the array check if it matches the HTML image "href" tag, # first found match (break) gets stuffed in the "img" variable.
 for i in $(seq 0 $[${#l[@]}-1]); do
  echo "${l[$i]:0:4}"|grep -qie href
  if [ "$?" = "0" ]; then
    # Strip the href= part.
    #img="$(echo ${l[$i]:4}|tr -d "\"")"
    img="$(echo ${l[$i]:5}|tr -d "\"")"
    newimg="${img}"
    echo $newimg > $tmpdir/img2get.txt
break
fi
done
# grep and write the title to a text file and then sed out the unwanted stuff - contributed by konsolebox and update by stardotstar to handle ' ;)
wget -O - "$uri" 2>/dev/null|grep -ie "<b>" 2>/dev/null|head -1 > $tmpdir/title.txt
cat $tmpdir/title.txt|sed -e s/".*<b> \{,1\}"//i -e s/" \{,1\}<\/b>.*"//i -e s/":"/"\:"/i -e s/"\""/"\\\""/i -e s/"'"/"\\\'"/i > $tmpdir/title.txt
title=''
title=`< $tmpdir/title.txt`
cat $tmpdir/title.txt
cp $tmpdir/title.txt $tmpdir/titletext.txt
cat $tmpdir/titletxt.txt
# set the date of the picoday
date +%d/%m/%y > /tmp/date.txt
date=`< $tmpdir/date.txt`

# 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}"\")." 2>&1
  exit 1
 fi

# Grab image and output to temp file.
 wget -q "$img" -O "$tmpdir/apod_orig"
 if [ -s "$tmpdir/apod_orig" ]; then

# 7. If size is not nil, check magic and find GIF or JPEG-type image (case insensitive).
  file -bi "$tmpdir/apod_orig" 2>/dev/null|egrep -qie "^image/(gi|jp)"
  if [ "$?" = "0" ]; then

# make a white canvas for the title block and append it to the grabbed pic
convert xc:white -resize 1x18! $tmpdir/canvas.ppm 2>&1
convert -append $tmpdir/canvas.ppm $tmpdir/apod_orig $tmpdir/intermediate_file.jpg 2>&1

# add the title text centered with the date, write as a jpg to maintain quality
addTitleText=$(convert -fill black -font Verdana-Regular -gravity "North Center" -pointsize 12 -draw "text 0 0 'Astronomy Picture of the day for $date \"$title\"'" $tmpdir/intermediate_file.jpg $tmpdir/apod.jpg 2>&1)

# Poke the necessary stuff into the gnomeconftool-2

gconftool-2 --type string  --set /desktop/gnome/background/color_shading_type "$bcst"
gconftool-2 --type string  --set /desktop/gnome/background/picture_filename $tmpdir/apod.jpg
gconftool-2 --type string  --set /desktop/gnome/background/picture_options "$bpo"
gconftool-2 --type string  --set /desktop/gnome/background/primary_color "$bpc"gconftool-2 --type string  --set /desktop/gnome/background/secondary_color "$bsc"

# Issue warning for unsupported image
    echo "${0##*/}: grabbed image is unsupported." >> /tmp/2getImg.sh
  fi
 else

# Add a warning for zero-sized images.
  echo "${0##*/}: grabbed image was empty." >> /tmp/2getImg.log
 fi

# Remove the temporary images and text files regardless but copy titled file to wallpaper directory for screensaver with date and title with whitespace and irregular characters removed.
 cat $tmpdir/title.txt|tr " \/" "_" > $tmpdir/title.txt
 cat $tmpdir/date.txt|tr -d "/" > $tmpdir/date.txt
 title=`< $tmpdir/title.txt`
 echo $title
 date=`< $tmpdir/date.txt`
 name="$title-$date.jpg"
 cp "$tmpdir/apod.jpg" "$slideshwdir/$name"
 rm -f "$tmpdir/apod_orig"a
 rm -f $tmpdir/img2get.txt
 rm -f $tmpdir/title.txt
 rm -f $tmpdir/date.txt
 rm -f $tmpdir/blank.ppm
 rm -f $tmpdir/intermediate.jpg
# End of outer loop.
done
exit 1
stardotstar@spitfire /tmp $


stardotstar@spitfire /tmp $ getdaily
+ for b in convert wget
+ which convert
+ case "$?" in
+ for b in convert wget
+ which wget
+ case "$?" in
+ big=1
+ ibcst=solid
+ ibpf=/usr/local/share/images/wallpaper.png
+ ibpo=wallpaper
+ ibpc='#000000'
+ ibsc='#000000'
+ tmpdir=/tmp
+ bcst=vertical-gradient
+ bpo=scaled
+ bpc='#0B0685'
+ bsc='#000000'
+ slideshwdir=/home/stardotstar/AV/Images/wallpapers/dailywallpaper
+ rm -f /tmp/apod.jpg
+ gconftool-2 --type string --set /desktop/gnome/background/color_shading_type s olid
+ gconftool-2 --type string --set /desktop/gnome/background/picture_filename /usr/local/share/images/wallpaper.png
+ gconftool-2 --type string --set /desktop/gnome/background/picture_options wallpaper
+ gconftool-2 --type string --set /desktop/gnome/background/primary_color '#000000'
+ gconftool-2 --type string --set /desktop/gnome/background/secondary_color '#000000'
+ 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 '\<a.*href="image'
+ head -1
+ sed -e 's/.*<a/<a/g' -e 's/>.*$//g'
+ read l
+ l=(${l})
++ seq 0 1
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo '<a'
+ grep -qie href
+ '[' 1 = 0 ']'
+ for i in '$(seq 0 $[${#l[@]}-1])'
+ echo href
+ grep -qie href
+ '[' 0 = 0 ']'
++ echo '"image/0609/m46m47_hetlage_big.jpg"'
++ tr -d '"'
+ img=image/0609/m46m47_hetlage_big.jpg
+ newimg=image/0609/m46m47_hetlage_big.jpg
+ echo image/0609/m46m47_hetlage_big.jpg
+ break
+ wget -O - http://antwrp.gsfc.nasa.gov/apod/astropix.html
+ grep -ie '<b>'
+ head -1
+ cat /tmp/title.txt
+ sed -e 's/.*<b> \{,1\}//i' -e 's/ \{,1\}<\/b>.*//i' -e 's/:/\:/i' -e 's/"/\"/i' -e 's/'\''/\\'\''/i'
+ title=
+ title=
+ cat /tmp/title.txt
+ cp /tmp/title.txt /tmp/titletext.txt
+ cat /tmp/titletxt.txt
cat: /tmp/titletxt.txt: No such file or directory
+ date +%d/%m/%y
+ date=10/09/06
++ dirname http://antwrp.gsfc.nasa.gov/apod/astropix.html
+ base=http://antwrp.gsfc.nasa.gov/apod
+ '[' 32 = 5 -a http://antwrp.gsfc.nasa.gov/apod = http: ']'
+ img=http://antwrp.gsfc.nasa.gov/apod/image/0609/m46m47_hetlage_big.jpg
+ '[' 66 -lt 18 -o -z http://antwrp.gsfc.nasa.gov/apod/image/0609/m46m47_hetlage_big.jpg ']'
+ wget -q http://antwrp.gsfc.nasa.gov/apod/image/0609/m46m47_hetlage_big.jpg -O/tmp/apod_orig
+ '[' -s /tmp/apod_orig ']'
+ file -bi /tmp/apod_orig
+ egrep -qie '^image/(gi|jp)'
+ '[' 0 = 0 ']'
+ convert xc:white -resize '1x18!' /tmp/canvas.ppm
+ convert -append /tmp/canvas.ppm /tmp/apod_orig /tmp/intermediate_file.jpg
++ convert -fill black -font Verdana-Regular -gravity 'North Center' -pointsize12 -draw 'text 0 0 '\''Astronomy Picture of the day for 10/09/06 ""'\''' /tmp/intermediate_file.jpg /tmp/apod.jpg
+ addTitleText=
+ gconftool-2 --type string --set /desktop/gnome/background/color_shading_type v ertical-gradient
+ gconftool-2 --type string --set /desktop/gnome/background/picture_filename /tmp/apod.jpg
+ gconftool-2 --type string --set /desktop/gnome/background/picture_options scaled
+ gconftool-2 --type string --set /desktop/gnome/background/primary_color '#0B0685'
+ gconftool-2 --type string --set /desktop/gnome/background/secondary_color '#000000'
+ echo '2getImg.sh: grabbed image is unsupported.'
+ cat /tmp/title.txt
+ tr ' \/' _
+ cat /tmp/date.txt
+ tr -d /
+ title=
+ echo

+ date=100906
+ name=-100906.jpg
+ cp /tmp/apod.jpg /home/stardotstar/AV/Images/wallpapers/dailywallpaper/-100906.jpg
+ rm -f /tmp/apod_origa
+ rm -f /tmp/img2get.txt
+ rm -f /tmp/title.txt
+ rm -f /tmp/date.txt
+ rm -f /tmp/blank.ppm
+ rm -f /tmp/intermediate.jpg
+ read l
+ exit 1

My underztanding of sed is very basic and I can't really see why the get imge section is working fine but the title - which often works when the script is run second time round fails - interestingly - on this notebook the title never if ever works but on my other it is generally only second time around and not when called by cron...

Hmmm.
Will

ghostdog74 09-10-2006 06:31 AM

hi
that's a good effort in bash. However there are modules out there that makes parsing html pages easy, eg in Perl, there are modules like LWP, or in Python, you can use urllib2 and BeautifulSoup module. eg a snippet

>>> import urllib2
>>> from BeautifulSoup import BeautifulSoup
>>> info = urllib2.urlopen("http://antwrp.gsfc.nasa.gov/apod")
>>> htmlsource = info.read()
>>> soup = BeautifulSoup(htmlsource)
>>> soup.head.title
<title>Astronomy Picture of the Day
</title>
>>> soup.findAll('img')
[<img src="image/0609/m46m47_hetlage.jpg" alt="See Explanation. Clicking on the picture will download the highest resolution version available." />]

For examples on Perl LWP you can see here
http://www.perl.com/pub/a/2002/08/20/perlandlwp.html

just a suggestion for your future web projects..;)

unSpawn 09-11-2006 11:36 AM

Need to raise the dead on this one
Nice to see you expand stuff, though it does need a bit of tending to, like what you're doing here:
Code:

# grep and write the title to a text file and then sed out the unwanted stuff - contributed by konsolebox and update by stardotstar to handle ' ;)
# 0. grab string to file
wget -O - "$uri" 2>/dev/null|grep -ie "<b>" 2>/dev/null|head -1 > $tmpdir/title.txt
# 1. cat file, string ops and write to *same* file. That won't work.
cat $tmpdir/title.txt|sed -e s/".*<b> \{,1\}"//i -e s/" \{,1\}<\/b>.*"//i -e s/":"/"\:"/i -e s/"\""/"\\\""/i -e s/"'"/"\\\'"/i > $tmpdir/title.txt
# 2. which gives empty title.
title=''
title=`< $tmpdir/title.txt`

while you could:
Code:

# 0. grab string to file
wget -O - "$uri" 2>/dev/null|grep -ie "<b>" 2>/dev/null|head -1 > $tmpdir/title.tmp
# 1. string ops and write to new file.
sed -e s/".*<b> \{,1\}"//i -e s/" \{,1\}<\/b>.*"//i -e s/":"/"\:"/i -e s/"\""/"\\\""/i -e s/"'"/"\\\'"/i \
$tmpdir/title.tmp > $tmpdir/title.txt

...or:
Code:

# 0. grab string, string ops and write to file
wget -O - "$uri" 2>/dev/null|grep -ie "<b>" 2>/dev/null|head -1\
|sed -e s/".*<b> \{,1\}"//i -e s/" \{,1\}<\/b>.*"//i -e s/":"/"\:"/i -e s/"\""/"\\\""/i -e s/"'"/"\\\'"/i \
> $tmpdir/title.txt

...or even "better":
Code:

# 0. Grab string to array:
title=($(wget -O - "$uri" 2>/dev/null|grep -ie "<b>" 2>/dev/null|head -1))
# 1. Check and expect at least three elements:
if [ "${#title[@]}" -gt "3" ]; then
 # 2. Ooh, tis is lame! Walk through all elements in array, checking if the first
 # char is a leftside caret. If not, echo, so filling the array again
 title=$(n=${#array[@]}; c=0; until [ "$c" = "$n" ]; do [ \
 "${array[$c]:0:1}" != "<" ] && echo "${array[$c]}"; ((c++)); done)
else
        title="empty title"
fi

...and if that doesn't do it then it appears you're going to learn Perl or Python? ;-p

stardotstar 09-11-2006 10:52 PM

Perfect UnSpawn ! Thanks heaps. Titles are coming in consistently and file names are cleaning up great!

Will


All times are GMT -5. The time now is 02:00 AM.