Introduction
Activity Watch1 is a cross-platform open-source time-tracking tool that helps us to track time spent on applications and websites.
At the moment, Activity Watch doesn't have any feature to show screen time alerts. In this post, we will see how to show screen time alerts using Activity Watch.
Python Script
Activity Watch provides an API to interact with the Activity Watch server. We can use the API to get the screen time data and show alerts.
import json
import os
from datetime import datetime
import requests
def get\_nonafk\_events(timeperiods=None):
headers = {"Content-type": "application/json", "charset": "utf-8"}
query = """afk\_events = query\_bucket(find\_bucket('aw-watcher-afk\_'));
window\_events = query\_bucket(find\_bucket('aw-watcher-window\_'));
window\_events = filter\_period\_intersect(window\_events, filter\_keyvals(afk\_events, 'status', ['not-afk']));
RETURN = merge\_events\_by\_keys(window\_events, ['app', 'title']);""".split("\n")
data = {
"timeperiods": timeperiods,
"query": query,
}
r = requests.post(
"http://localhost:5600/api/0/query/",
data=bytes(json.dumps(data), "utf-8"),
headers=headers,
params={},
)
return json.loads(r.text)[0]
def main():
now = datetime.now()
timeperiods = [
"/".join([now.replace(hour=0, minute=0, second=0).isoformat(), now.isoformat()])
]
events = get\_nonafk\_events(timeperiods)
total\_time\_secs = sum([event["duration"] for event in events])
total\_time\_mins = total\_time\_secs / 60
print(f"Total time: {total\_time\_mins} seconds")
hours, minutes = divmod(total\_time\_mins, 60)
minutes = round(minutes)
print(f"Screen Time: {hours} hours {minutes} minutes")
# show mac notification
os.system(f"osascript -e 'display notification \"{hours} hours {minutes} minutes\" with title \"Screen TIme\"'")
if \_\_name\_\_ == "\_\_main\_\_":
main()
This script2 will show the screen time alerts using the Activity Watch API. We can run this script using the below command.
$ python screen_time_alerts.py
We can set up a cron job to run this script every hour to show screen time alerts.
$ crontab -e
0 * * * * python screen_time_alerts.py
We can also modify the script to show alerts only when the screen time exceeds a certain limit.
Conclusion
Since Actvity Watch is open-source and provides an API, we can extend its functionality to show screen time alerts. We can also use the API to create custom reports and dashboards.
Top comments (0)