#!/bin/sh # pulls client time from freshbooks and sends to harvest. # note you still have to submit the time in harvest. TOK= TODAY=`date +"%Y-%m-%d"` LAST_MONDAY=`date -d "last monday" +"%Y-%m-%d"` FB_PROJECT_IDS="1 2 3" # get data from fb, up to one week ago mkdir -p tmp for i in $FB_PROJECT_IDS; do curl -s -u $TOK:x https://yourfb.freshbooks.com/api/2.1/xml-in -d " \ \ \ $LAST_MONDAY \ $i\ 1500\ " > tmp/$i.time done # translate xml for i in $FB_PROJECT_IDS; do # proj translate sed ' s/>1<\/pro/ type="integer">123<\/pro/g s/>2<\/pro/ type="integer">124<\/pro/g s/>3<\/pro/ type="integer">125<\/pro/g' < tmp/$i.time > tmp/$i.tim2 # then task translate sed ' s/>1<\/task/ type="integer">10001<\/task/g s/>2<\/task/ type="integer">10002<\/task/g s/>3<\/task/ type="integer">10003<\/task/g s/>4<\/task/ type="integer">10004<\/task/g s/>5<\/task/ type="integer">10005<\/task/g s/>6<\/task/ type="integer">10006<\/task/g' < tmp/$i.tim2 > tmp/$i.tim3 # date sed ' s/<\/date>/<\/spent_at>/g s///g' < tmp/$i.tim3 > tmp/$i.tim4 done for i in $FB_PROJECT_IDS; do # remove extra lines in each entry grep -v '' tmp/$i.tim4 | grep -v '' | grep -v '/time_entry>' | grep -v 'response>' |grep -v 'time_entries>' | grep -v ' tmp/$i.tim5 echo '' >> tmp/$i.tim5 done mkdir -p toadd # split into sep files for post for i in $FB_PROJECT_IDS; do perl -e ' $/ = undef; @chunks = split(/\s+/m,<>); for ($j = 0; $j < $#chunks; $j++) { next if (length($chunks[$j]) < 2); #print($j,length($chunks[$j]),"\n"); open (FILE, ">toadd/'$i'.toadd.$j") or die $!; print FILE "\n"; print FILE $chunks[$j]."\n"; print FILE "\n"; } ' < tmp/$i.tim5 done #post to harvest for file in toadd/*; do POST_DATA=`cat $file`; #echo $POST_DATA curl http://yourharvest.harvestapp.com/daily/add -H 'Accept: application/xml' -H 'Content-Type: application/xml' -u harvestuser:harvestpass --data "$POST_DATA" done #cleanup rm toadd/* rm tmp/*