2019-11-09 15:28:16 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""
|
|
|
|
Logs what window is in focus
|
|
|
|
"""
|
|
|
|
|
|
|
|
import csv
|
|
|
|
import datetime
|
|
|
|
import os
|
|
|
|
import typing
|
|
|
|
|
|
|
|
import i3ipc
|
|
|
|
|
|
|
|
|
|
|
|
class ScreenTime:
|
|
|
|
FIELDS = ["date", "type", "class", "role", "instance", "title"]
|
|
|
|
|
|
|
|
def write(self, line: typing.Dict) -> None:
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
line["date"] = now.timestamp()
|
|
|
|
|
|
|
|
print("WROTE", line)
|
2021-06-13 11:49:21 +02:00
|
|
|
with open(self.csv_path, "a") as typedfd:
|
2019-11-09 15:28:16 +01:00
|
|
|
writer = csv.DictWriter(typedfd, fieldnames=self.FIELDS)
|
|
|
|
writer.writerow(line)
|
|
|
|
|
2021-06-13 11:49:21 +02:00
|
|
|
def on_window_event(
|
|
|
|
self, _: i3ipc.connection.Connection, e: i3ipc.events.WindowEvent
|
|
|
|
) -> None:
|
2019-11-09 15:28:16 +01:00
|
|
|
focused = self.i3.get_tree().find_focused()
|
2021-06-13 11:49:21 +02:00
|
|
|
self.write(
|
|
|
|
{
|
|
|
|
"type": "window_" + e.change,
|
|
|
|
"class": focused.window_class,
|
|
|
|
"role": focused.window_role,
|
|
|
|
"title": focused.window_title,
|
|
|
|
"instance": focused.window_instance,
|
|
|
|
}
|
|
|
|
)
|
2019-11-09 15:28:16 +01:00
|
|
|
|
2021-06-13 11:49:21 +02:00
|
|
|
def on_mode_event(
|
|
|
|
self, _: i3ipc.connection.Connection, e: i3ipc.events.ModeEvent
|
|
|
|
) -> None:
|
|
|
|
self.write({"type": "mode", "title": e.change})
|
2019-11-09 15:28:16 +01:00
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.i3 = i3ipc.Connection()
|
|
|
|
self.i3.on(i3ipc.Event.WINDOW, self.on_window_event)
|
|
|
|
self.i3.on(i3ipc.Event.MODE, self.on_mode_event)
|
|
|
|
|
|
|
|
self.csv_path = os.path.join(
|
2021-06-13 11:49:21 +02:00
|
|
|
os.path.expanduser(os.getenv("XDG_CACHE_PATH", "~/.cache/")),
|
|
|
|
"screentime.csv",
|
|
|
|
)
|
2019-11-09 15:28:16 +01:00
|
|
|
if not os.path.isfile(self.csv_path):
|
2021-06-13 11:49:21 +02:00
|
|
|
with open(self.csv_path, "w") as typedfd:
|
2019-11-09 15:28:16 +01:00
|
|
|
writer = csv.DictWriter(typedfd, fieldnames=self.FIELDS)
|
|
|
|
writer.writeheader()
|
|
|
|
self.write({"type": "start"})
|
|
|
|
|
|
|
|
def main(self) -> None:
|
|
|
|
self.i3.main()
|
|
|
|
|
|
|
|
|
2021-06-13 11:49:21 +02:00
|
|
|
if __name__ == "__main__":
|
2019-11-09 15:28:16 +01:00
|
|
|
ST = ScreenTime()
|
|
|
|
ST.main()
|