Screen Time Alerts from Activity Watch

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()
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()
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()

Enter fullscreen mode Exit fullscreen mode

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
$ python screen_time_alerts.py
$ python screen_time_alerts.py

Enter fullscreen mode Exit fullscreen mode

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
$ crontab -e
0 * * * * python screen_time_alerts.py
$ crontab -e 0 * * * * python screen_time_alerts.py

Enter fullscreen mode Exit fullscreen mode

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.


  1. Activity Watch

  2. Screen Time Alerts Script

原文链接:Screen Time Alerts from Activity Watch

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
Life is like a cup of tea. It won't be bitter for a lifetime but for a short while anyway.
人生就像一杯茶,不会苦一辈子,但总会苦一阵子
评论 抢沙发

请登录后发表评论

    暂无评论内容