Transfer Jpilot data into Thunderbird's Lightning calendar

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
User avatar
Indy
Posts: 73
Joined: Wed 01 Feb 2006, 10:52
Location: Sydney, Australia

Transfer Jpilot data into Thunderbird's Lightning calendar

#1 Post by Indy »

Transfer Jpilot data into Thunderbird's Lightning add-on (manually, but with help from a bash script).

Tested in:
- Thunderbird (3.1.10)
- Lightning add-on (1.0b2)
- Jpilot (1.6.0)


What:

jpilot2lightning is a bash script to export (only) Today's appointments from Jpilot to create a .csv file that is already massaged into the format that Lightning expects. The output file is called jpilot2lightning.csv.

Why:

With this, I can keep an eye on today's events/appointments (and Todos) when I'm in Thunderbird (where I spend a lot of time) so I don't have to keep checking Jpilot (my main PIM).

Jpilot (GUI) does have its own export feature but it exports everything. I only want to export Today's. If you do multiple imports in Lightning, you end up with duplicate entries. Also, Lightning expects a different record layout from what Jpilot produces. This script does the massaging to the expected record layout for you.

The way I use it is, in the mornings, I run this script then import the jpilot2lightning.csv file into Thunderbird.

This is my daily "poor man's sync" of Jpilot to Lightning and is strictly one-way. Seems to be working ok for me, so far!

Note: Jpilot "Todos" are converted into Events for today's calendar in Thunderbird. This is because the Import for Tasks into Lightning doesn't seem to work/exist. The script gives all todos an "event time" of 12:00pm so they're listed in the middle of my Calendar page (which I have set to display 24hrs per day)

To use:

Extract jpilot2lightning (attached) into somewhere like /root/my-applications/bin, make sure it's executable, and you're ready to go.

1. In a terminal, run jpilot2lightning
2. In Thunderbird (with Lightning installed), manually import the data using the jpilot2lightning.csv (in /root) as the source. Done!


Sample output:

Code: Select all

# jpilot2lightning

------------------------------
jpilot2lightning run complete
------------------------------

  1) Today's Jpilot appointments (and Todos) exported to /root/jpilot2lightning.csv.
     (NB. Todos are converted to "appointments" - all are set for 12:00:00 PM)
  2) Ready to (manually) import into Thunderbird's Lightning (calendar) add-on.

     To import, go into Thunderbird, then choose:
     - Events and Tasks
     - Import...
     - at the bottom right, change display from (*.ics) to (*.csv)
     - navigate to /root
     - select jpilot2lightning.csv
     - click Open
  Done! Today's appointments should now be in your Calendar

Note:
----
If you're (re-)updating today's appointments in Lightning from Jpilot, to avoid duplicate entries,
you may want to delete Today's events in Lightning before you do the import again.

An easy way to do that is, in Thunderbird:
  i) go to your Calendar tab,
 ii) change the display to Today's Events,
iii) press CTRL-A to select all entries,
 iv) then hit Delete.
#
------------------------------------------------------------------

Code: Select all

#!/bin/sh
#-----------------------------------------------------------------
# jpilot2lightning (v3.7)
# Exports Jpilot's APPOINTMENTS for TODAY (only) and creates a .CSV file that can be (manually) imported into Thunderbird's Lightning add-on.
# Does a separate data-dump for the TODOs. Todos are converted into "appointments" as Lightning's Tasks Import seems to be non-existent.
# Output file: $HOME/jpilot2lightning.csv (usually, /root)
# Indy. May-2011 
#-----------------------------------------------------------------
#
# Variables expected by Thunderbird's Lightning (Calendar) add-on
# ----------------------------------------------------------------
# Description         Variable name  Notes (format)
# ------------------  -----------    ------------------------------
# Subject           : subject
# Start Date		: startdate 	 (mm/dd/yyyy)
# Start Time		: starttime 	 (hh:mm:ss AM/PM)
# End Date			: enddate 		 (mm/dd/yyyy)
# End Time			: endtime 		 (hh:mm:ss AM/PM)
# All day event		: alldayevent	 (True/False)
# Reminder on/off	: reminder		 (True/False)
# Reminder Date		: reminderdate 	 (mm/dd/yyyy)
# Reminder Time		: remindertime 	 (hh:mm:ss AM/PM)
# Categories		: categories
# Description		: description
# Location			: location
# Private			: private		 (True/False)
#-----------------------------------------------------------------

# Set output file here:
outputfile="$HOME"/jpilot2lightning.csv
today=`date +%m/%d/%Y`

line1='"Subject","Start Date","Start Time","End Date","End Time","All day event","Reminder on/off","Reminder Date","Reminder Time","Categories","Description","Location","Private"'
echo $line1 > $outputfile # Write the first line (headers) to the output file:

jpilotdata=/tmp/todays_entries.csv # temp data dump via jpilot-data
jpilot-dump +D%N%XRDATA:%w%v%u -N > $jpilotdata # NB. "RDATA:" signifies start of the Reminder Data portion, after the colon.

inputfile=$jpilotdata

while read curline; do

#	Set: starttime
	starttimehh=${curline:11:2}
	starttime24hh=$starttimehh # used later to help calculate the reminder time
	starttimemm=${curline:14:2}

	if  [ "$starttimehh" -gt 12 ]; then
		starttimehh=$((starttimehh-12))
		amorpm="PM"
	else
		if  [ "$starttimehh" -eq 12 ]; then
			amorpm="PM"
		else
			amorpm="AM"
		fi
	fi
		
	starttime=$starttimehh':'$starttimemm':00 '$amorpm

#	Set: enddate
	enddateyy=${curline:19:2}
	enddatemm=${curline:22:2}
	enddatedd=${curline:25:2}
	enddate=$enddatemm'/'$enddatedd'/20'$enddateyy

#	Set: endtime
	endtimehh=${curline:28:2}
	endtimemm=${curline:31:2}

	if  [ "$endtimehh" -ge 12 ]; then
		endtimehh=$((endtimehh-12))
		amorpm="PM"
	else
		amorpm="AM"
	fi
		
	endtime=$endtimehh':'$endtimemm':00 '$amorpm

#	Set: subject
	reclength=${#curline}
	subjecttemp=${curline:35:reclength}
	endquote=`expr index "$subjecttemp" \" `
	endofsubject=$((endquote-1))
	subject=${curline:35:endofsubject}

#	Set: reminder    (Keep in mind that I only get Today's appointments.)

# 	Get the index of "RDATA:" ie., start of reminder data.Add 5 to the index to point to the start of the actual data (after the colon).
	reminderdatastart=$((`awk -v a="$curline" -v b="RDATA:" 'BEGIN{print index(a,b)}'`+5))
	reminderdatalength=$((reclength-reminderdatastart))
	reminderdata=${curline:reminderdatastart:reminderdatalength}
	reminderswitch=${reminderdata:0:1}									# switch: 1 = alarm on, 0 = alarm off
	remindervalue=${reminderdata:1:(reminderdatalength-2)}				# value: number of units in advance of target to give the alarm
	reminderunit=${reminderdata:(reminderdatalength-1):1}				# unit: m = minutes, h = hours, d = days (in advance of target date/time)

	if [ "$reminderswitch" = "1" ]; then
		reminder="True"
	else
		reminder="False"
	fi

#	Set: reminder_date
	reminderdate=$today # Old, repeating events can have very old start dates, so just reset to Today.

#	Set: reminder_time
#	Jpilot works out the reminder in terms of "%v, number of advance alarm units (m-inutes, h-ours or d-ays)

#	Calculate starttime no. of minutes from midnight for Start Time:
	hh2mmfrommidnight=$((10#$starttime24hh * 60))					# explicitly state base 10 to stop error; was interpreting octal if 08,09
	totalminsfrommidnight=$((hh2mmfrommidnight+starttimemm))

#	Calculate the reminder in-advance minutes:

	case "$reminderunit" in
	
	m) 	units="minutes"
		reminderadvancem=$remindervalue
		;;
	h) 	units="hours"
		reminderadvancem=$((remindervalue * 60))
		;;
	d) 	units="days"
		reminderadvancem=$((remindervalue * 1440))
		;;
	*) 	units="minutes"
		reminderadvancem=$remindervalue
		;;
	esac

#	Calculate the actual reminder time (i.e., starttime, converted to totalminsfrommidnight, minus reminderadvancem, converted back to hh:mm)
	remindertimemins=$((totalminsfrommidnight - reminderadvancem))
	remindertimehh=`printf "%02d" $((remindertimemins / 60))`
	remindertimemm=`printf "%02d" $((remindertimemins % 60))`

#   To cater for those wildly-in-advance reminder times (to before today) just reset to start time.
#   I may make it default to something else, say, 2 hours before the event (enough time to shower, change and rush on over there?!?)
	if [ "$remindertimehh" -lt 0 ]; then 
		remindertimehh=$starttime24hh
		remindertimemm=$starttimemm
	fi	

# 	Convert back to 12-hr format:
	if  [ "$remindertimehh" -gt 12 ]; then
		remindertimehh=$((remindertimehh-12))
		reminderamorpm="PM"
	else
		if  [ "$remindertimehh" -eq 12 ]; then
			reminderamorpm="PM"
		else
			reminderamorpm="AM"
		fi
	fi

	remindertime=$remindertimehh":"$remindertimemm":00 "$reminderamorpm

#	Set:description

	endofdescription=$((`awk -v a="$curline" -v b="RDATA:" 'BEGIN{print index(a,b)}'`-1))
	descriptionstart=$((36+endofsubject))
	descriptionlength=$((endofdescription-descriptionstart))
		
	if [ "$descriptionlength" -gt 0 ]; then
		description=${curline:descriptionstart:descriptionlength}
	else
		description=""
	fi

# 	DATE OVERRIDE. startdate and enddate set to today's date. This is because repeat events retain the original date which can be very old, even if the entry is valid for today.

	startdate=$today
	enddate=$today

# 	Set defaults for these remaining variables, for which the jpilot-dump doesn't (seem to) give any data, thus these arbitrary defaults:
	alldayevent="False"
	categories=""
	location=""
	private="False"

# 	Set up and write (append) the record to the $outputfile:	
	jpilot_to_lightning='"'$subject'","'$startdate'","'$starttime'","'$enddate'","'$endtime'","'$alldayevent'","'$reminder'","'$reminderdate'","'$remindertime'","'$categories'","'$description'","'$location'","'$private'"'

	echo $jpilot_to_lightning >> $outputfile
	
done < $inputfile


#--------------------------------------------------------
# TODO section
#--------------------------------------------------------
datemin=`date +%Y%m%d`
jpilotdata=/tmp/todays_todo_entries.csv

jpilot-dump -T > $jpilotdata # Todo dump

inputfile=$jpilotdata
todostarttime="12:00:00 PM" # arbitrarily set to 12:00pm (Jpilot doesn't allow timestamps for Todos)
#starttimehh="00"
#starttimemm=0
#amorpm="AM"

while read curline; do
	
	completedflag=${curline:0:1}
	startdateyy=${curline:6:4}
	startdatemm=${curline:11:2}
	startdatedd=${curline:14:2}
	startdate=$startdatemm'/'$startdatedd'/'$startdateyy

	tododate=$startdateyy$startdatemm$startdatedd 
	
	if [ "$tododate" -ge "$datemin" ] && [ "$completedflag" = "0" ]; then
		starttime=$todostarttime 
		enddate=$startdate
		endtime=$starttime
		reclength=${#curline}
		subject=${curline:18:(reclength-19)}
		reminder="False"
		reminderdate=$today
		remindertime="00:00:00 PM"
		description=""
		startdate=$today
		enddate=$today
		alldayevent="False"
		categories=""
		location=""
		private="False"
		jpilottodo2lightning='"'$subject'","'$startdate'","'$starttime'","'$enddate'","'$endtime'","'$alldayevent'","'$reminder'","'$reminderdate'","'$remindertime'","'$categories'","'$description'","'$location'","'$private'"'
		echo $jpilottodo2lightning >> $outputfile
	fi
		
done < $inputfile

echo -e "\n------------------------------"
echo "jpilot2lightning run complete"
echo -e "------------------------------\n"
echo -e "  1) Today's Jpilot appointments (and Todos) exported to $outputfile."
echo    "     (NB. Todos are converted to \"appointments\" - all are set for $todostarttime)"
echo -e "  2) Ready to (manually) import into Thunderbird's Lightning (calendar) add-on."
echo -e "\n     To import, go into Thunderbird, then choose:"
echo -e "     - Events and Tasks"
echo -e "     - Import..."
echo -e "     - at the bottom right, change display from (*.ics) to (*.csv)"
echo -e "     - navigate to $HOME"
echo -e "     - select jpilot2lightning.csv"
echo -e "     - click Open"
echo -e "  Done! Today's appointments should now be in your Calendar"
echo -e "\nNote:"
echo  "----"
echo -e "If you're (re-)updating today's appointments in Lightning from Jpilot, to avoid duplicate entries,\nyou may want to delete Today's events in Lightning before you do the import again."
echo -e "\nAn easy way to do that is, in Thunderbird:\n  i) go to your Calendar tab,\n ii) change the display to Today's Events,\niii) press CTRL-A to select all entries,\n iv) then hit Delete."

# --------------------- end of jpilot2lightning ------------------------

May someone else find this useful!

Feedback welcome.

(p.s. this bash stuff is pretty addictive :D )
Attachments
jpilot2lightning.tar.gz
Extract jpilot2lightning into somewhere like /root/my-applications/bin, make sure it's executable, and you're ready to go.
(3.23 KiB) Downloaded 255 times
Post Reply