Skip to content

Instantly share code, notes, and snippets.

@one-data-cookie
Created July 7, 2024 19:55
Show Gist options
  • Save one-data-cookie/3a92fc2d4bef08be67f43cb1e2df2a1e to your computer and use it in GitHub Desktop.
Save one-data-cookie/3a92fc2d4bef08be67f43cb1e2df2a1e to your computer and use it in GitHub Desktop.
Converts data from Loop Habit Tracker (Android, CSV) to Awesome Habits (iOS, JSON)
import pandas as pd
import json
from datetime import datetime, timezone
# Function to convert date to Apple epoch time
def to_apple_epoch(date_str):
dt = datetime.strptime(date_str, "%Y-%m-%d")
apple_epoch = datetime(2001, 1, 1, tzinfo=timezone.utc)
delta = dt.replace(tzinfo=timezone.utc) - apple_epoch
return int(delta.total_seconds())
# Habit mapping from names to IDs
habit_mapping = {
"Do something": "ABDB7B76-B8CF-4AAD-8B83-E212695B1D2D",
"No something": "DFB3AE83-D3B5-4CE6-82E2-36B9FDD72D08"
}
# Load the CSV file
file_path = 'checkmarks.csv'
df = pd.read_csv(file_path)
# Generate the JSON records
records = []
for index, row in df.iterrows():
date_apple_epoch = to_apple_epoch(row['Date'])
for column in df.columns[1:]:
habit_id = habit_mapping.get(column, "")
if not habit_id:
continue # Skip columns not in habit mapping
if column.lower().startswith("no"):
quit_habit = True
count_goal = 0
count_done = 0 if row[column] == 2 else 1
skip = row[column] == -1
else:
quit_habit = False
count_goal = 1
count_done = 1 if row[column] == 2 else 0
skip = row[column] == -1
record = {
"countDone": count_done,
"countDoneLocked": 0,
"countGoal": count_goal,
"countGoalLocked": 0,
"date": date_apple_epoch,
"frequency": 0,
"habitId": habit_id,
"note": "",
"quitHabit": quit_habit,
"skip": skip,
"timeZone": "Europe/Prague",
"unit": "",
"weekdays": "MoTuWeThFrSaSu"
}
records.append(record)
# Create the final JSON structure
json_data = json.dumps({
"customLists": [],
"habitListRelations": [],
"habits": [],
"records": records,
"reminders": []
}, indent = 2)
# Save JSON to a file
output_path = "habits.json"
with open(output_path, "w") as json_file:
json_file.write(json_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment