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.sh [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.sh(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
96 echo "# coopanet configuration">$CCPATH/config
97 echo "COOPLOGIN=$COOPLOGIN">>$CCPATH/config
98 echo "COOPPASSWORD=$COOPPASSWORD">>$CCPATH/config
100 echo "config writen to $CCPATH/config" >&2
101 crontab -l | grep -v ccoop-update.sh || echo "$(($RANDOM%60)) 6 * * * $PWD/`dirname $0`/ccoop-update.sh -q" | crontab -
102 echo "crontab installed" >&2
105 function dounconfig() {
107 if [[ -e $CCPATH/config ]]; then
109 echo "$CCPATH/config deleted">&2
112 if crontab -l | grep ccoop-update.sh ; then
113 crontab -l | grep -v ccoop-update.sh | crontab -
114 echo "crontab uninstalled" >&2
117 if [[ -z "$ok" ]]; then
118 echo "Allready uninstalled." >&2
123 function fetchall() {
125 if [[ -n "$log" ]]; then
129 inform "WGET login form"
130 fetch https://www.coopanet.com/banque/cpt/
132 inform "WGET bql/connexion.do"
133 fetch "--post-data=site=C&codeUtil=$COOPLOGIN&motPasse=$COOPPASSWORD&pbValider=Valider" https://www.coopanet.com/banque/cpt/bql/connexion.do
135 inform "WGET cpt/connexion.do"
136 fetch https://www.coopanet.com/banque/cpt/cpt/connexion.do
138 if grep -q "Vous venez de quitter COOP@NET" $TMP; then
139 echo "Can't log in. Check password" >&2
143 accountsId=(`grep numeroExterne $TMP | grep hidden | cut -d \" -f 6`)
144 accountsNames=(`grep "<td style=\"width:9%;\">" $TMP | cut -d ">" -f 2 | cut -d "<" -f 1`)
145 accountsBalances=(`grep "Consulter votre relevé" $TMP | cut -d ">" -f 3 | cut -d "<" -f 1`)
148 for idx in ${!accountsId[@]}; do
149 account=${accountsId[$idx]}
150 name=${accountsNames[$idx]}
151 balance=${accountsBalances[$idx]}
152 inform "$name $account $balance"
154 inform "WGET situationcomptes.do"
155 fetch "--post-data=numeroExterne=$account&typeAction=2&btAction=OK" https://www.coopanet.com/banque/cpt/cpt/situationcomptes.do
157 dateFin=`grep dateFin $TMP | cut -d \" -f 10`
158 inform "WGET selectiontelechargement.do"
159 fetch "--post-data=format=3&dateDebut=01/01/2001&dateFin=$dateFin&btValider=Valider" https://www.coopanet.com/banque/cpt/cpt/selectiontelechargement.do
161 inform "WGET telechargement.do"
162 ppath=`grep path $TMP | cut -d \" -f 6`
163 fetch "--post-data=path=$ppath&zip=2&btConfirmer=Confirmer" https://www.coopanet.com/banque/cpt/cpt/telechargement.do
164 tail -n +2 $TMP > $CCPATH/$account.`date +%Y%m%d`.csv
165 rm -f $CCPATH/$account.last.csv
166 ln -s $CCPATH/$account.`date +%Y%m%d`.csv $CCPATH/$account.last.csv
170 inform "WGET reconnect.do"
171 fetch https://www.coopanet.com/banque/cpt/communs/reconnect.do
174 rm -f $COOKIEJAR $TMP
178 # That function updates $account.csv files, adding the latest missing entries
179 # It dumps on stdout the new lines
180 function updatehistory() {
181 inform "Updating complete csv files"
182 # merge values in $account.csv
183 for idx in ${!accountsId[@]}; do
184 account=${accountsId[$idx]}
185 name=${accountsNames[$idx]}
186 balance=${accountsBalances[$idx]}
188 # get new lines into $account.new.csv
189 diff -Nau $CCPATH/$account.last.csv $CCPATH/$account.csv | grep "^-" | grep -v "^---" | cut -c 2- > $CCPATH/$account.new.csv
192 cat $CCPATH/$account.new.csv
194 cat $CCPATH/$account.csv >>$CCPATH/$account.new.csv
195 mv $CCPATH/$account.new.csv $CCPATH/$account.csv
200 inform "Updating feeds"
201 for idx in ${!accountsId[@]}; do
202 account=${accountsId[$idx]}
203 name=${accountsNames[$idx]}
204 balance=${accountsBalances[$idx]}
206 "`dirname $0`/ccoop-atomize.py" "$account" "$name" "$balance" > $CCPATH/$account.atom
211 umask 0077 # make sure the generated files are only readable by the user
213 if [[ -n $isconfig ]]; then
216 if [[ -n $isunconfig ]]; then
220 # Make sure our temporary files are deleted on error and on exit
221 trap "rm -f $COOKIEJAR $TMP; exit" INT TERM EXIT
223 if [[ `ls -l $CCPATH/config | cut -c 8-10` != "---" ]]; then
224 echo "Security warning: config file should be chmod o-rwx" >&2
226 if [[ ! -r $CCPATH/config ]]; then
227 echo "Use '%0 --config'"
229 source $CCPATH/config
231 if [[ -n $log ]]; then
232 inform "Warning, $LOGFILE will contain sensitive information. You should delete it."