- Code: Select all
#!/bin/bash
# We need linebreaks fixing for the return output from gcalcli
IFS="
"
#Now set up some initial variables
month=`date +%m`
today=`date +%d/%m/%Y`
year=`date +%Y`
thisday=`date +%d`
nmonth=$month
reallytoday=$thisday
# And and array with the number of days in each month
declare -a mlength=( 31 28 31 30 31 30 31 31 30 31 30 31 )
syear=$year
curyear=$year
days=${mlength[$month-1]}
maxdays=$days
let "maxdays = $maxdays + 1"
# Now this bit gets complicated. The script will display the past five days and the next 23 days appointments (28 days in total) so we need some jigarry pokery to fix up the start and end dates
if [ $thisday -lt 6 ]; then
if [ $month -gt 1 ]; then
let "month = $month - 1"
else
month=12
let "syear = $year - 1"
fi
let "ldays = 5 - $thisday"
else
let "ldays = $thisday - 5"
fi
mdays=${mlength[$month-1]}
let "thisday = $mdays - $ldays"
stdate=$month/$thisday/$syear
nday=`expr $days - $reallytoday`
nyear=$curyear
if [ $nday -lt 28 ]; then
let "nday = 28 - $nday"
let "nmonth = $nmonth + 1"
if [ $nmonth -gt 12 ]; then
nmonth=1
let "nyear = $curyear + 1"
fi
fi
ndate=$nmonth/$nday/$nyear
# OK - we have the date range. The script will work either using a config file for multiple accounts, or by specifying the accoutn details on the script command line, so we need to check both
if [ -f ~/.gcals -a $# = 0 ]; then
while read line
do
# parse the config file
l=$line
account=`echo $l | sed 's/user\=\(.*\)password.*/\1/'`
password=`echo $l |sed 's/.*password\=\(.*\)/\1/'`
# and get the info from gcalcli
events=(`gcalcli --user $account --pw $password --nc --24hr agenda "$stdate" "$ndate"`)
# Now we need to format it for conky. You must have the colors defined in your conkyrc. Feel free to change the color declarations (or remove them altogether) but make sure you only change or remove \{colorX} or you'll break the sed expressions.
for i in "${events[@]}"
do
ev=""
ev="$ev`echo $i | sed 's/ \{9\}/\$\{color8\}\$\{goto 130\}/' | sed 's/\( [0-9]\{2\}\) /\1\$\{color7\}\$\{goto 80\}/' | sed 's/\(:[0-9]\{2\}\) /\1\$\{color8\}\$\{goto 130\}/'`"
ev="\${color5}$ev"
if [ -n $ev ]; then
echo -e "$ev"
fi
done
done < ~/.gcals
else
if [ $# = 2 ]; then
# As above
events=(`gcalcli --user $1 --pw $2 --nc --24hr agenda "$stdate" "$ndate"`)
for i in "${events[@]}"
do
ev=""
ev="$ev`echo $i | sed 's/ \{9\}/\$\{color8\}\$\{goto 130\}/' | sed 's/\( [0-9]\{2\}\) /\1\$\{color7\}\$\{goto 80\}/' | sed 's/\(:[0-9]\{2\}\) /\1\$\{color8\}\$\{goto 130\}/'`"
ev="\${color5}$ev"
if [ -n $ev ]; then
echo -e "$ev"
fi
done
else
# Very limited checking - if there's no config file and less than two command line arguments, output an error
echo "Not enough arguments"
fi
fi
The script will either read account details from the command line or from a config file. If you use multiple google calendars the config file is probably better. Save the script somewhere accessible (I use ~/scripts/) as gcalparse.sh and make it executable (cd ~/scripts && chmod a+x gcalparse.sh). the config file is called ~/.gcals and has one line for each account as follows:
- Code: Select all
user=youraccount@gmail.com password=yourpassword
Or to use command line arguments:
- Code: Select all
bash gcalparse.sh youraccount@gmail.com yourpassword
Note - if your sh link points to dash then you MUST call the script with bash rather than sh. dash doesn't support the arrays used in this script. if sh points to bash then you are fine to use sh.
Now, in your conkyrc you need to define a couple of variable colours:
- Code: Select all
color5 ffff00 #Orange
color7 00ff00 #Green
color8 ff0000 #Red
Change these as you see fit or, see the comments in the bash script for removing them altogether.
Finally, the actual code to call the script in conky:
If you are using a config file:
- Code: Select all
${color5}Date${goto 80}${color7}Time${color8}${goto 130}Event
${execpi 30 bash /path/to/script/gcalparse.sh}
Or without a config:
- Code: Select all
${color5}Date${goto 80}${color7}Time${color8}${goto 130}Event
${execpi 30 bash /path/to/script/gcalparse.sh youraccont@gmail.com yourpassword}
Again, you can substitute or remove the colouring as you see fit. Note the use of execpi rather than exec, to make sure that conky properly parses the input. This is for a 30 second delay. Feel free to change the delay time to.
NOW THE BAD NEWS
There's a bug in the conky < 1.82 that means it doesn't correctly parse the input to execpi (or to execp) and the second and subsequent lines won't respect the layout. This is fixed in conky 1.82 and later. If you only have one appointment every 28 days you won't notice (the first line is output correctly) but if you are likely to have more than one entry then you MUST update conky.



