3 # Crédit Coopératif account tools
5 # It fetch csv file and merge them to previous ones
6 # It also generates a local file suitable for a feed reader
8 # Depends on wget, iconv
13 LOGFILE=$CCPATH/coop.log # unsued unless --log option is used
14 COOKIEJAR=$CCPATH/cookiejar # cookies, deleted on exit
15 TMP=$CCPATH/lastpage.html # last html page, deleted on exit
24 echo "Usage: ccoop-update [options]"
25 echo " -h|--help Display that help"
26 echo " -q|--quiet Don't display informationnal messages"
27 echo " --log Log everything to $LOGFILE"
28 echo " --config Setup a config file and install cron entry"
29 echo " --unconfig Remove config file and remove cron entry"
30 #echo " --target=<filename> Set target filename. Defaults to $target_file"
31 #echo "See man ccoop-update(1) for documentation."
63 echo "Unknown option $_OPT"
70 # That function prints messages to stderr, unless quiet is set
71 # It logs into $LOGFILE if log is set, no matter whether quiet is set or not
73 if [ -n "$quiet" ]; then
74 if [ -n "$log" ]; then
75 echo $@ >> $LOGFILE >&2
78 if [ -n "$log" ]; then
79 echo $@ | tee -a $LOGFILE >&2
87 wget -q --load-cookies $COOKIEJAR --save-cookies $COOKIEJAR --keep-session-cookies $@ -O - | iconv -f ISO-8859-1 -t UTF-8 > $TMP
88 if [ -n "$log" ]; then
94 read -p "Coopanet login: " COOPLOGIN
95 read -p "Coopanet password: " -s COOPPASSWORD
97 echo "# coopanet configuration">$CCPATH/config
98 echo "COOPLOGIN=$COOPLOGIN">>$CCPATH/config
99 echo "COOPPASSWORD=$COOPPASSWORD">>$CCPATH/config
101 echo "config writen to $CCPATH/config" >&2
102 if [[ "${0:0:1}"=="/" ]]; then
108 crontab -l | grep -v ccoop-update || echo "$(($RANDOM%60)) 6 * * * $command -q" | crontab -
109 echo "crontab installed" >&2
112 function dounconfig() {
114 if [[ -e $CCPATH/config ]]; then
116 echo "$CCPATH/config deleted">&2
119 if crontab -l | grep -q ccoop-update ; then
120 crontab -l | grep -v ccoop-update | crontab -
121 echo "crontab uninstalled" >&2
124 if [[ -z "$ok" ]]; then
125 echo "Allready uninstalled." >&2
127 if [[ -e "$CCPATH" ]]; then
128 echo "Password cleared. Remove ~/ccoop manually if you want to erase history." >&2
133 function fetchall() {
135 if [[ -n "$log" ]]; then
139 inform "WGET login form"
140 fetch https://www.coopanet.com/banque/cpt/
142 inform "WGET bql/connexion.do"
143 fetch "--post-data=site=C&codeUtil=$COOPLOGIN&motPasse=$COOPPASSWORD&pbValider=Valider" https://www.coopanet.com/banque/cpt/bql/connexion.do
145 inform "WGET cpt/connexion.do"
146 fetch https://www.coopanet.com/banque/cpt/cpt/connexion.do
148 if grep -q "Vous venez de quitter COOP@NET" $TMP; then
149 echo "Can't log in. Check password" >&2
153 accountsId=(`grep numeroExterne $TMP | grep hidden | cut -d \" -f 6`)
154 accountsNames=(`grep "<td style=\"width:9%;\">" $TMP | cut -d ">" -f 2 | cut -d "<" -f 1`)
155 accountsBalances=(`grep "Consulter votre relevé" $TMP | cut -d ">" -f 3 | cut -d "<" -f 1`)
158 for idx in ${!accountsId[@]}; do
159 account=${accountsId[$idx]}
160 name=${accountsNames[$idx]}
161 balance=${accountsBalances[$idx]}
162 inform "$name $account $balance"
164 inform "WGET situationcomptes.do"
165 fetch "--post-data=numeroExterne=$account&typeAction=2&btAction=OK" https://www.coopanet.com/banque/cpt/cpt/situationcomptes.do
167 dateFin=`grep dateFin $TMP | cut -d \" -f 10`
168 inform "WGET selectiontelechargement.do"
169 fetch "--post-data=format=3&dateDebut=01/01/2001&dateFin=$dateFin&btValider=Valider" https://www.coopanet.com/banque/cpt/cpt/selectiontelechargement.do
171 inform "WGET telechargement.do"
172 ppath=`grep path $TMP | cut -d \" -f 6`
173 fetch "--post-data=path=$ppath&zip=2&btConfirmer=Confirmer" https://www.coopanet.com/banque/cpt/cpt/telechargement.do
174 tail -n +2 $TMP > $CCPATH/$account.`date +%Y%m%d`.csv
175 rm -f $CCPATH/$account.last.csv
176 ln -s $CCPATH/$account.`date +%Y%m%d`.csv $CCPATH/$account.last.csv
180 inform "WGET reconnect.do"
181 fetch https://www.coopanet.com/banque/cpt/communs/reconnect.do
184 rm -f $COOKIEJAR $TMP
188 # That function updates $account.csv files, adding the latest missing entries
189 # It dumps on stdout the new lines
190 function updatehistory() {
191 inform "Updating complete csv files"
192 # merge values in $account.csv
193 for idx in ${!accountsId[@]}; do
194 account=${accountsId[$idx]}
195 name=${accountsNames[$idx]}
196 balance=${accountsBalances[$idx]}
198 # get new lines into $account.new.csv
199 diff -Nau $CCPATH/$account.last.csv $CCPATH/$account.csv | grep "^-" | grep -v "^---" | cut -c 2- > $CCPATH/$account.new.csv
202 cat $CCPATH/$account.new.csv
204 cat $CCPATH/$account.csv >>$CCPATH/$account.new.csv
205 mv $CCPATH/$account.new.csv $CCPATH/$account.csv
210 inform "Updating feeds"
211 if [[ "${0:0:1}"=="/" ]]; then
215 path="$PWD/`dirname $0`"
217 for idx in ${!accountsId[@]}; do
218 account=${accountsId[$idx]}
219 name=${accountsNames[$idx]}
220 balance=${accountsBalances[$idx]}
222 "$path/ccoop-atomize" "$account" "$name" "$balance" > $CCPATH/$account.atom
227 umask 0077 # make sure the generated files are only readable by the user
229 if [[ -n $isconfig ]]; then
232 if [[ -n $isunconfig ]]; then
236 # Make sure our temporary files are deleted on error and on exit
237 trap "rm -f $COOKIEJAR $TMP; exit" INT TERM EXIT
239 if [[ ! -r $CCPATH/config ]]; then
240 echo "Use '$0 --config'"
243 if [[ `ls -l $CCPATH/config | cut -c 8-10` != "---" ]]; then
244 echo "Security warning: config file should be chmod o-rwx" >&2
246 source $CCPATH/config
248 if [[ -n $log ]]; then
249 inform "Warning, $LOGFILE will contain sensitive information. You should delete it."