Bit of new conky code - google calendar appointments

Add functionality to your desktop
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
Roken
Level 5
Level 5
Posts: 737
Joined: Fri Nov 19, 2010 4:55 pm
Location: Newport, S Wales

Bit of new conky code - google calendar appointments

Post by Roken »

OK, mainly a script that probably has applications outside of conky with a little modification, but primarily designed for conky. This will query google calendars for upcoming appointments (and appointments in the last 5 days) and display them in your conky. It requires the gcalcli package to work. First the script:

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.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Kernel Linux Tex 5.12.14-zen1-1-zen, XFCE
Arch
Dual GTX1070 8Gb
AMD Ryzen 1800X
32Gb RAM
User avatar
Roken
Level 5
Level 5
Posts: 737
Joined: Fri Nov 19, 2010 4:55 pm
Location: Newport, S Wales

Re: Bit of new conky code - google calendar appointments

Post by Roken »

Found an error parsing the date after the 5th of the month. The following should fix it (though I won't know until February). I could always change my system date, but I'd rather wait for the surprise :)

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"
  mdays=${mlength[$month-1]}
else
  let "ldays = $thisday - 5"
  let "mdays = $ldays + $ldays"
fi

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
Kernel Linux Tex 5.12.14-zen1-1-zen, XFCE
Arch
Dual GTX1070 8Gb
AMD Ryzen 1800X
32Gb RAM
Locked

Return to “Compiz, Conky, Docks & Widgets”