Created
June 3, 2024 02:10
-
-
Save lhr0909/b671ff551351a7e362491f0b9834305a to your computer and use it in GitHub Desktop.
Convert RescueTime CSV into ActivityWatch bucket JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import json | |
import datetime | |
events = [] | |
# Open the CSV | |
with open("portable_user_history-2024-06-02-csv.csv", "r") as f: | |
reader = csv.DictReader( | |
f, | |
fieldnames=["datetime", "app", "title", "subcategory", "category", "duration"], | |
) | |
# read line by line | |
last_datetime_obj = None | |
offset_seconds = 0 | |
for row in reader: | |
print(row) | |
# convert date time string to ISO8601 with UTC timezone | |
# current format: 2016-08-30 13:00:00 -0700 | |
# desired format: 2016-08-30T20:00:00.000000+00:00 | |
datetime_str = row["datetime"] | |
datetime_obj = datetime.datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S %z") | |
if last_datetime_obj is None or last_datetime_obj < datetime_obj: | |
last_datetime_obj = datetime_obj | |
offset_seconds = 0 | |
# add offset as seconds into datetime_iso | |
datetime_obj = datetime_obj + datetime.timedelta(seconds=offset_seconds) | |
datetime_iso = datetime_obj.astimezone(datetime.timezone.utc).isoformat( | |
timespec="microseconds" | |
) | |
event = { | |
"timestamp": datetime_iso, | |
"duration": float(row["duration"]), | |
"data": { | |
"app": row["app"], | |
"title": row["title"], | |
"subcategory": row["subcategory"], | |
"category": row["category"], | |
}, | |
} | |
events.append(event) | |
offset_seconds += float(row["duration"]) | |
result = { | |
"buckets": { | |
"rescuetime": { | |
"id": "rescuetime", | |
"created": datetime.datetime.now(datetime.timezone.utc).isoformat( | |
timespec="microseconds" | |
), | |
"name": None, | |
"type": "currentwindow", | |
"client": "rescuetime", | |
"hostname": "Simons-M2.local", | |
"events": events, | |
}, | |
}, | |
} | |
with open("rescuetime.json", "w") as f: | |
json.dump(result, f, indent=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For each event, RescueTime only tagged the start of the hour when the event occurred. This conversion assumes that the exported events happened in order, and I re-constructed the start timestamp for each event based on the duration and its order.