bbqwall

Submitted scripts and programs
Forum rules
Your own work only.
machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

bbqwall

Unread post by machinebacon » Thu Aug 01, 2013 3:42 am

Fetches a list that contains filenames and presents the list to the user. The selected file is downloaded and set as wallpaper via pcmanfm.
Depends on yad, wget, pcmanfm, [optional: ping]

Code: Select all

#!/bin/bash
RESTART="`readlink -f "$0"`"
TESTSERVER="linuxbbq.org"
SERVER="http://linuxbbq.org/wallpapers/"
BBQLIST="http://linuxbbq.org/wallpapers/wallpaperlist.txt"
TERMSET="x-terminal-emulator -e "
## gui information ##
TITLE="--always-print-result --dialog-sep --image=/usr/share/icons/bbqtux2.png --title="
TITLETEXT="LinuxBBQ Wallpaper Downloader"
VER="1.0"
TEXT="Please select a wallpaper to download"

## test download server ##
   PINGTEST=`ping -c 1 "$TESTSERVER" 2>&1 | grep unknown `
   if [[ ! "$PINGTEST" = "" ]]; then 
      yad $TITLE"$TITLETEXT" --no-buttons --timeout=10 --text=" Either your Internet connection is not working or the server is down. Sorry." &
      exit 1
   fi

 if [[ ! -f "/tmp/wallpaperlist.txt" ]]; then
    cd /tmp
    wget -c "$BBQLIST"
 fi


GUI=$( cat "/tmp/wallpaperlist.txt" | rev | cut -d '.' -f2- |rev |
yad --list $TITLE"$TITLETEXT" --column="$TEXT" --geometry="620x420" --button="gtk-quit:1" --button="gtk-ok:0")
  result=$?
  [[ $result = 1 ]] && echo "Exiting" && exit

  case $result in

1)
   echo "cancel pressed exiting"
   exit  
;;

0)
## download selection gui result ##
   GETBBQ=` echo $GUI | cut -d '|' -f 1 `
     echo "Downloading..."
   yad $TITLE"$TITLETEXT" --no-buttons --text=" Please wait, attempting $GETBBQ download ....... " &
   YPID=$!

   
   DOWNBBQ="$SERVER$GETBBQ.jpg"
    cd /home/$USER/Images && wget -c $DOWNBBQ #"$DOWNMD5"
    kill $YPID &&  yad $TITLE"$TITLETEXT" --no-buttons --timeout=2 --text=" $GETBBQ has been downloaded to $HOME/Images directory!" &
	pcmanfm --wallpaper-mode=stretch -w /home/$USER/Images/$GETBBQ.jpg
   sleep 2s && $RESTART && exit
;;   
esac
..gnutella..

User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Re: bbqwall

Unread post by xaos52 » Sat Sep 07, 2013 5:29 pm

@mb: I have created an entirely new 'bbqwall' script, not because yours was bad, just for fun and flexing my bash scripting skills, especially the use of arrays in bash.

Plus, I want to give John an occasion to get back at me, after criticizing his 'beebotron' script :razz:

You can find the new version here: my github repos.

If something does not work as expected, or if you have any suggestions, don't hesitate to tell me.

This version is for a 'bloated install', needing wget, yad and pcmanfm installed.

It is my intention to make the same script work on less bloated systems. Suggestions for lighter replacements of those commands are welcome.

Oh, and congrats for keeping this up for a year now.

BTW: if you have other scripts that need some work, give me a call.
Connected. Take this REPL, brother, and may it serve you well.

User avatar
johnraff
Sperminator
Posts: 199
Joined: Wed Oct 17, 2012 6:38 pm
Location: Japan
Contact:

Re: bbqwall

Unread post by johnraff » Sat Sep 07, 2013 6:26 pm

Hi mb and xaos - all this juicy code, just as it's coming up to 2:30am and I have to crash... I'll be away the next two days too, so xaos, you'll have to wait a couple of days for any scathing criticism! At first glace, my only comment so far is - at 380 lines it looks a bit long for a relatively simple job?

Maybe I'll add a third version, just to be obnoxious. Image
All code is one.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: bbqwall

Unread post by machinebacon » Sat Sep 07, 2013 7:14 pm

@doctor: thank you for your offer. I'll take a look at what you created there ;) Of course it'd be perfect if yad => dialog/whiptail, pcmanfm => cat/echo and -- well, wget is lite enough :) The reason for using yad is that it's shiny (and customizable) enough for GUI but not too heavy around the waist. Of course I prefer console (who doesn't?) :) I'll get back to you!

@raffles: I'd love to see your approach ;)
..gnutella..

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: bbqwall

Unread post by DebianJoe » Sat Sep 07, 2013 7:32 pm

For full-blown GUI environments, I may finally get around to starting a wallpaper browsing tool with previews before you choose the ones you want. I've been putting it off because I don't enjoy working with GTK+/X-toolsets very much, but with the amount of requirements to run the above...it doesn't make much sense to leave it as a bash script.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
johnraff
Sperminator
Posts: 199
Joined: Wed Oct 17, 2012 6:38 pm
Location: Japan
Contact:

Re: bbqwall

Unread post by johnraff » Wed Sep 11, 2013 2:23 pm

OK a third version. As Joe points out, some preview of the wallpaper before downloading it seems like a good idea, which means having some thumbnail sized versions available. For testing purposes I've put up a bunch of files with width resized to 150px at http://www.rafflesnagoya.com/test/bbq/w ... thumbnails with names with added -t's like debjoe1-t.png.
The first time the script is run it has to download a set of thumbnails, but after that it just adds and removes so as to match the list from the bbq server. feh is used to generate a spread of thumbnails - click the one you want and then press enter.

I've stolen imported some code from Dr Xaos' amazing work. Sorry Doc. I couldn't find anything to criticize...

All I can say for this one is that it's small, has dependencies of wget & feh only, and has that thumbnail menu. A lot of things could be tweaked if it were to be used at all (maybe in more minimalistic mixes?), such as the file naming convention, directory paths etc.

Anyway, fwiw:

Code: Select all

#!/bin/bash
## bbqwall-feh - using feh to display thumbnails
## Refresh local thumbnail dir from list on server.
## Choose a thumbnail, download the full image if necessary
## and set as wallpaper.
## (Click a thumbnail to select, then press "Enter".)
## Adjust settings below as needed.
## Dependencies: wget feh
## and whatever is needed to set wallpaper

usage='Usage: bbqwall-feh
No options are available.
(--select is an internal option for fetching the selected wallpaper.)
Select available wallpapers from the linuxBBQ server for download
and install the requested wallpaper.
Choose by clicking the desired thumbnail image,
then press the "Enter" key.'

# set your directories for wallpapers & thumbnails
wallpaper_dir="$HOME/wallpapers/bbq"
thumbnail_dir="$HOME/wallpapers/bbq/thumbnails"

# set server urls
server_wallpaper_dir=http://linuxbbq.org/wallpapers
server_thumbnail_dir=http://www.rafflesnagoya.com/test/bbq/wallpapers/thumbnails
server_wallpaper_list=http://linuxbbq.org/wallpapers/wallpaperlist.txt

# width of feh display (pixels)
display_width=1050
# width of feh thumbnails
thumb_width=100

# relation of thumbnail name to full file name
# change this code as needed
thumbnail_suffix='-t'
filename_to_thumbnail() {
    thumb="${1%.*}$thumbnail_suffix.${1##*.}"
    printf '%s' "$thumb"
}
thumbnail_to_filename() {
    file="${1%$thumbnail_suffix.*}.${1##*.}"
    printf '%s' "$file"
}

# edit this function to suit your system
set_wallpaper() {
    [[ $(feh -U "$1") ]] || error_exit "$1 is probably not an image file"
   pcmanfm --wallpaper-mode=stretch -w "$1"
}

# amend as necessary - see set_wallpaper() above
required_commands='wget feh pcmanfm'
###########################################################################

error_exit() {
    echo "$0 error: $1" >&2
    exit 1
}

missing_commands=
for i in $required_commands
do
    hash $i || missing_commands+=" $i"
done
[[ $missing_commands ]] && error_exit "This script requires the following commands: $missing_commands
Please install the packages containing the missing commands
and rerun the script."

mkdir -p "$wallpaper_dir" || error_exit "unable to make $wallpaper_dir"
mkdir -p "$thumbnail_dir" || error_exit "unable to make $thumbnail_dir"

declare -A list
get_list() {
    for i in $( wget --quiet $server_wallpaper_list -O- || error_exit 'Either your Internet connection is not working or the server is down. Sorry.' )
    do
        list["$i"]=1
    done
}

update_thumbnails() {
    # remove old thumbnails not on list
    local filename
    for i in "$thumbnail_dir"/*
    do
        [[ -f $i ]] || continue
        filename="${i##*/}"
        filename="$( thumbnail_to_filename $filename )"
        [[ ${list["$filename"]-0} = 1 ]] || {
            echo "removing old thumbnail $i"
            rm "$i"
        }
    done
    # refresh thumbnails
    for i in ${!list[@]}
    do
        thumbnail="$( filename_to_thumbnail $i )"
        [[ -f $thumbnail_dir/$thumbnail ]] || {
            echo "downloading $thumbnail"
            wget --quiet "$server_thumbnail_dir/$thumbnail" -O "$thumbnail_dir/$thumbnail" || { echo "$0: WARNING unable to download thumbnail: $thumbnail" >&2; }
        }
    done
}

self_path=$( readlink -f "$0" )

case $1 in
'--select')
;;
'')
    # wgetting list substitutes for ping test
    get_list
    update_thumbnails
    exec feh --thumbnail --quiet --sort name --limit-width $display_width --thumb-width $thumb_width --thumb-title '>ENTER' --title 'Choose a wallpaper (click, then press enter)' --action "$self_path --select %n" "$thumbnail_dir"
    exit
;;
*)
    echo "$usage"
    exit
;;
esac

#######################################

# if "--select" option passed, download image (if necessary) and set as wallpaper
[[ $2 ]] || error_exit 'no wallpaper file defined'
filename="$( thumbnail_to_filename $2 )"

[[ -f $wallpaper_dir/$filename ]] || {
    get_list
    [[ ${list["$filename"]-0} = 1 ]] || error_exit "$filename is not available on server"
    echo "downloading $filename..."
    wget --quiet "$server_wallpaper_dir/$filename" -O "$wallpaper_dir/$filename"|| error_exit "unable to download wallpaper: $filename"
}
echo "setting $filename as wallpaper"
set_wallpaper "$wallpaper_dir/$filename"

pkill -f "feh --thumbnail .* $thumbnail_dir"
exit
edit 130915: The code has been cleaned up a little bit. The functionality is the same.
Attachments
bbwall-feh.sh.tar.gz
(1.7 KiB) Downloaded 131 times
Last edited by johnraff on Sat Sep 14, 2013 6:09 pm, edited 2 times in total.
All code is one.

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: bbqwall

Unread post by rhowaldt » Wed Sep 11, 2013 3:15 pm

debjoe1-t.png
i want a DebianJoe wallpaper. that would rock.

oh yeah, great work all of you! :D
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: bbqwall

Unread post by machinebacon » Wed Sep 11, 2013 3:36 pm

superb, thank you Raffles!

I'll create the thumbnails when I'm back from the trip and pack this one into the repos, and the one of the Doctor too.

Thanks to xaos for your contribution, too!
..gnutella..

User avatar
johnraff
Sperminator
Posts: 199
Joined: Wed Oct 17, 2012 6:38 pm
Location: Japan
Contact:

Re: bbqwall

Unread post by johnraff » Wed Sep 11, 2013 4:18 pm

oops, sorry I left the set_wallpaper function at the test setting of

Code: Select all

set_wallpaper() {
    [[ $(feh -U "$1") ]] || error_exit "$1 is probably not an image file"
    notify-send "WALLPAPER" "setting $1 as wallpaper"
}
Of course that would have to be changed to

Code: Select all

set_wallpaper() {
    [[ $(feh -U "$1") ]] || error_exit "$1 is probably not an image file"
    pcmanfm --wallpaper-mode=stretch -w "$1"
}
or something suitable for the system. EDIT 130915: now corrected.

feh itself is able to set wallpapers btw - it's got quite an impressive list of options.

@machinebacon re thumbnails - do you think they're better with a modified name as above, or since they'd be in a different directory, maybe they'd be OK with identical names? (It would allow some messy code to be taken out of the script.)

Oh yes, something else I forgot to mention - the server wallpaperlist.txt file leaves out some of the images that are up there. Is that deliberate? If not, maybe a php auto-generated list? Of course, that would mean auto-maintaining the thumbnails too - not impossible I'd guess, like if every request for the list triggered a refreshing of the server thumbnail directory?
Last edited by johnraff on Sat Sep 14, 2013 6:11 pm, edited 2 times in total.
All code is one.

User avatar
johnraff
Sperminator
Posts: 199
Joined: Wed Oct 17, 2012 6:38 pm
Location: Japan
Contact:

Re: bbqwall

Unread post by johnraff » Wed Sep 11, 2013 4:31 pm

rhowaldt wrote:
debjoe1-t.png
i want a DebianJoe wallpaper. that would rock.
It's up there - go and get it!
All code is one.

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: bbqwall

Unread post by DebianJoe » Wed Sep 11, 2013 5:25 pm

Very neat John. I applaud your work on this (also to Dr. Xaos's contributions.)
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
johnraff
Sperminator
Posts: 199
Joined: Wed Oct 17, 2012 6:38 pm
Location: Japan
Contact:

Re: bbqwall

Unread post by johnraff » Wed Sep 11, 2013 6:43 pm

^thank you Joe.

@xaos I did find this little thing at line 352:

Code: Select all

tempfile || {
    $dialog "$title_options" --title="$titletext" --no-buttons --timeout=2 --text="Could not create temp file."
    return 1
}
As it's in the main script, that 'return 1' should be 'exit 1' maybe? (The whole same snippet appears in the function select_wallpapers, on line 238.)

Oh, tiny point, the "function" special word is optional when defining a function, and it seems leaving it out ( functionname() { ... } ) might be more portable and Posixly Correct?
All code is one.

User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Re: bbqwall

Unread post by xaos52 » Wed Sep 11, 2013 7:16 pm

Right on both points, John.

I replaced that snippet from a function to the main script. Should be exit 1 indeed.

I have no problem in admitting that your solution is more practical than mine. Good job. Well done.

On the other hand, mine allows downloading of several walls in one go, and limiting the selection to what is not installed already.
As I said before, an exercise in handling ( associative and normal) arrays.
Connected. Take this REPL, brother, and may it serve you well.

User avatar
johnraff
Sperminator
Posts: 199
Joined: Wed Oct 17, 2012 6:38 pm
Location: Japan
Contact:

Re: bbqwall

Unread post by johnraff » Thu Sep 12, 2013 6:18 am

^The arrival of associative arrays in bash was good news, making a lot of things easier. Image
All code is one.

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: bbqwall

Unread post by rhowaldt » Thu Sep 12, 2013 7:38 am

@john: ah i found it, but sadly the debianjoe wall wasn't what i was hoping for... alas!
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Re: bbqwall

Unread post by xaos52 » Thu Sep 12, 2013 8:19 am

@john

Add 'notify-send' to the dependencies. I did not have it installed after a debian testing install.
the process just hangs there, never reaching the pkill. Needs Ctrl+C to end it.

You seem to prefer using strings over arrays.
For the best results and the least headaches, remember that if you have a list of things, you should always put it in an array.
From: this page
Connected. Take this REPL, brother, and may it serve you well.

User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: bbqwall

Unread post by wuxmedia » Thu Sep 12, 2013 10:20 am

excellent work - bashers - this was on my wish list for a while now.
Thanks
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
johnraff
Sperminator
Posts: 199
Joined: Wed Oct 17, 2012 6:38 pm
Location: Japan
Contact:

Re: bbqwall

Unread post by johnraff » Thu Sep 12, 2013 11:02 am

xaos52 wrote:@john

Add 'notify-send' to the dependencies. I did not have it installed after a debian testing install.
the process just hangs there, never reaching the pkill. Needs Ctrl+C to end it.
Sorry, my mistake - notify-send shouldn't be there at all. I was just using it for testing because I didn't want to install pcmanfm or interfere with my current wallpaper method. (See my post above.) The set_wallpaper function on line 36 needs to be adjusted to suit your system's needs.
You seem to prefer using strings over arrays.
For the best results and the least headaches, remember that if you have a list of things, you should always put it in an array.
From: this page
Yes I that's true about arrays, and I enjoy using them. However, if you're sure that all the things in your list will be single words then I see nothing wrong with putting them in a string, separated by spaces. Using an array in every single case might sometimes seem like overkill. My missing commands check takes up 6 lines of code to do pretty much the same as your array version in 16 lines. I guess it also partly depends on how mission-critical the task you're performing is.
All code is one.

User avatar
johnraff
Sperminator
Posts: 199
Joined: Wed Oct 17, 2012 6:38 pm
Location: Japan
Contact:

Re: bbqwall

Unread post by johnraff » Sat Sep 14, 2013 7:13 pm

The code has been updated a little bit - maybe a bit easier to read, using an array (OK xaos you win!) to hold the file list so there's no longer any need to call grep, and put a couple of things in functions - stuff like that. It should still work the same.

@xaos52 Thank you for that link. I thought I had read everything on Greg's Wiki, but missed that excellent Bash Guide - maybe it's a recent addition? Anyway a great page to suggest to bash starters.

I kept the missing command test as space-separated words in a string (commands - like urls - never have spaces) but decided to put the filenames from the bbq server list into an associative array, as the indexes.

Code: Select all

for i in $( wget -q http://linuxbbq.org/wallpapers/wallpaperlist.txt -O- ); do list["$i"]=1; done
so you can easily test if a file is in the list or not without using grep

Code: Select all

[[ ${list["$filename"]} = 1 ]] && echo "found"
While redoing this I spent some more time looking at your script, and have a couple of questions, if that's OK:

1) You have a very elaborate system in place to handle tempfiles - why don't you just store the file list and wget log in variables for the duration of the script?

2) You use the bash builtin mapfile to create arrays in several places. What's the advantage in eg line 169 of doing 'mapfile -t all_walls < <(sort "${wallpaper_list_file}")' instead of 'all_walls=( $(sort "${wallpaper_list_file}"))' ?

3) On line 174 you use find to go deep into the Images directory tree. If that was my image collection (luckily it's "images" not "Images") it would make a very long list! Better to make a special "wallpaper" directory?

4) On line 218 ' < <(echo "$gui_selected_walls")' could be '<<<"$gui_selected_walls"' I think, saving a call to echo?

5) On line 293 'selections_for_active[${#selections_for_active[@]}]="$wall"' could be 'selections_for_active+="$wall"' ?

Best wishes Image
All code is one.

User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Re: bbqwall

Unread post by xaos52 » Sun Sep 15, 2013 10:29 am

1) You have a very elaborate system in place to handle tempfiles - why don't you just store the file list and wget log in variables for the duration of the script?
I picked that snippet from a configure script.
There is nothing wrong with borrowong snippets from the masters in the business, and I am of the opinion that if you can read a configure script and figure out exactly what it is doing, then you are not a bash beginner any more.

I will come back to your other questions later, when I have more time.

Best wishes -to you too, and may you enjoy your scripting for many more years to come :)
Connected. Take this REPL, brother, and may it serve you well.

Post Reply