2019-05-19 18:31:25 +02:00
#!/usr/bin/env python3
from influxdb import InfluxDBClient
import datetime
import time
import sqlite3 as lite
import argparse
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 )
2019-05-19 18:43:00 +02:00
measurements = db . get_list_measurements ( )
2022-02-27 13:07:51 +01:00
ts = 0
lastdate = 0
laststeps = 0
2019-05-19 18:43:00 +02:00
if measurements != [ ] :
lastentry = db . query ( ' SELECT LAST( " steps " ) FROM " steps " ' )
2019-05-19 18:31:25 +02:00
points = lastentry . get_points ( ' steps ' )
lastdate = list ( points ) [ 0 ] [ ' time ' ]
ts = time . mktime ( datetime . datetime . strptime ( lastdate , ' % Y- % m- %d T % H: % M: % SZ ' ) . timetuple ( ) )
2022-02-27 13:07:51 +01:00
if ts == datetime . datetime . now ( ) . timestamp ( ) / / 86400 * 86400 + time . timezone :
points = lastentry . get_points ( ' steps ' )
laststeps = list ( points ) [ 0 ] [ ' last ' ]
2019-05-19 18:31:25 +02:00
2022-02-27 10:20:35 +01:00
if args . verbose :
2022-02-27 13:07:51 +01:00
print ( " last entry is %s , ts is %s , number of steps is %s \n " % ( lastdate , ts , laststeps ) )
2019-05-19 18:31:25 +02:00
con = lite . connect ( dbfile )
with con :
cur = con . cursor ( )
2022-02-27 13:07:51 +01:00
cur . execute ( " SELECT recordedForDate,steps,distanceInMeters,activeTimeInSeconds,calories FROM dailyActivityLog WHERE recordedForDate >= %s AND steps > %s " % ( ts , laststeps ) )
2019-05-19 18:31:25 +02:00
while True :
row = cur . fetchone ( )
if row == None :
break
mytime = datetime . datetime . fromtimestamp ( row [ 0 ] ) . strftime ( ' % Y- % m- %d T % H: % M: % SZ ' )
data = [
{ " measurement " : " steps " ,
" time " : mytime ,
" fields " : {
" steps " : row [ 1 ] ,
" distanceInMeters " : row [ 2 ] ,
" activeTimeInSeconds " : row [ 3 ] ,
" calories " : row [ 4 ]
}
}
]
2022-02-27 10:20:35 +01:00
if args . verbose :
print ( " writing data for %s " % ( mytime ) )
2019-05-19 18:31:25 +02:00
db . write_points ( data )
2020-06-28 18:30:07 +02:00
db . close ( )
2019-05-19 18:31:25 +02:00
def parse_args ( ) :
""" Parse the args from main. """
parser = argparse . ArgumentParser (
description = ' Export Pacer data to InfluxDB ' )
2022-02-27 10:26:09 +01:00
parser . add_argument ( ' -v ' , ' --verbose ' , required = False ,
2022-02-27 10:20:35 +01:00
action = " store_true " ,
help = ' verbose output ' )
2022-02-27 10:26:09 +01:00
parser . add_argument ( ' -H ' , ' --host ' , type = str , required = False ,
2019-05-19 18:31:25 +02:00
default = ' localhost ' ,
help = ' hostname of InfluxDB http API ' )
2022-02-27 10:26:09 +01:00
parser . add_argument ( ' -u ' , ' --user ' , type = str , required = False ,
2019-05-19 18:31:25 +02:00
default = ' root ' ,
help = ' username for InfluxDB http API ' )
2022-02-27 10:26:09 +01:00
parser . add_argument ( ' -p ' , ' --password ' , type = str , required = False ,
2019-05-19 18:31:25 +02:00
default = ' root ' ,
help = ' passworid for InfluxDB http API ' )
2022-02-27 10:26:09 +01:00
parser . add_argument ( ' -P ' , ' --port ' , type = int , required = False , default = 8086 ,
2019-05-19 18:31:25 +02:00
help = ' port of InfluxDB http API ' )
2022-02-27 10:26:09 +01:00
parser . add_argument ( ' -n ' , ' --dbname ' , type = str , required = True , default = ' demo ' ,
2019-05-19 18:31:25 +02:00
help = ' InfluxDB database name ' )
2022-02-27 10:26:09 +01:00
parser . add_argument ( ' -f ' , ' --dbfile ' , type = str , required = True , default = ' demo.db ' ,
2019-05-19 18:31:25 +02:00
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 )