Runs every 1461 days
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { blob } from "https://esm.town/v/std/blob";
import process from "node:process";
import { TodoistApi } from "npm:@doist/todoist-api-typescript";
import Instructor from "npm:@instructor-ai/instructor";
import Jimp from "npm:jimp";
import OpenAI from "npm:openai";
import { z } from "npm:zod";
const force_update_database = false; // set force update database to true
// if you added new items to habitify after running this script
// for the first time.
// INST: Add these keys in val env variables
// NOTE: the image notes don't work at the moment, and if you add a image note,
// the task is skipped
const TODOIST_API_KEY = process.env.TODOIST_API_KEY;
const HABITIFY_API_KEY = process.env.HABITIFY_API_KEY;
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const DEF_TIMEZONE = "America/Los_Angeles"; // Get your timezone from here: https://stackoverflow.com/a/54500197
var add_to_habitify_todoist_project_id = "XXXX"; // INST: Project ID goes here
var todoist_dict_mapping = { // Note: You can choose to leave this dict empty and it will still work
"Image Notes": { // This is for a section that is image notes only for any habit without adding a log
"todoist-section-id": "XXXX", // INST: section id for just image notes section goes here
"habitify-area-name": "undefined", // keep this value as undefined
specialPrompt: "only_image", // keep the value as only_image
},
"Text Notes": { // This is for a section that is text notes only for any habit without adding a log
"habitify-area-name": "undefined", // keep this value as undefined
"todoist-section-id": "XXXX", // INST: section id for just image notes section goes here
specialPrompt: "only_text", // keep the value as only_text
},
Cats: {
"todoist-section-id": "XXXX", // section id for the area goes here
"habitify-area-name": "Cats", // INST: habitify area name goes here. Match exactly
specialPrompt: "", // keep the value as empty text
},
Food: { // You do not need to have a section that tracks calories, it is just a bonus
"todoist-section-id": "XXXX", // section id for the area goes here
"habitify-area-name": "Health", // INST: habitify area name goes here. Match exactly
specialPrompt: "track_calories", // keep the value as track_calories if you want to
// estimate calories based on entry. the estimation is pretty wild and can vary by +/- 500 kCal at least
},
// ADD any other sections as areas here to help the model choose amongst less habits.
// Keep specialPrompt value as empty strng
};
const todoistapi = new TodoistApi(TODOIST_API_KEY);
const oai = new OpenAI({
apiKey: OPENAI_API_KEY ?? undefined,
});
const client = Instructor({
client: oai,
mode: "TOOLS",
});
function getHabitifyAreaName(section_id) {
if (!section_id) {
return ["undefined", ""];
}
for (var key in todoist_dict_mapping) {
if (todoist_dict_mapping[key]["todoist-section-id"] === section_id) {
return [
todoist_dict_mapping[key]["habitify-area-name"],
todoist_dict_mapping[key]["specialPrompt"].toLowerCase(),
];
}
}
return ["undefined", ""];
}
function convertDateObject(due) {
function convertToISOWithOffset(datetimeStr, timezoneStr) {
const date = new Date(datetimeStr);
const [, sign, hours, minutes] = timezoneStr.match(
/GMT ([+-])(\d{1,2}):(\d{2})/,
);
date.setUTCMinutes(
date.getUTCMinutes()
+ (parseInt(hours) * 60 + parseInt(minutes)) * (sign === "+" ? 1 : -1),
);
return (
date.toISOString().split(".")[0]
+ `${sign}${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}`
);
}
const formatDate = (date, datetime, timezone) => {
let isoString = datetime ? datetime : date;
if (timezone && timezone.startsWith("GMT") && timezone.length > 3) {
return convertToISOWithOffset(datetime, timezone);
} else {
return isoString;
}
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 19, 2024