First version of ccoop
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Mon, 10 Mar 2008 18:45:18 +0000 (18:45 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Mon, 10 Mar 2008 18:45:18 +0000 (18:45 +0000)
ccoop-atomize.py [new file with mode: 0755]
ccoop-update.sh [new file with mode: 0755]

diff --git a/ccoop-atomize.py b/ccoop-atomize.py
new file mode 100755 (executable)
index 0000000..c9bb64c
--- /dev/null
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+
+import sys
+
+account=sys.argv[1]
+name=sys.argv[2]
+balance=sys.argv[3]
+
+lines = file(account+".csv").readlines()
+nlines = len(lines)
+
+print '<?xml version="1.0" encoding="UTF-8"?>'
+print '<feed xmlns="http://www.w3.org/2005/Atom">'
+print "<title>"+name+" "+account+"</title>"
+print '<subtitle type="text">Balance: '+balance+'</subtitle>'
+iline = 0
+for line in lines:
+    line = line[:-1]
+    iline+=1
+    if not line:
+        continue
+    date,libel1,libel2,amount,sign,checkid = line.split(';')
+    date=date[6:]+"-"+date[3:5]+"-"+date[0:2]
+    libel1=libel1.strip()
+    libel2=libel2.strip()
+    amount=amount.strip()
+    sign=sign.strip()
+    print "<entry>"
+    print "   <id>"+str(nlines-iline)+"</id>"
+    print "   <updated>"+date+"</updated>"
+    print "   <title>"+libel1+"</title>"
+    print "   <content type='text'>"
+    print "      "+libel1
+    print "      "+libel2
+    print "      "+sign
+    print "      "+amount
+    print "      "+checkid
+    print "   </content>"
+    print "</entry>"
+
+print "</feed>"
diff --git a/ccoop-update.sh b/ccoop-update.sh
new file mode 100755 (executable)
index 0000000..9fdd8aa
--- /dev/null
@@ -0,0 +1,175 @@
+#!/usr/bin/env bash
+# crédit coopératif fetcher
+# Depends on wget, iconv
+
+set -e
+
+LOGFILE=coop.log # unsued unless --log option is used
+COOKIEJAR=cookiejar
+TMP=lastpage.html
+
+function parsearg() {
+    _OPT="${1%=?*}"
+    _VAL="${1#?*=}"
+}
+
+function usage() {
+    #echo $'\n' $@ $'\n'
+    echo "Usage: coop.sh [options]"
+    echo "  -h|--help             Display that help"
+    echo "  -q|--quiet            Don't display informationnal messages"
+    echo "  -l|--log              Log requests to $LOGFILE"
+    #echo "  --target=<filename>  Set target filename. Defaults to $target_file"
+    #echo "See man curcy-update(1) for documentation."
+}
+
+for arg in "$@"; do
+    parsearg $arg
+
+    case $_OPT in
+    -q|--quiet)
+        quiet=1
+        continue
+    ;;
+    -l|--log)
+        log=1
+        continue
+    ;;
+    -h|--help)
+        usage 
+        exit 0
+    ;;
+    #--target)
+    #    target_file=$_VAL
+    #    continue
+    #;;
+    *)
+        echo "Unknown option $_OPT"
+        usage
+        exit 22
+    ;;
+    esac
+done
+
+# That function prints messages, unless quiet is set
+# It logs into $LOGFILE if log is set, no matter whether quiet is set or not
+function inform() {
+    if [ -n "$quiet" ]; then
+        if [ -n "$log" ]; then
+            echo $@ >> $LOGFILE
+        fi
+    else # not quiet
+        if [ -n "$log" ]; then
+            echo $@ | tee -a $LOGFILE
+        else
+            echo $@
+        fi
+    fi
+}
+
+function fetch() {
+    wget -q --load-cookies $COOKIEJAR --save-cookies $COOKIEJAR --keep-session-cookies $@ -O - | iconv -f ISO-8859-1 -t UTF-8 > $TMP
+    if [ -n "$log" ]; then
+        cat $TMP >> $LOGFILE
+    fi
+}
+
+function fetchall() {
+    echo > $COOKIEJAR
+    echo > $LOGFILE
+    
+    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
+    
+    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`)
+    accountsBalances=(`grep "Consulter votre relevé" $TMP | cut -d ">" -f 3 | cut -d "<" -f 1`)
+    unset IFS
+
+    for idx in ${!accountsId[@]}; do
+        account=${accountsId[$idx]}
+        name=${accountsNames[$idx]}
+        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
+        
+        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 "WGET 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
+        tail -n +2 $TMP > $account.`date +%Y%m%d`.csv
+        rm -f $account.last.csv
+        ln -s $account.`date +%Y%m%d`.csv $account.last.csv
+    done
+    
+    # logout
+    inform "WGET reconnect.do"
+    fetch https://www.coopanet.com/banque/cpt/communs/reconnect.do
+    
+    # delete tmp files
+    rm -f $COOKIEJAR $TMP
+}
+
+
+# That function updates $account.csv files, adding the latest missing entries
+# It dumps on stdout the new lines
+function updatehistory() {
+    inform "Updating complete csv files"
+    # merge values in $account.csv
+    for idx in ${!accountsId[@]}; do
+        account=${accountsId[$idx]}
+        name=${accountsNames[$idx]}
+        balance=${accountsBalances[$idx]}
+
+        # get new lines into $account.new.csv
+        diff -Nau $account.last.csv $account.csv | grep "^-" | grep -v "^---" | cut -c 2- > $account.new.csv
+    
+        # print new entries
+        cat $account.new.csv
+    
+        cat $account.csv >>$account.new.csv
+        mv $account.new.csv $account.csv
+    done
+}
+
+function atomize() {
+    inform "Updating feeds"
+    for idx in ${!accountsId[@]}; do
+        account=${accountsId[$idx]}
+        name=${accountsNames[$idx]}
+        balance=${accountsBalances[$idx]}
+
+        ./ccoop-atomize.py "$account" "$name" "$balance" > $account.atom
+    done
+}
+
+umask 0077 # make sure the generated files are only readable by the user
+
+cd `dirname $0`
+
+if [[ `ls -l config | cut -c 8-10` != "---" ]]; then
+    echo "Security warning file should be chmod o-rwx"
+fi
+source config
+
+
+if [[ -n $log ]]; then
+    inform "Warning, $LOG will contain sensitive information. You should delete it."
+fi
+
+fetchall
+updatehistory
+atomize
+