Friday, March 5, 2010

Use GKrellKam to displays random web thumbnails on desktop

We are going to use google search to select a category of random images to display through gkrellkam and set the selected image as the wallpaper

INSTALL:
apt-get install gkrellm gkrellkam curl gconf2 hindent gpicview imagemagick

CONFIGURATION:
1. Copy both scripts to opt  as /opt/google_images.sh, /opt/local_images.sh make them executable ( chmod +x ) and download bellow image and save it as /opt/no_image.png. This image will show when there is a problem retrieving the images.

2. Start gkrellm and gkrellkam and right-click on the plugin... Configuration window of gkrellkam will occur.

3. Set the 'Image source' as following:
[path to google_images.sh] [search+string]::[site]
spaces of the search string should be combined with '+' sign
eg: /opt/google_images.sh "land::nationalgeographic.com"

4. In 'Options' tab set 'Path to image viewer program' similar to 'Image source' above. You can copy and paste it
[[path to google_images.sh] [search+string]::[site]

5. both [search+string] and [site] can not be null at the same time but other all combinations work but '::' separator should be there.
[search+string]::[site]
::[site]
[search+string]::
5. Thats it..

USING MOUSE KEYS:

middle-mouse-button :
Will cache the image thumb locations at first use and then it will go to next image before timer expires when you click

left-mouse-button :
Will download the image in real size and set as the desktop wallpaper if it is downloadable

right-mouse-button :
Will open the configuration dialog
google_images.sh
#!/bin/bash
# Copyright (C) 2008 Nayanajit Mahendra Laxaman mail: nmlaxaman@gmail.com








# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.



if $(gconftool-2 -g /system/http_proxy/use_http_proxy)
then 
   export http_proxy="http://$(gconftool-2 -g /system/http_proxy/host):3128"
else
   unset http_proxy
   unset https_proxy
fi

function debug(){
   DEBUG=false

   if $DEBUG
   then
      echo "DEBUG:$@"
   fi
}

PARAMS=$1
#Custom url
#PARAMS="::http://ftp.gnome.org/pub/GNOME/teams/art.gnome.org/backgrounds/"
#PARAMS="wallpaper+road::"

WORKING='/tmp'
CATEGORY='';
SITE=''
NO_IMAGE="/opt/myapps/no_image.png"

if [[ $PARAMS != '' ]]
then
   CATEGORY=$( echo $PARAMS | awk -F '::' '{print $1}' )
   SITE=$( echo $PARAMS | awk -F '::' '{print $2}' )
   debug $SITE
fi

#if not accessible to google local images
ping -c 1 google.com 2>/dev/null >/dev/null
if [[ $? != 0 ]]
then
   /opt/myapps/local_images.sh $CATEGORY $2
   exit 0;
fi



DOWNLOAD_DIR="$WORKING/gkrellmkam_imgs_google";

CURRENT="$WORKING/next_img_google"
CATEGORY_D=""
if [[  $CATEGORY == "" ]]
then
   CATEGORY_D="DEFAULT"
else
   CATEGORY_D=$CATEGORY
fi
CURRENT_IMAGE_URL="$WORKING/$CATEGORY_D-current_image_url_google"
IMAGE_LIST="$WORKING/$CATEGORY_D-$SITE-img_list_google"
LOG="$WORKING/gkrellmkam_imgs_google_log"
COPY_DIR="$HOME/Pictures/google/$CATEGORY_D";

function urand(){
COUNT=$(($1+1))
while true
do
   FOUND=false
   RAND=$(($RANDOM%($COUNT)))
   for NO in $( cat $TMP 2>/dev/null )
   do
      if [[ $NO == $RAND ]]
      then
         FOUND=true
      fi
   done

   if ! $FOUND
   then
      echo $RAND
      echo $RAND >> $TMP
      break
   fi
done
}

function rand_file(){
TMP="/tmp/tn"
SC="/tmp/sc"
rm $TMP $SC 2>/dev/null
FILE=$1

COUNT=$( wc -l $FILE | awk '{print $1}' )
cat $FILE | while read line
do
   echo "LINE_ARRAY[$(urand $COUNT)]=\"$line\"" >> $SC
done 

source $SC
rm $FILE
for (( i=0; i<=${#LINE_ARRAY[@]}; i++ ))
do
   LINE=${LINE_ARRAY[$i]}
   if [[ $( echo $LINE | grep . ) != '' ]]
   then
      echo ${LINE_ARRAY[$i]} >> $FILE
   fi
done
}

function log(){
echo  $1 >> $LOG
}

function set_bg(){
XSPLASH_BG='/usr/share/images/xsplash/bg_2560x1600.jpg'
GDM_BG='/usr/share/backgrounds/Naranja.jpg'
if [[ $( file --mime-type "$@" | grep "image/" ) != '' ]]
then
   gpicview  "$@"&
   sleep 6
   zenity --question --text "Would you like to set it as the wallpaper, gdm background and, xsplash background?"  --cancel-label=NO;
   if [[ $? == 0 ]]
   then
      #Set user wallpaper
      gconftool-2 --type string --set /desktop/gnome/background/picture_filename "$@"
      if [[ -e $XSPLASH_BG ]] 
      then
         if [[ $( file --mime-type "$@" | grep jpeg ) != '' ]]
         then
            #Set xsplash background
            gksudo cp "$@" $XSPLASH_BG
            #gksudo cp "$@" $GDM_BG
         elif [[ -e /usr/bin/convert ]]
         then
            #Set xsplash background
            gksudo convert "$@" $XSPLASH_BG
            #gksudo convert "$@" $GDM_BG
         else
            zenity --error  --text="Can not set xsplash background and gdm background!"
         fi
      fi
      #Set gdm background
      sudo -u gdm gconftool-2 --type string --set /desktop/gnome/background/picture_filename "$@"
   fi
else
   zenity --error  --text="Image can not download\!"
fi
}


if [[ $# == 2 ]]
then

  if [[ ! -e "$COPY_DIR" ]]
  then
    mkdir -p  $COPY_DIR
  fi

  URL=$( cat $CURRENT_IMAGE_URL | awk -F 'http://' '{print "http://"$3 }')
  cd $DOWNLOAD_DIR
  FILE_O="$( basename $URL )"
  FILE="$(md5sum < $FILE_O | awk '{print$1}')-$FILE_O"
  if [[ -e "$FILE" ]]
  then
    if [[ -e "$COPY_DIR/$FILE_O" ]]
    then
      cp "$FILE" "$COPY_DIR/$(date +%S)-$FILE_O"
      $( set_bg "$COPY_DIR/$(date +%S)-$FILE_O" )
    else
      cp "$FILE" "$COPY_DIR/$FILE_O"
      $( set_bg "$COPY_DIR/$FILE_O" )
    fi
    exit 0;
  else
    wget -c  "$URL" -O "$FILE" 2>/dev/null
    if [[ -e  "$FILE" ]]
    then
      if [[ -e "$COPY_DIR/$FILE_O" ]]
      then
      cp "$FILE" "$COPY_DIR/$(date +%S)-$FILE_O"
       $( set_bg "$COPY_DIR/$(date +%S)-$FILE_O" )
      else
       cp "$FILE" "$COPY_DIR/$FILE_O"
       $( set_bg "$COPY_DIR/$FILE_O" )
      fi
    else
      gpicview  "$( basename $URL )" &
      $( set_bg "$( basename $URL )" )
    fi
  fi
  exit 0
fi



function gen_list(){
TMP_LIST="/tmp/tmp_list";
IMG_SIZE="xga" #2mp,
SAFE="active" #off
echo "" > $TMP_LIST
for start in 0 21 42 63 84 105
do
   debug "curl -A \"Mozilla/4.0\" \"http://images.google.com/images?q=$CATEGORY&imgsz=$IMG_SIZE&safe=$SAFE&start=$start&ndsp=21&as_sitesearch=$SITE\" | hindent -s 2>/dev/null| grep '/dev/null | hindent -s 2>/dev/null| grep '> $TMP_LIST 
done

if [[  $( cat  $TMP_LIST | wc -l ) != 0 ]]
then
  cp $TMP_LIST $IMAGE_LIST
  rand_file $IMAGE_LIST
  echo 1 > $CURRENT
fi
}

touch  $IMAGE_LIST  $CURRENT 2>/dev/null
mkdir  $DOWNLOAD_DIR 2>/dev/null

IMG_NO=$( cat  $CURRENT )
NO_OF_IMGS=$( cat $IMAGE_LIST | wc -l )

if [[ $NO_OF_IMGS == $IMG_NO || $NO_OF_IMGS == 0 ]]
then
  gen_list $CATEGORY
  echo 1 > $CURRENT
  IMG_NO=1
fi 

i=1;
if [[ $NO_OF_IMGS != 0 ]]
then
while read line 
do
  if [[ $i == $IMG_NO ]]
  then
    if [[ $NO_OF_IMGS == $IMG_NO ]]
    then
      echo 1 >  $CURRENT
    else
      echo $(($i+1)) >  $CURRENT
    fi
    cd $DOWNLOAD_DIR
    FILE_NAME="$( basename $line )"
    if [[ -e  "$DOWNLOAD_DIR/$FILE_NAME" ]]
    then
      echo  "$DOWNLOAD_DIR/$FILE_NAME"
    else
      wget -c "$line" -O "$FILE_NAME" >/dev/null 2>/dev/null
      #curl "$line" -o "$FILE_NAME" 2>/dev/null 
      echo  "$DOWNLOAD_DIR/$FILE_NAME"
    fi
    echo  "$line" > $CURRENT_IMAGE_URL
    break;
  fi
  i=$(($i+1));
done < $IMAGE_LIST
else
   echo $NO_IMAGE 
fi


local_images.sh
#!/bin/bash
# Copyright (C) 2008 Nayanajit Mahendra Laxaman mail: nmlaxaman@gmail.com


# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

if [[ $# -lt 1 ]]
then
   exit 0;
fi

CATEGORY=$1
WORKING="/tmp"
G_DIR="$HOME/Pictures/google"
CACHE_IMG_LIST="/tmp/cache_img_list_$CATEGORY"
THUMB_DIR="$WORKING/thumb_dir"
CURRENT="$WORKING/cache_current"
CURRENT_IMG_PATH="$WORKING/cache_current_img_path"
NO_IMAGE="/opt/myapps/no_image.png"


function urand(){
COUNT=$(($1+1))
while true
do
   FOUND=false
   RAND=$(($RANDOM%($COUNT)))
   for NO in $( cat $TMP 2>/dev/null )
   do
      if [[ $NO == $RAND ]]
      then
         FOUND=true
      fi
   done

   if ! $FOUND
   then
      echo $RAND
      echo $RAND >> $TMP
      break
   fi
done
}

function rand_file(){
TMP="/tmp/tn"
SC="/tmp/sc"
rm $TMP $SC 2>/dev/null
FILE=$1

COUNT=$( wc -l $FILE | awk '{print $1}' )
cat $FILE | while read line
do
   echo "LINE_ARRAY[$(urand $COUNT)]=\"$line\"" >> $SC
done 

source $SC
rm $FILE
for (( i=0; i<=${#LINE_ARRAY[@]}; i++ ))
do
   LINE=${LINE_ARRAY[$i]}
   if [[ $( echo $LINE | grep . ) != '' ]]
   then
      echo ${LINE_ARRAY[$i]} >> $FILE
   fi
done
}


function set_bg(){
XSPLASH_BG='/usr/share/images/xsplash/bg_2560x1600.jpg'
GDM_BG='/usr/share/backgrounds/Naranja.jpg'
if [[ $( file --mime-type "$@" | grep "image/" ) != '' ]]
then
   gpicview  "$@"&
   sleep 6
   zenity --question --text "Would you like to set it as the wallpaper, gdm background and, xsplash background?"  --cancel-label=NO;
   if [[ $? == 0 ]]
   then
      #Set user wallpaper
      gconftool-2 --type string --set /desktop/gnome/background/picture_filename "$@"
      if [[ -e $XSPLASH_BG ]] 
      then
         if [[ $( file --mime-type "$@" | grep jpeg ) != '' ]]
         then
            #Set xsplash background
            gksudo cp "$@" $XSPLASH_BG
            #gksudo cp "$@" $GDM_BG
         elif [[ -e /usr/bin/convert ]]
         then
            #Set xsplash background
            gksudo convert "$@" $XSPLASH_BG
            #gksudo convert "$@" $GDM_BG
         else
            zenity --error  --text="Can not set xsplash background and gdm background!"
         fi
      fi
      #Set gdm background
      sudo -u gdm gconftool-2 --type string --set /desktop/gnome/background/picture_filename "$@"
   fi
else
   zenity --error  --text="Image can not download\!"
fi
}

if [[ $# == 2 ]]
then
   #gpicview  "$( cat $CURRENT_IMG_PATH )" &
   set_bg  $( cat $CURRENT_IMG_PATH )
   exit 0;
fi

touch  $CACHE_IMG_LIST  $CURRENT 2>/dev/null
mkdir  $THUMB_DIR 2>/dev/null

IMG_NO=$( cat  $CURRENT )
NO_OF_IMGS=$( cat $CACHE_IMG_LIST | wc -l )

if [[ $NO_OF_IMGS == $IMG_NO || $NO_OF_IMGS == 0 ]]
then
   find -L $G_DIR | grep -e $CATEGORY -e '\.'...*$ > $CACHE_IMG_LIST
   rand_file $CACHE_IMG_LIST
   echo 1 > $CURRENT
   IMG_NO=1
fi 

i=1;
if [[ $NO_OF_IMGS != 0 ]]
then
while read line 
do
  if [[ $i == $IMG_NO ]]
  then
    if [[ $NO_OF_IMGS == $IMG_NO ]]
    then
      echo 1 >  $CURRENT
    else
      echo $(($i+1)) >  $CURRENT
    fi
    cd $THUMB_DIR
    FILE_NAME="$( basename $line )"
    if [[ -e "$THUMB_DIR/$FILE_NAME" ]]
    then
      echo "$THUMB_DIR/$FILE_NAME"
    else
      convert -resize 150 "$line" "$THUMB_DIR/$FILE_NAME" 2>/dev/null
      echo "$THUMB_DIR/$FILE_NAME"
    fi
    echo "$line" > $CURRENT_IMG_PATH
    break;
  fi
  i=$(($i+1));
done < $CACHE_IMG_LIST
else
   echo $NO_IMAGE
fi

No comments:

Post a Comment