Skip to content

Commit 120b1f7

Browse files
committed
docs(example): added time_spent_today.py example
1 parent 907f5f4 commit 120b1f7

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Client library for ActivityWatch in Python.
99

1010
Please see the documentation for usage and examples.
1111

12+
1213
## How to install
1314

1415
To install the latest git version directly from github without cloning, run
@@ -19,3 +20,11 @@ To install from a cloned version, cd into the directory and run
1920
system-wide it can be installed with `pip install .`, but that has the issue
2021
that it might not get the exact version of the dependencies due to not reading
2122
the poetry.lock file.
23+
24+
25+
## Examples
26+
27+
The `examples/` directory contains a couple of example scripts, including:
28+
29+
- `time_spent_today.py` - fetches all non-afk events and sums their duration to get the total active time for the day.
30+
- `merge_buckets.py` - merges two buckets with non-intersecting events by moving all events from one into the other.

‎examples/time_spent_today.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# NOTE: Might not treat timezones correctly.
2+
3+
from datetime import datetime, time, timedelta
4+
import aw_client
5+
import socket
6+
7+
# Set this to your AFK bucket
8+
bucket_id = "aw-watcher-afk_{}".format(socket.gethostname())
9+
10+
daystart = datetime.combine(datetime.now().date(), time())
11+
dayend = daystart + timedelta(days=1)
12+
13+
awc = aw_client.ActivityWatchClient("testclient")
14+
events = awc.get_events(bucket_id, start=daystart, end=dayend)
15+
events = [e for e in events if e.data["status"] == "not-afk"]
16+
total_duration = sum((e.duration for e in events), timedelta())
17+
print("Total time spent on computer today: {}".format(total_duration))

0 commit comments

Comments
 (0)