Skip to content

Commit 9de8095

Browse files
committed
feat: added load_dataframe.py example
1 parent 67e9052 commit 9de8095

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

examples/load_dataframe.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Load ActivityWatch data into a dataframe, and export as CSV.
3+
"""
4+
5+
from datetime import datetime, timedelta, timezone
6+
7+
import pandas as pd
8+
9+
from aw_client import ActivityWatchClient
10+
11+
12+
_query = """
13+
window = flood(query_bucket(find_bucket("aw-watcher-window_")));
14+
afk = flood(query_bucket(find_bucket("aw-watcher-afk_")));
15+
afk = filter_keyvals(afk, "status", ["not-afk"]);
16+
events = filter_period_intersect(window, afk);
17+
RETURN = {"events": events};
18+
"""
19+
20+
21+
def main() -> None:
22+
now = datetime.now(tz=timezone.utc)
23+
td30d = timedelta(days=7)
24+
25+
aw = ActivityWatchClient()
26+
print("Querying...")
27+
data = aw.query(_query, [(now - td30d, now)])
28+
29+
events = [
30+
{
31+
"timestamp": e["timestamp"],
32+
"duration": timedelta(seconds=e["duration"]),
33+
**e["data"],
34+
}
35+
for e in data[0]["events"]
36+
]
37+
38+
df = pd.json_normalize(events)
39+
df["timestamp"] = pd.to_datetime(df["timestamp"], infer_datetime_format=True)
40+
df.set_index("timestamp", inplace=True)
41+
42+
print(df)
43+
44+
answer = input("Do you want to export to CSV? (y/N): ")
45+
if answer == "y":
46+
filename = "output.csv"
47+
df.to_csv(filename)
48+
print(f"Wrote to {filename}")
49+
50+
51+
if __name__ == "__main__":
52+
main()

0 commit comments

Comments
 (0)