Some of my scripts for daily use on a linux desktop

Showing posts with label bash script. Show all posts
Showing posts with label bash script. Show all posts

Friday, April 9, 2010

Pie Chart & Ring Widget



Edit: v1.1 15/05/2010, widget can now draw a ring, see at bottom.

Here is another widget for Conky... it's a pie-chart fully customizable. For example, for the circle in the above picture : it's a file system (yes it is!), each sector is proportional to the size of a disk, and two colors are used to draw a sector (one for used space, and one for free space).

It can be downloaded from Ubuntu forums here.

You need Conky 1.8.0 to use it.
To call it in a Lua script, you have to set parameters in a table:
pie_settings= { table1, table2 ...}

Each table has is own parameters, only one is mandatory, it's tableV, a table containing the values to display.

Example of tableV, of course, in real life, table is created by the script :
 -- table of labels and values 
{{label1,conky_variable,conky_argument,convert to Go-Mo-Ko units (true/false) or unit}, ...}
-- all the values are at maximum values in this example
-- last parameter set to true means that the script
will convert the values in Go, Mo ...
local k=1024*1024*1024
tableV={{"/","", k*50, k*50,true},
{"/home","", k*500, k*500,true},
{"/data","", k*1000, k*1000,true},
{"stuff","", k*10, k*10,true}
},

The Lua file contains a script which use the output of df command, because this widget was made with the disks spaces in mind, but it can be use with any other value, like this (but it's not the purpose of the script, so we won't use it):
 tableV={{"cpu0","cpu", "cpu0",100,"%"},
{"cpu1","cpu", "cpu1",100,"%"},
{"fan","", "${exec sensors | grep 'CPU Fan' | cut -c13-16}", 2000," rpm"},
{"sys","", "${exec sensors | grep 'Sys Temp' | cut -c15-17}", 100,"°C"}
},

Examples of tablebg and tablefg (background and foreground colors):
 tablebg={{0xFFFFFF,0.5},
{0xFFFFFF,0.5},
{0xFFFFFF,0.5},
{0xFFFFFF,0.5}
},
tablefg={{0xFF0000,1},
{0x00FF00,1},
{0x0000FF,1},
{0xFFFF00,1}
},

With the above parameters and these ones :
 xc=conky_window.width/2,
yc=conky_window.height/2,
int_radius=0,
radius=100,
first_angle=0,
last_angle=360,
proportional=true,
gradient_effect=false,
show_text=true,
line_lgth=30,
line_space=20,
line_width=1,
extend_line=true,
txt_font="FreeSans",
font_size=12,
font_color=nil,
font_alpha=1,
txt_offset=1,
txt_format="&m",
nb_decimals=0,

we obtain this simple kind of chart


If the number of values in a colors table is less than the number of sectors, like that :
 tablefg={{0xFF0000,1},
{0xFFFF00,1}},

colors restart at the end of the table :

Now, if we change the second parameter of tableV, the value for each sector, we change the way the graph is displayed.
 local k=1024*1024*1024
tableV={{"/", .25*k*50, k*50,true},
{"/home", .50*k*500, k*500,true},
{"/data", .75*k*1000, k*1000,true},
{"stuff", .90*k*10, k*10,true}
},


This is what I wanted to display on my desktop : the amount of used space on my external or internal drives and the proportion of each drive in the full file system. Like the big arc on first picture, I have to clean some disks ...



Now, have a look to more interestings parameters ;-)
With int_radius=50, (internal radius)


For the angles, (O° is North and angles go clockwise),
 first_angle=-45
last_angle=180


Back to full circle and proportional parameter:
 first_angle=0,
last_angle=360,
proportional=false,


Back to proportional=true and adding some gradient and hiding text
 proportional=true,
gradient_effect=true,
show_text=false,




By the way, here is an interesting script to toggle the text (or others flags) when the conky is running.
In the Lua script :
 local file = io.open("/tmp/flag-conky","r")
io.close()
show_text=(file == nil)

And a bash script associated with a keyboard shortcut:
 #!/bin/bash
flag="/tmp/flag-conky"
if [ -f $flag ]; then
rm $flag
else
echo > $flag
fi



The lines options
 line_lgth=100
line_width=4


line_space can be usefull when labels are too close:
--for the second arc (yellow and blue labels are separated)
first_angle=0,
last_angle=90,
line_space=20,


Last parameter for lines is extend_line which will extend the horizontal line if the text is too long, with extend_line=true on the second arc :

The text options
txt_font and font_size are obvious.
font_color and font_alpha create a linear gradient. To keep text color the same color of the line, set font_color=nil
txt_offset is the space between the line and the text
nb_decimals is the number of decimals for the numbers ;-)
With
 txt_font="FreeSans",
font_size=12,
font_color=0xFFFFFF,
font_alpha=0.75,
txt_offset=1,
nb_decimals=1,



The last parameter txt_format is the string for formating texts. Some tags can be used :

-- &l for label (the first value of tableV)
-- &v for value (the second value of tableV)
-- &m for max value (the third value of tableV)
-- &o for occupied percentage (ie tableV[2]/tableV[3])
-- &f for free percentage (ie tableV[3] - tableV[2]/tableV[3])
-- &n for free value (non-occupied) (ie tableV[3] - tableV[2])

With
 txt_format="&l &m, free &f",
nb_decimals=0,



That's all ;-)



Edit : a last example (code is here ):



Edit for version 1.1, I added the parameter type_arc. If type arc is "r" (the default value) values are displayed on a radial way as before :

but if type_arc="l" (l for linear) then values are dispayed like rings :

Wednesday, April 7, 2010

Square to Rounded image with Lua/imlib


I wanted to display Moon and Earth pictures on my conky but the only images I found where squares images with a black background and the Moon or the Earth in the middle.
So to display the image correctly in the conky, I had to set transparency of the background of the original image. Well, I've done that with imlib and post the resulting script here (or in french here).

I also post the script to get Earth or Moon images from this site :
http://www.fourmilab.ch/earthview/expert.html

Of course the images are updated every hour so the night area is reflecting the reality !
And of course again, there are a few parameters to set the latitude, longitude and so on ...

If you are inspired, you can have some amazing conkys like this one :


or that:



I also updated the clock and the calendar wheel (this one is now a widget and it is more easy to use it).
And I discover that the widgets can be called with the dofile() function in the Lua script. No more need to copy/paste the widget but to use a line like this :
dofile("/home/wlourf/calendar/calendar.lua")

Tuesday, February 2, 2010

How to get pictures from RSS feeds from deviantArt (get_deviation_search.sh v1.0)

Each time you make a research on deviantArt web site (or when you browse a category) a RSS feed is generated.
For example, to see the newest deviations in the fractal category, the rss feed generated at the bottom of the page is http://backend.deviantart.com/rss.xml?q=in%3Adigitalart/fractals%20sort%3Atime&type=deviation.

For my Photo Album script, I wanted to download/save pictures from the RSS feed to my hard drive.
Well, I wrote something and it works but it certainly can be improve!

I call this script : get_deviation_search.sh and it should be run with at least three arguments:
1st arg : folder where to save the pictures
2nd arg : delay (in minutes) for repeating the script (0 is for a single run)
3rd arg and following : urls of the rss feed to follow
and more parameters can be set inside the script


#example for photomanipulations, fractals categories and research for "conky"

./get_deviation_search.sh /home/wlourf/deviant 5 http://backend.deviantart.com/rss.xml?q=in%3Adigitalart/fractals%20sort%3Atime\&type=deviation http://backend.deviantart.com/rss.xml?q=in%3Adigitalart/photomanip%20sort%3Atime\&type=deviation http://backend.deviantart.com/rss.xml?q=in%3Adigitalart/fractals%20sort%3Atime\&type=deviation http://backend.deviantart.com/rss.xml?q=sort%3Atime%20conky\&type=deviation


IMPORTANT : In the RSS feed the symbol & has to replace by \& elsewhere it won't work

This script can also be downloaded here


En français (soyons fous) :
Ce script permet de télécharger les images des liens RSS générés sur les pages des résultats de recherche sur deviantArt.
Il fonctionne avec 3 arguments minimum :
1: le dossier où enregistrer les images
2: le temps à attendre entre chaque boucle (0 pour une seule boucle)
3 et suivants : liens RSS (Dans ces liens il faut remplacer & par /&)


The script :

#! /bin/bash
#This script get pictures from RSS feed from deviantArt website and save them to hard disk
#
# by wlourf http://u-scripts.blogspot.com/
# v1.0 - 02 Feb. 2010
#
#1st arg : folder where to save the pictures
#2nd arg : delay (in minutes) for repetaing the script (0 is for a single run)
#3rd arg and following : urls of the rss feed to follow

#some paramaters are set here
file1=/tmp/get_deviant_rss.txt
file2=/tmp/get_liste.txt
#display filename even if it is not saved (ie saved previously)
display_files_not_saved=false
#if script running in gnome_terminal, right click on the file can open it
display_files_with_link=true
############################# end ###########################

clear

if [[ $# -lt 3 ]]; then
  echo 'This script needs at least 3 arguments : '
  echo ' - full path of the folder where save the pictures'
  echo ' - run the script every X minutes, 0 for a single run'
  echo ' - one or more RSS feeds to watch (take care to escape the & symbol with \&)'
  exit
fi

folder=$1
repeat=$2
args=$*
rss=${args#$1" "}
rss=${rss#$2" "}
nbUrl=0

if [[ {$folder:-1} != "/" ]]; then
  folder="$folder/"
fi

for url in $rss
do
  ((nbUrl++))
  tabRSS[$nbUrl]=$url
done

nbLoops=0
flag=true
while ( $flag ); do

  echo "-------------------"
  echo "folder = "$folder
  echo "-------------------"
  echo "loop every $repeat minutes"
  echo "-------------------"

  ((nbLoops++))

  mkdir -p $folder
  cd $folder

  for ((u=1 ; u<=$nbUrl ; u++))
  do
    #read one rss feed
    echo "-------------------"
    echo "rss $u/$nbUrl = "${tabRSS[u]}
    GET ${tabRSS[u]} > $file1
    
    #extract the link to the fullview image
    match="medium=\"image\""
    url_line=""

    begin="http"
    end="\" height"

    table=()
    idx=1
    #put the links in a table for better display
    while read line
    do
      if [[ "$line" =~ "${match}" ]]; then
        url_line=$line
        a=$(($(expr "$url_line" : ".*$begin")-${#begin}))
        b=$(($(expr "$url_line" : ".*$end")-$a-${#end}))
        url_link=${url_line:$a:$b}
      table[$idx]=$url_link
      ((idx++))
      fi
    done < $file1

    #read the table ans save the image if not already saved
    nbLinks=$(($idx-1))
    nbSaved=0
    for ((a=1 ; a<=$nbLinks ; a++))
    do
      link=${table[a]}
      img_name=$(expr match "$link" '.*\(/.*\)')
      img_name=${img_name:1}
      txt="loop $nbLoops - url $u - $a/$nbLinks"
      if ($display_files_with_link) then
        strFile="file://"$folder$img_name
      else
        strFile=$img_name
      fi
      if [ -f "$img_name" ]; then
        if ($display_files_not_saved) then
          echo $txt" - *** "$strFile" *** already saved"
        fi
      else
        echo $txt" - "$strFile
        wget -q $link
        ((nbSaved++))
      fi

    done
    echo
    echo "$nbSaved/$nbLinks pictures saved for loop number $nbLoops and rss= $url"
    echo "-------------------"
    echo
  done
  
  if [[ $repeat -eq "0" ]]; then
    flag=false
  fi

  nows=$(date +%s)
  waitto=$(($nows+$repeat*60))
  sl=$(($waitto-$nows))
  echo "Wait $((sl/60)) minutes till :"
  date --date "$sl sec"

  sleep $sl
done

echo
echo "Finish! after $nbLoops loop(s)"

Sunday, January 31, 2010

How to get random pictures from deviantArt (get_deviation.sh v1.0)

As I was a bit boring to see always the sames pictures from my hard drive with my conky script "Photo Album Stack", I tried to get some other pictures from the web.

Picasa as an interesting Slideshow tool.
If you open just this page in a browser (epiphany for example), you have to set the folder of the cache
/home/wlourf/.gnome2/epiphany/mozilla/epiphany/Cache in the script and that's it.
But pictures on Picasa are not really interesting, isn't it?

DeviantArt has also a random tool but whithout slideshow, so you have to click to display images ...
I wrote this script (get_deviation.sh) that go to the random page and save the image (in fullsize) to the hard disk. This one can be improved because regular expressions are not my favorite toy!
Next, I will try to do the same for pictures from one single category.

Before running the script you have to set the folder variable in it.

This script can also be downloaded here.


In a few days, I will post the last version of the Photo Album Script which will run with this script.


En français (soyons fous)!
Ceci est un script qui enregistre des images aléatoires du site deviantArt sur son PC. Pour être utilisé, il faut paramétrer la variable folder au début du script.


The script : get_deviation.sh

#! /bin/bash
#This script get a random image from deviant art website and save it to hard disk!
#
# by wlourf http://u-scripts.blogspot.com/
# v1.0 - 31 Jan. 2010
#
#argument 1 (optional) is the path+filename where to save the image
#if argument 1 is not present, image will be saved in $folder
#
#other paramaters are set here
file1=/tmp/get_random.txt
file2=/tmp/get_image.txt
folder=~/deviant

############################# end ###########################

#get a random url
GET http://www.deviantart.com/random/deviation > $file1

#extract the link to the deviation page
match="<input type="\" name="\" value="\" begin="http" end="\">"

a=$(($(expr "$url_line" : ".*$begin")-${#begin}))
b=$(($(expr "$url_line" : ".*$end")-$a-${#end}))

url_page=${url_line:$a:$b}

#echo "deviation--> "$url_page

#get the deviation page
GET $url_page > $file2

#extract the link to the fullview image
match="fullview"
url_line=""

while read line
do
  if [[ "$line" =~ "${match}" ]]; then
    url_line=$line
    break
  fi
done < $file2

begin="src\":\""
end="\"},\"smallview"

b=$(($(expr "$url_line" : ".*$end")-${#end}))
url2=${url_line:0:$b}

a=$(($(expr "$url2" : ".*$begin")))
url_img=${url2:$a}

#save image to hard disk if url ok
if [[ $url_img != "" ]]; then
  cd $folder
  if [[ $1 != "" ]]; then
    wget $url_img -O $1
  else
    wget $url_img
  fi
fi



If you want the script to run in a loop, just write in another script:

while true
do
  ./get_deviation.sh
done