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"
29 #echo " --target=<filename> Set target filename. Defaults to $target_file"
30 #echo "See man ccoop.sh(1) for documentation."
58 echo "Unknown option $_OPT"
65 # That function prints messages to stderr, unless quiet is set
66 # It logs into $LOGFILE if log is set, no matter whether quiet is set or not
68 if [ -n "$quiet" ]; then
69 if [ -n "$log" ]; then
70 echo $@ >> $LOGFILE >&2
73 if [ -n "$log" ]; then
74 echo $@ | tee -a $LOGFILE >&2
82 wget -q --load-cookies $COOKIEJAR --save-cookies $COOKIEJAR --keep-session-cookies $@ -O - | iconv -f ISO-8859-1 -t UTF-8 > $TMP
83 if [ -n "$log" ]; then
88 function makeconfig() {
89 read -p "Coopanet login: " COOPLOGIN
90 read -p "Coopanet password: " -s COOPPASSWORD
91 echo "# coopanet configuration">$CCPATH/config
92 echo "COOPLOGIN=$COOPLOGIN">>$CCPATH/config
93 echo "COOPPASSWORD=$COOPPASSWORD">>$CCPATH/config
95 echo "config file writen" >&2
100 if [[ -n "$log" ]]; then
104 inform "WGET login form"
105 fetch https://www.coopanet.com/banque/cpt/
107 inform "WGET bql/connexion.do"
108 fetch "--post-data=site=C&codeUtil=$COOPLOGIN&motPasse=$COOPPASSWORD&pbValider=Valider" https://www.coopanet.com/banque/cpt/bql/connexion.do
110 inform "WGET cpt/connexion.do"
111 fetch https://www.coopanet.com/banque/cpt/cpt/connexion.do
113 if grep -q "Vous venez de quitter COOP@NET" $TMP; then
114 echo "Can't log in. Check password" >&2
118 accountsId=(`grep numeroExterne $TMP | grep hidden | cut -d \" -f 6`)
119 accountsNames=(`grep "<td style=\"width:9%;\">" $TMP | cut -d ">" -f 2 | cut -d "<" -f 1`)
120 accountsBalances=(`grep "Consulter votre relevé" $TMP | cut -d ">" -f 3 | cut -d "<" -f 1`)
123 for idx in ${!accountsId[@]}; do
124 account=${accountsId[$idx]}
125 name=${accountsNames[$idx]}
126 balance=${accountsBalances[$idx]}
127 inform "$name $account $balance"
129 inform "WGET situationcomptes.do"
130 fetch "--post-data=numeroExterne=$account&typeAction=2&btAction=OK" https://www.coopanet.com/banque/cpt/cpt/situationcomptes.do
132 dateFin=`grep dateFin $TMP | cut -d \" -f 10`
133 inform "WGET selectiontelechargement.do"
134 fetch "--post-data=format=3&dateDebut=01/01/2001&dateFin=$dateFin&btValider=Valider" https://www.coopanet.com/banque/cpt/cpt/selectiontelechargement.do
136 inform "WGET telechargement.do"
137 ppath=`grep path $TMP | cut -d \" -f 6`
138 fetch "--post-data=path=$ppath&zip=2&btConfirmer=Confirmer" https://www.coopanet.com/banque/cpt/cpt/telechargement.do
139 tail -n +2 $TMP > $CCPATH/$account.`date +%Y%m%d`.csv
140 rm -f $CCPATH/$account.last.csv
141 ln -s $CCPATH/$account.`date +%Y%m%d`.csv $CCPATH/$account.last.csv
145 inform "WGET reconnect.do"
146 fetch https://www.coopanet.com/banque/cpt/communs/reconnect.do
149 rm -f $COOKIEJAR $TMP
153 # That function updates $account.csv files, adding the latest missing entries
154 # It dumps on stdout the new lines
155 function updatehistory() {
156 inform "Updating complete csv files"
157 # merge values in $account.csv
158 for idx in ${!accountsId[@]}; do
159 account=${accountsId[$idx]}
160 name=${accountsNames[$idx]}
161 balance=${accountsBalances[$idx]}
163 # get new lines into $account.new.csv
164 diff -Nau $CCPATH/$account.last.csv $CCPATH/$account.csv | grep "^-" | grep -v "^---" | cut -c 2- > $CCPATH/$account.new.csv
167 cat $CCPATH/$account.new.csv
169 cat $CCPATH/$account.csv >>$CCPATH/$account.new.csv
170 mv $CCPATH/$account.new.csv $CCPATH/$account.csv
175 inform "Updating feeds"
176 for idx in ${!accountsId[@]}; do
177 account=${accountsId[$idx]}
178 name=${accountsNames[$idx]}
179 balance=${accountsBalances[$idx]}
181 "`dirname $0`/ccoop-atomize.py" "$account" "$name" "$balance" > $CCPATH/$account.atom
186 umask 0077 # make sure the generated files are only readable by the user
188 if [[ -n $ismakeconfig ]]; then
192 # Make sure our temporary files are deleted on error and on exit
193 trap "rm -f $COOKIEJAR $TMP; exit" INT TERM EXIT
195 if [[ `ls -l $CCPATH/config | cut -c 8-10` != "---" ]]; then
196 echo "Security warning: config file should be chmod o-rwx" >&2
198 source $CCPATH/config
200 if [[ -n $log ]]; then
201 inform "Warning, $LOGFILE will contain sensitive information. You should delete it."