commit 2f722449722470dc14cd85c102784e75686fa023 Author: Mariano Ramon Date: Tue Apr 7 06:33:19 2020 -0300 basic script and web stub diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e5a4893 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +nohup.out \ No newline at end of file diff --git a/dmapp/dmweb/__init__.py b/dmapp/dmweb/__init__.py new file mode 100644 index 0000000..7180060 --- /dev/null +++ b/dmapp/dmweb/__init__.py @@ -0,0 +1,19 @@ +import os + +from flask import Flask +from dmweb import dm + +def create_app(test_config=None): + + app = Flask("deskmeter") #instance_relative_config=True + + try: + os.makedirs(app.instance_path) + except OSError: + pass + + app.register_blueprint(dm.dmbp) + + return app + + diff --git a/dmapp/dmweb/dm.py b/dmapp/dmweb/dm.py new file mode 100644 index 0000000..0fb400d --- /dev/null +++ b/dmapp/dmweb/dm.py @@ -0,0 +1,7 @@ +from flask import Blueprint + +dmbp = Blueprint("deskmeter", __name__, url_prefix="/") + +@dmbp.route("/") +def index(): + return "Hello, World!" diff --git a/dmapp/run.py b/dmapp/run.py new file mode 100644 index 0000000..0f7a83d --- /dev/null +++ b/dmapp/run.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from dmweb import create_app + +app = create_app() +app.run(host='0.0.0.0', debug=True, threaded=True) + diff --git a/dmmain.py b/dmmain.py new file mode 100644 index 0000000..6d2caa5 --- /dev/null +++ b/dmmain.py @@ -0,0 +1,59 @@ +import subprocess +import os +import datetime +import time + +from pymongo import MongoClient +from pprint import pprint + +client = MongoClient() + +now = datetime.datetime.utcnow + +db = client.deskmeter +switches = db.switch +dailies = db.daily + +desktops = ("Work", + "Browse", + "Write", + "Learn", + "Idle") + + +unlabeled = "Other" + +def active_workspace(): + + workspaces = subprocess.check_output(["wmctrl", "-d"]) \ + .decode("utf-8").strip("\n").split("\n") + + for workspace in workspaces: + if workspace[3] == "*": + return int(workspace[0]) + + +def desktop(workspace_index): + try: + return desktops[workspace_index] + except IndexError: + return unlabeled + + + +current_workspace = active_workspace() +last_switch_time = now() + +while True: + if current_workspace != active_workspace(): + + delta = round((now() - last_switch_time ).total_seconds()) + switch = { "workspace": desktop(current_workspace), + "date": last_switch_time, + "delta": delta } + + switches.insert_one(switch) + current_workspace = active_workspace() + last_switch_time = now() + + time.sleep(1) \ No newline at end of file