Moved from wget to curl, because wget mangle the cookies starting with double quotes...
[ccoop.git] / ccoop-update
index 66ed5973f4a94533f3a029c252a12d0e455a8a60..47756ff2332e2150c383e237edf1d5a953238959 100755 (executable)
@@ -10,7 +10,7 @@
 set -e
 
 export CCPATH=~/ccoop
-LOGFILE=$CCPATH/coop.log # unsued unless --log option is used
+LOGFILE=$CCPATH/ccoop.log # unsued unless --log option is used
 COOKIEJAR=$CCPATH/cookiejar # cookies, deleted on exit
 TMP=$CCPATH/lastpage.html # last html page, deleted on exit
 
@@ -84,7 +84,7 @@ function inform() {
 }
 
 function fetch() {
-    wget -q --load-cookies $COOKIEJAR --save-cookies $COOKIEJAR --keep-session-cookies $@ -O - | iconv -f ISO-8859-1 -t UTF-8 > $TMP
+    curl --cookie  $COOKIEJAR --cookie-jar $COOKIEJAR --location "$@" | iconv -f ISO-8859-1 -t UTF-8 > $TMP
     if [ -n "$log" ]; then
         cat $TMP >> $LOGFILE
     fi
@@ -99,7 +99,13 @@ function doconfig() {
     echo "COOPPASSWORD=$COOPPASSWORD">>$CCPATH/config
     echo >&2
     echo "config writen to $CCPATH/config" >&2
-    crontab -l | grep -v ccoop-update || echo "$(($RANDOM%60)) 6     * * *   $PWD/`dirname $0`/ccoop-update -q" | crontab -
+    if [[ "${0:0:1}"=="/" ]]; then
+        # absolute path
+        command="$0"
+    else
+        command="$PWD/$0"
+    fi
+    crontab -l | grep -v ccoop-update || echo "$(($RANDOM%60)) 6     * * *   $command -q" | crontab -
     echo "crontab installed" >&2
 }
 
@@ -130,22 +136,17 @@ function fetchall() {
         echo> $LOGFILE
     fi
     
-    inform "WGET login form"
-    fetch https://www.coopanet.com/banque/cpt/
-    
-    inform "WGET bql/connexion.do"
-    fetch "--post-data=site=C&codeUtil=$COOPLOGIN&motPasse=$COOPPASSWORD&pbValider=Valider" https://www.coopanet.com/banque/cpt/bql/connexion.do
-    
-    inform "WGET cpt/connexion.do"
-    fetch https://www.coopanet.com/banque/cpt/cpt/connexion.do
-    if grep -q "Vous venez de quitter  COOP@NET" $TMP; then
+    inform "CURL /banque/sso/co/connexionsec.do"
+    fetch --data "codeUtil=$COOPLOGIN&motPasse=$COOPPASSWORD&identType=MDP&pbValider=Valider" https://www.coopanet.com/banque/sso/co/connexionsec.do
+
+    if grep -q "Choisissez votre mode d" $TMP; then
         echo "Can't log in. Check password" >&2
+        exit 1
     fi
 
     IFS=$'\n'
-    accountsId=(`grep numeroExterne $TMP | grep hidden | cut -d \" -f 6`)
-    accountsNames=(`grep "<td style=\"width:9%;\">" $TMP | cut -d ">" -f 2 | cut -d "<" -f 1`)
+    accountsId=(`grep numeroExterne $TMP | grep hidden | cut -d \" -f 14`)
+    accountsNames=(`grep "<TD class=txt width=\"99%\">" $TMP |  cut -d \> -f 6  | cut -d "<" -f 1`)
     accountsBalances=(`grep "Consulter votre relevé" $TMP | cut -d ">" -f 3 | cut -d "<" -f 1`)
     unset IFS
 
@@ -155,24 +156,24 @@ function fetchall() {
         balance=${accountsBalances[$idx]}
         inform "$name $account $balance"
     
-        inform "WGET situationcomptes.do"
-        fetch "--post-data=numeroExterne=$account&typeAction=2&btAction=OK" https://www.coopanet.com/banque/cpt/cpt/situationcomptes.do
+        inform "CURL situationcomptes.do"
+        fetch --data "numeroExterne=$account&typeAction=2&btAction=OK" https://www.coopanet.com/banque/cpt/cpt/situationcomptes.do
         
         dateFin=`grep dateFin $TMP | cut -d \" -f 10`
-        inform "WGET selectiontelechargement.do"
-        fetch "--post-data=format=3&dateDebut=01/01/2001&dateFin=$dateFin&btValider=Valider" https://www.coopanet.com/banque/cpt/cpt/selectiontelechargement.do
+        inform "CURL selectiontelechargement.do"
+        fetch --data "format=3&dateDebut=01/01/2001&dateFin=$dateFin&btValider=Valider" https://www.coopanet.com/banque/cpt/cpt/selectiontelechargement.do
     
-        inform "WGET telechargement.do"
+        inform "CURL telechargement.do"
         ppath=`grep path $TMP | cut -d \" -f 6`
-        fetch "--post-data=path=$ppath&zip=2&btConfirmer=Confirmer" https://www.coopanet.com/banque/cpt/cpt/telechargement.do
+        fetch --data "path=$ppath&zip=2&btConfirmer=Confirmer" https://www.coopanet.com/banque/cpt/cpt/telechargement.do
         tail -n +2 $TMP > $CCPATH/$account.`date +%Y%m%d`.csv
         rm -f $CCPATH/$account.last.csv
         ln -s $CCPATH/$account.`date +%Y%m%d`.csv $CCPATH/$account.last.csv
     done
     
     # logout
-    inform "WGET reconnect.do"
-    fetch https://www.coopanet.com/banque/cpt/communs/reconnect.do
+    inform "CURL /banque/sso/ssologout.do"
+    fetch https://www.coopanet.com/banque/sso/ssologout.do
     
     # delete tmp files
     rm -f $COOKIEJAR $TMP
@@ -200,14 +201,20 @@ function updatehistory() {
     done
 }
 
-function atomize() {
+function makefeed() {
     inform "Updating feeds"
+    if [[ "${0:0:1}"=="/" ]]; then
+        # absolute path
+        path="`dirname $0`"
+    else
+        path="$PWD/`dirname $0`"
+    fi
     for idx in ${!accountsId[@]}; do
         account=${accountsId[$idx]}
         name=${accountsNames[$idx]}
         balance=${accountsBalances[$idx]}
 
-        "`dirname $0`/ccoop-atomize" "$account" "$name" "$balance" > $CCPATH/$account.atom
+        "$path/ccoop-feeder" "$account" "$name" "$balance" > $CCPATH/$account.atom
     done
 }
 
@@ -240,5 +247,5 @@ fi
 
 fetchall
 updatehistory
-atomize
+makefeed