Create a gist now

Instantly share code, notes, and snippets.

@proffalken /LICENSE
Last active Feb 26, 2017

What would you like to do?
PY3STATUS_CONNECTOR_DB

Py3Status ConnectorDB

This is a Py3Status Module to connect to ConnectorDB and pull back various statistics.

At the moment, it only retrieves steps, however I plan to get it to pull more data over time.

Installation

Download the connector_db.py file and place it into your Py3Status Modules Directory

Configure your i3 status bar with the following:

order += "connector_db"

connector_db {
  username = "ConnectorDB User Name"
  password = "ConnectorDB Password"
  connectordb_server = "ConnectorDB URL"
  mobile_device = "Name of device running ConnectorDB App"
}

Now reload i3wm

# -*- coding: utf-8 -*-
"""
Pull metrics from ConnectorDB and display them on the taskbar
"""
import connectordb
import datetime
import time
class Py3status:
username = ""
password = ""
connectordb_server = ""
mobile_device = ""
def get_metrics(self):
try:
today = datetime.date.today()
seconds_since_midnight = time.time() - time.mktime(today.timetuple())
midnight = time.time() - seconds_since_midnight
cxdb = connectordb.ConnectorDB(
self.username,
self.password,
url=self.connectordb_server)
mob = cxdb.user[self.mobile_device]
mob_data = connectordb.ConnectorDB(mob.apikey,
url=self.connectordb_server)
steps = mob_data['steps']
transform = "if t > %s | sum | if last" % self.midnight
steps_taken = steps(transform=transform)[0]["d"]
except:
steps_taken = "Disconnected"
return {
'full_text': "Steps: %s" % steps_taken
}
Copyright 2017 Matthew Macdonald-Wallace
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
dkumor commented Feb 24, 2017

This is wonderful!

BTW: you don't need a transform to query by timestamp, you can run

steps_taken = steps(t1=self.midnight,t2=time.time(), transform="sum | if last")

Since ConnectorDB indexes data by timestamp, this query will be extremely fast even with millions of datapoints!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment