From 634bbe2edf917da75a753dc82b9f9ef8d1e43b5a Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Fri, 13 May 2016 13:25:19 +0200 Subject: [PATCH] initial commit --- README.md | 23 ++++++++++ bliep.py | 42 ++++++++++++++++++ index.php | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++ js/.gitkeep | 0 s.css | 7 +++ 5 files changed, 193 insertions(+) create mode 100644 README.md create mode 100755 bliep.py create mode 100644 index.php create mode 100644 js/.gitkeep create mode 100644 s.css diff --git a/README.md b/README.md new file mode 100644 index 0000000..9643033 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +bliep +===== + +### Installatie instructies +Beide todo's zijn gemarkeerd met `FIXME` +- Wachtwoord invoeren bij `bliep.py` en een cronjob ervoor maken + (`@daily /home/mart/bin/bliep.py`) +- Database info aanpassen bij `index.php` +- Javascript kopieren van gnuplot folder(`/usr/share/gnuplot/gnuplot/#.#/js`) + naar `js`. + +### Database +``` +CREATE TABLE IF NOT EXISTS `data` ( + `amount` int(11) NOT NULL, + `date` datetime NOT NULL, + `description` text NOT NULL, + `uid` bigint(64) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +ALTER TABLE `data` + ADD PRIMARY KEY (`uid`), ADD UNIQUE KEY `uid` (`uid`); +``` diff --git a/bliep.py b/bliep.py new file mode 100755 index 0000000..d178b01 --- /dev/null +++ b/bliep.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +import requests +import mysql.connector +import hashlib + +apiurl = 'https://bliep-main.bliep.nl/api'; + +r = requests.post('{}/authenticate'.format(apiurl), json={ + "username": + "mart@martlubbers.net", + "password": +#FIXME + "HIER JE WACHWOORD INVOEREN", + "client_id": + "393977lIRGDPPGSNt2z5Hhj7VdLu0GEDh7pCKC64Jht2id5TyBNr7DSJjGS7jIAF"}) +token = 'Bearer {}'.format(r.json()['access_token']) +r = requests.get( + '{}/profile/credit/history'.format(apiurl), + params={'limit': '200', 'offset': '0'}, + headers={'Authorization': token}) + +try: + cnx = mysql.connector.connect( + user='bliep', password='bliep123', host='localhost', database='bliep') +except mysql.connector.Error as err: + print(err) + exit() +cursor = cnx.cursor() +for i in r.json()['data']['items']: + q = "INSERT IGNORE INTO data (amount, date, description, uid) "\ + "VALUES (%s, FROM_UNIXTIME(%s), %s, %s)" + amount = int(float(i['amount'])*100) + date = int(i['date']/1000) + res = cursor.execute(q, ( + amount, + date, + i['description'], + int(str(date)+str(abs(amount))))) +cnx.commit() +cursor.close() +cnx.close() diff --git a/index.php b/index.php new file mode 100644 index 0000000..f5164ff --- /dev/null +++ b/index.php @@ -0,0 +1,121 @@ + + +Bliep + + + + + + + +
No support for HTML 5 canvas element
+
+ array("pipe", "r"), + 1 => array("pipe", "w"), + 2 => array("pipe", "w") +); +$process = proc_open('gnuplot', $descriptorspec, $pipes); +$tmpfile = tempnam("/tmp", "bliep"); +$tmphandle = fopen($tmpfile, "w"); +$gnuplot =<< 0.0, + "bel" => 0.0, + "int" => 0.0, + "opw" => 0.0 + ); + } + $row[1] = floatval(intval($row[1])*0.01); + if(strpos($row[2], "sms") !== FALSE){ + $data[$row[0]]["sms"] += -$row[1]; + } else if (strpos($row[2], "Bellen") !== FALSE){ + preg_match_all('!\d+!', $row[2], $matches); + $nulnegen = strstr("0900", $matches[0][0]); + if($nulnegen === FALSE || $nulnegen != 0){ + if($matches[0][0][0] === "0"){ + $data[$row[0]]["bel"] += -$row[1]; + } + } + } else if (strpos($row[2], "pwaardering") !== FALSE){ + $data[$row[0]]["opw"] += $row[1]; + } else { + $data[$row[0]]["int"] += -$row[1]; + } +} +foreach($data as $key => $row){ + fwrite($tmphandle, sprintf("%d\t%.2f\t%.2f\t%.2f\n", + intval($key), $row["int"], $row["bel"], $row["sms"])); +} +fclose($tmphandle); +fwrite($pipes[0], sprintf(" +plot '%s' using 2 t 'internet',\ +'' using 3 t 'bellen',\ +'' using 4 t 'sms'\ +\n", $tmpfile)); +fwrite($pipes[0], "quit\n"); +fclose($pipes[0]); +$javascript = stream_get_contents($pipes[1]); +fclose($pipes[1]); +echo "err: " . stream_get_contents($pipes[2]); +fclose($pipes[2]); +$return_value = proc_close($process); +unlink($tmpfile); + +$r = mysqli_query($conn, " +SELECT amount, date, description +FROM data +ORDER BY date DESC LIMIT 25"); +if($r === FALSE){ + die(sprintf("Query error: %s\n", mysqli_error($conn))); +} +echo "\n\n"; +echo "\n"; +while($row = mysqli_fetch_array($r)){ + printf("\n", + $row[1], intval($row[0])*0.01, $row[2]); +} +echo "
DateAmountDescription
%s€%.2f%s
\n"; +mysqli_close($conn); +echo ""; +?> + + + diff --git a/js/.gitkeep b/js/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/s.css b/s.css new file mode 100644 index 0000000..f4bf54d --- /dev/null +++ b/s.css @@ -0,0 +1,7 @@ +table, th, td { + border: 1px solid black; + border-collapse: collapse; +} +td { + padding: 15px; +} -- 2.20.1