diff --git a/pacer2influxdb.py b/pacer2influxdb.py new file mode 100755 index 0000000..b3f2f44 --- /dev/null +++ b/pacer2influxdb.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +from influxdb import InfluxDBClient +import datetime +import time +import sqlite3 as lite +import argparse +import json + +def main(host='localhost', port=8086, user='root', password='root', dbname='demo', dbfile='demo.db'): + """Instantiate the connection to the InfluxDB client.""" + + db = InfluxDBClient(host, port, user, password, dbname) + lastentry = db.query('SELECT LAST("steps") FROM "steps"') + if lastentry != None: + points = lastentry.get_points('steps') + lastdate = list(points)[0]['time'] + ts = time.mktime(datetime.datetime.strptime(lastdate, '%Y-%m-%dT%H:%M:%SZ').timetuple()) + + print("last entry is %s, ts is %s" % (lastdate,ts)) + + con = lite.connect(dbfile) + with con: + + cur = con.cursor() + cur.execute("SELECT recordedForDate,steps,distanceInMeters,activeTimeInSeconds,calories FROM dailyActivityLog WHERE recordedForDate > %s ORDER BY recordedForDate DESC" %ts) + + while True: + + row = cur.fetchone() + if row == None: + break + mytime = datetime.datetime.fromtimestamp(row[0]).strftime('%Y-%m-%dT%H:%M:%SZ') + data = [ + {"measurement":"steps", + "time":mytime, + "fields": { + "steps":row[1], + "distanceInMeters":row[2], + "activeTimeInSeconds":row[3], + "calories":row[4] + } + } + ] + + print(data) + db.write_points(data) + +def parse_args(): + """Parse the args from main.""" + parser = argparse.ArgumentParser( + description='Export Pacer data to InfluxDB') + parser.add_argument('--host', type=str, required=False, + default='localhost', + help='hostname of InfluxDB http API') + parser.add_argument('--user', type=str, required=False, + default='root', + help='username for InfluxDB http API') + parser.add_argument('--password', type=str, required=False, + default='root', + help='passworid for InfluxDB http API') + parser.add_argument('--port', type=int, required=False, default=8086, + help='port of InfluxDB http API') + parser.add_argument('--dbname', type=str, required=True, default='demo', + help='InfluxDB database name') + parser.add_argument('--dbfile', type=str, required=True, default='demo.db', + help='sqlite (pacer) database name') + return parser.parse_args() + + +if __name__ == '__main__': + args = parse_args() +main(host=args.host, port=args.port, user=args.user, password=args.password, dbname=args.dbname, dbfile=args.dbfile)