initial commit
authorMart Lubbers <mart@martlubbers.net>
Fri, 13 May 2016 11:25:19 +0000 (13:25 +0200)
committerMart Lubbers <mart@martlubbers.net>
Fri, 13 May 2016 11:25:19 +0000 (13:25 +0200)
README.md [new file with mode: 0644]
bliep.py [new file with mode: 0755]
index.php [new file with mode: 0644]
js/.gitkeep [new file with mode: 0644]
s.css [new file with mode: 0644]

diff --git a/README.md b/README.md
new file mode 100644 (file)
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 (executable)
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 (file)
index 0000000..f5164ff
--- /dev/null
+++ b/index.php
@@ -0,0 +1,121 @@
+<html>
+<head>
+<title>Bliep</title>
+<link rel="stylesheet" type="text/css" href="s.css" />
+<script src="js/canvastext.js"></script>
+<script src="js/gnuplot_common.js"></script>
+<script src="js/gnuplot_mouse.js"></script>
+</head>
+<body>
+<canvas id="bliep" width=900 height=600>
+<div id="err_msg">No support for HTML 5 canvas element</div>
+</canvas>
+<?php
+error_reporting(E_ALL);
+ini_set('display_errors', '1');
+
+//FIXME
+$conn = mysqli_connect("localhost", "USER", "PASSWORD", "bliep");
+if (!$conn) {
+       die(sprintf("Connect error: %s\n", mysqli_connect_error()));
+}
+
+$descriptorspec = array(
+   0 => 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 =<<<GNUPLOT
+set term canvas size 900,600 name 'bliep'
+set xlabel "Weeknumber"
+set ylabel "Count"
+set xrange [-0.5:*]
+set xtics 1
+set ytics 0.25
+set style data histograms
+set style histogram rowstacked
+set boxwidth 1 relative
+set style fill solid 1.0 border -1
+set title "Telefoon gebruik"
+
+GNUPLOT;
+fwrite($pipes[0], $gnuplot);
+$r = mysqli_query($conn, "
+SELECT WEEK(date), amount, description
+FROM data 
+WHERE YEAR(date)=2016
+");
+if($r === FALSE){
+       die(sprintf("Query error: %s\n", mysqli_error($conn)));
+}
+$data = array();
+while($row = mysqli_fetch_array($r)){
+       if(!array_key_exists($row[0], $data)){
+               $data[$row[0]] = array(
+                       "sms" => 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<table>\n";
+echo "<tr><td>Date</td><td>Amount</td><td>Description</td></tr>\n";
+while($row = mysqli_fetch_array($r)){
+       printf("<tr><td>%s</td><td>&euro;%.2f</td><td>%s</td></tr>\n",
+               $row[1], intval($row[0])*0.01, $row[2]);
+}
+echo "</table>\n";
+mysqli_close($conn);
+echo "<script>$javascript</script>";
+?>
+<script>
+bliep()
+</script>
+</body>
+</html>
diff --git a/js/.gitkeep b/js/.gitkeep
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/s.css b/s.css
new file mode 100644 (file)
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;
+}