Tuesday, March 16, 2010

Astronomy Picture Of The Day Background Changer.


I've been always a big fan of transparent/opaque terminals, and it was essential to have a nice image on the background to keep me in a good mood and without too much distraction.
I remember spending time searching for a new desktop background from time to time when I grew bored of it, which was basically after a week or so, this wasn't an easy task and I often kept lots of images on a folder that was simply boring to search through.
I couldn't really find one that would stick with me forever so I decided to make an automatic wallpaper changer.
For a while I played with Flickr stores and it was all nice throwing random images to the desktop every 5 minutes.
The only problem was I didn't really find out how to set effective tag filters so now and then I had this ugly 640x480 as my background which was really painful. I didn't even bother trying to find out how friendly a image would be as a desktop background taking in mind it will be seen from a terminal.
So after a while of stumbling I saw the NASA's Astronomy Picture Of The Day and I had to start grabbing this images and set them on the background.
This is not an optimum setup and if anybody has suggestions please let me know.
For this task I play with several tools: DBUS, cron, gconftool, perl and wget.
I'm using gnome at the moment (Not a very happy desktop-manager user tho), and I had to include a file on the System -> Preferences -> Startup Applications.
The file is confdbus.sh which contains:
#!/bin/bash
# Export the dbus session address on startup so it can be used by cron
touch $HOME/.Xdbus
chmod 600 $HOME/.Xdbus

env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.Xdbus
echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.Xdbus

# Export XAUTHORITY value on startup so it can be used by cron
env | grep XAUTHORITY >> $HOME/.Xdbus
echo 'export XAUTHORITY' >> $HOME/.Xdbus

#Export http_proxy for the wget (Delete this lines if you don't use a proxy)
env | grep http_proxy >> $HOME/.Xdbus
echo 'export http_proxy' >> $HOME/.Xdbus

Note that you must manually execute ~/bin/confdbus.sh the first time in order to create the ~/.Xdbus file in your already open gnome session.
The next file we need is the file that will be called by the cron entry that will actually pull the new data.
This file is ~/bin/apod.sh which contains:
#!/bin/bash
#Create the directory if $HOME/.apod/ doesn't exist
mkdir -p ~/.apod/
#Get today's astronomy picture and save it to today.html
wget http://antwrp.gsfc.nasa.gov/apod/ -O $HOME/.apod/today.html
#Grep the IMG tag from apod and wget it to today.jpg
grep IMG $HOME/.apod/today.html| awk -F\" '{ system("wget http://antwrp.gsfc.nasa.gov/apod/"$2" -O $HOME/.apod/today.jpg") }'
#Set the new desktop background on gnome
/usr/bin/gconftool-2 --type string --set /desktop/gnome/background/picture_filename $HOME/.apod/today.jpg
#Format the file to end in the nearest
perl -p -i -e 'chomp if(!/\>$/)' $HOME/.apod/today.html;
#I like 42 columns for conky, otherwise it looks too big in my opinion.
export conkycolumns=42;
#Save the APotD Description on a txt file for conky, this perl call will grab only the section we care for, which is the Description.
perl -e 'my $flag=0;while(<>){ $flag = 0 if(/<b> Tomorrow/); if ($flag){s/<[^>]*>/\ /g; s/\ \ /\ /g; s/\ \././g; s/\ ,/,/g; s/([^\n]{0,$ENV{conkycolumns}})(?:\b\s*|\n)/$1\n/gi; print;} $flag=1 if (/Explanation\:/); }' $HOME/.apod/today.html > $HOME/.apod/today.txt


The crontab -l shows:
15 10 * * * . ~/.Xdbus; ~/bin/apod.sh > ~/apod.log 2>&1


I'm saving just the latest command run info for debug in case it fails, they have changed the format of the page like once in the last year.

My ~/.conkyrc has an:
execi 10 cat ~/.apod/today.txt

My full ~/.conkyrc config is:
# set to yes if you want Conky to be forked in the background
background yes

cpu_avg_samples 2
net_avg_samples 2

out_to_console no

# X font when Xft is disabled, you can pick one with program xfontsel
#font 7x12
#font 6x10
#font 7x13
#font 8x13
#font 7x12
#font *mintsmild.se*
#font -*-*-*-*-*-*-34-*-*-*-*-*-*-*
#font -artwiz-snap-normal-r-normal-*-*-100-*-*-p-*-iso8859-1

# Use Xft?
use_xft yes

# Xft font when Xft is enabled
xftfont Bitstream Vera Sans Mono:size=8

own_window_transparent yes
#own_window_colour hotpink
# Text alpha when using Xft
xftalpha 0.8

on_bottom yes

# mail spool
mail_spool $MAIL

# Update interval in seconds
update_interval 1
# Create own window instead of using desktop (required in nautilus)
own_window yes
own_window_type override
own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager

# Use double buffering (reduces flicker, may not work for everyone)
double_buffer yes

# Minimum size of text area
#minimum_size 280 5
#maximum_width 150

# Draw shades?
draw_shades no

# Draw outlines?
draw_outline no

# Draw borders around text
draw_borders no

# Stippled borders?
stippled_borders 10

# border margins
border_margin 4

# border width
border_width 1

# Default colors and also border colors
default_color white
default_shade_color white
default_outline_color white

# Text alignment, other possible values are commented
#alignment top_left
#minimum_size 10 10
#gap_x 13
#gap_y 13
alignment top_right
#alignment bottom_left
#alignment bottom_right

# Gap between borders of screen and text

# Add spaces to keep things from moving about? This only affects certain objects.
use_spacer no

# Subtract file system buffers from used memory?
no_buffers yes

#Text Buffer Size
text_buffer_size 1536

# set to yes if you want all text to be in uppercase
uppercase no

# boinc (seti) dir
# seti_dir /opt/seti

# Possible variables to be used:
#
# Variable Arguments Description
# acpiacadapter ACPI ac adapter state.
# acpifan ACPI fan state
# acpitemp ACPI temperature.
# adt746xcpu CPU temperature from therm_adt746x
# adt746xfan Fan speed from therm_adt746x
# battery (num) Remaining capasity in ACPI or APM
# battery. ACPI battery number can be
# given as argument (default is BAT0).
# buffers Amount of memory buffered
# cached Amount of memory cached
# color (color) Change drawing color to color
# cpu CPU usage in percents
# cpubar (height) Bar that shows CPU usage, height is
# bar's height in pixels
# downspeed net Download speed in kilobytes
# downspeedf net Download speed in kilobytes with one
# decimal
# exec shell command Executes a shell command and displays
# the output in torsmo. warning: this
# takes a lot more resources than other
# variables. I'd recommend coding wanted
# behaviour in C and posting a patch :-).
# execi interval, shell Same as exec but with specific interval.
# command Interval can't be less than
# update_interval in configuration.
# fs_bar (height), (fs) Bar that shows how much space is used on
# a file system. height is the height in
# pixels. fs is any file on that file
# system.
# fs_free (fs) Free space on a file system available
# for users.
# fs_free_perc (fs) Free percentage of space on a file
# system available for users.
# fs_size (fs) File system size
# fs_used (fs) File system used space
# hr (height) Horizontal line, height is the height in
# pixels
# i2c (dev), type, n I2C sensor from sysfs (Linux 2.6). dev
# may be omitted if you have only one I2C
# device. type is either in (or vol)
# meaning voltage, fan meaning fan or temp
# meaning temperature. n is number of the
# sensor. See /sys/bus/i2c/devices/ on
# your local computer.
# kernel Kernel version
# loadavg (1), (2), (3) System load average, 1 is for past 1
# minute, 2 for past 5 minutes and 3 for
# past 15 minutes.
# machine Machine, i686 for example
# mails Mail count in mail spool. You can use
# program like fetchmail to get mails from
# some server using your favourite
# protocol. See also new_mails.
# mem Amount of memory in use
# membar (height) Bar that shows amount of memory in use
# memmax Total amount of memory
# memperc Percentage of memory in use
# new_mails Unread mail count in mail spool.
# nodename Hostname
# outlinecolor (color) Change outline color
# pre_exec shell command Executes a shell command one time before
# torsmo displays anything and puts output
# as text.
# processes Total processes (sleeping and running)
# running_processes Running processes (not sleeping),
# requires Linux 2.6
# shadecolor (color) Change shading color
# stippled_hr (space), Stippled (dashed) horizontal line
# (height)
# swapbar (height) Bar that shows amount of swap in use
# swap Amount of swap in use
# swapmax Total amount of swap
# swapperc Percentage of swap in use
# sysname System name, Linux for example
# time (format) Local time, see man strftime to get more
# information about format
# totaldown net Total download, overflows at 4 GB on
# Linux with 32-bit arch and there doesn't
# seem to be a way to know how many times
# it has already done that before torsmo
# has started.
# totalup net Total upload, this one too, may overflow
# updates Number of updates (for debugging)
# upspeed net Upload speed in kilobytes
# upspeedf net Upload speed in kilobytes with one
# decimal
# uptime Uptime
# uptime_short Uptime in a shorter format
#
# seti_prog Seti@home current progress
# seti_progbar (height) Seti@home current progress bar
# seti_credit Seti@hoome total user credit


# variable is given either in format $variable or in ${variable}. Latter
# allows characters right after the variable and must be used in network
# stuff because of an argument
#${font Dungeon:style=Bold:pixelsize=10}I can change the font as well
#${font Verdana:size=10}as many times as I choose
#${font Perry:size=10}Including UTF-8,
#${font Luxi Mono:size=10}justo como este texto que o google traduz fêz o português
# stuff after 'TEXT' will be formatted on screen
#${font Grunge:size=12}${time %a %b %d}${alignr -25}${time %k:%M}

TEXT
$nodename - $sysname $kernel on $machine
$stippled_hr
${color lightgrey}Uptime:$color $uptime ${color lightgrey}- Load:$color $loadavg
${color lightgrey}CPU Usage:${color #5000a0} ${cpu}% ${cpubar}
${color black}${cpugraph 000000 5000a0}
${color lightgrey}RAM Usage:$color $mem/$memmax - $memperc% $membar
${color lightgrey}Swap Usage:$color $swap/$swapmax - $swapperc% ${swapbar}
${color lightgrey}Processes:$color $processes ${color grey}Running:$color $running_processes
$color$stippled_hr
${color lightgrey}Networking:
Down:${color #8844ee} ${downspeed eth1} k/s${color lightgrey} ${offset 70}Up:${color #22ccff} ${upspeed eth1} k/s
${color black}${downspeedgraph eth1 32,150 ff0000 0000ff} $alignr${color black}${upspeedgraph eth1 32,150 0000ff ff0000}
${color lightgrey}File systems:
/ $color${fs_used /}/${fs_size /} ${fs_bar /}
/home $color${fs_used /home}/${fs_size /home} ${fs_bar /home}
/var $color${fs_used /var}/${fs_size /var} ${fs_bar /var}
#${font Dungeon:style=Bold:pixelsize=12}${color #88aadd}MPD: ${alignc}$mpd_artist - $mpd_title
#${color #88aadd}$mpd_bar
#${color #88aadd}${alignc}$mpd_status
${color}Name PID CPU% MEM%
${color #ddaa00} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}
${color lightgrey} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}
${color lightgrey} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}
${color}Mem usage
${color #ddaa00} ${top_mem name 1} ${top_mem pid 1} ${top_mem cpu 1} ${top_mem mem 1}
${color lightgrey} ${top_mem name 2} ${top_mem pid 2} ${top_mem cpu 2} ${top_mem mem 2}
${color lightgrey} ${top_mem name 3} ${top_mem pid 3} ${top_mem cpu 3} ${top_mem mem 3}

${color #ddaa00}
${font Dungeon:style=Bold:pixelsize=12}Astronomy PotD Explanation:
${color lightgrey}
${font Dungeon:style=Bold:pixelsize=12}${execi 10 cat ~/.apod/today.txt}