-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.gs
More file actions
502 lines (474 loc) · 17.5 KB
/
code.gs
File metadata and controls
502 lines (474 loc) · 17.5 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/* Copyright 2021 @Lord_Vlad
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
This script was originally comissioned by the copyright owner. I (@Stryder2076) started using it myself and modified and distributed it with his permission.*/
const jsonString =
HtmlService.createHtmlOutputFromFile("config.html").getContent();
const jsonObject = JSON.parse(jsonString);
const habId = jsonObject.habId;
const habToken = jsonObject.habToken; // Never share your API token with anyone even on Github
const soonTodoBlacklist = jsonObject.todoSoonDueDateBlacklist;
const overDueTodoBlacklist = jsonObject.todoOverDueBlacklist;
const lowTodoCountItemBlacklist = jsonObject.todoCountItemBlacklist;
const habitToCheck = jsonObject.habitToCheck;
const taskName = jsonObject.taskName;
const AllCursesCheckedChecklistName = jsonObject.allCursesCheckedName;
const haveNoSoonToDosChecklistName = jsonObject.noSoonTodosName;
const haveNoOverdueToDosChecklistName = jsonObject.noOverdueTodosName;
const haveLessThanTwentyTodosName = jsonObject.haveALowTodoCountName;
function scheduleCron() {
const getParams = {
method: "get",
headers: {
"x-api-user": habId,
"x-api-key": habToken,
},
};
const postParams = {
method: "post",
headers: {
"x-api-user": habId,
"x-api-key": habToken,
},
};
// below code until next comment goes through every todo and makes it default to not being blacklisted
let allTodos = UrlFetchApp.fetch(
"https://habitica.com/api/v3/tasks/user?type=todos",
getParams
);
let parsedTD = JSON.parse(allTodos.getContentText());
for (todos of parsedTD.data) {
todos.blacklisted = false;
}
// Below code until next comment is fetching the list of daily tasks and defining the majority of variables that will be used later on.
let response = UrlFetchApp.fetch(
"https://habitica.com/api/v3/tasks/user?type=dailys",
getParams
);
response = JSON.parse(response.getContentText());
Logger.log("Searching for the task: " + taskName);
// This for loop goes through every task and checks to see if it's the same as the chosen daily up top.
for (task of response.data) {
if (task.text.trim().toLowerCase() === taskName.trim().toLowerCase()) {
Logger.log("Found task " + task.id);
var taskId = task.id;
// This for loop looks through the checklist of the correct task and checks to see if the checklist items exist, if they do it communicates that to another code section through a function output and moves on, otherwise nothing happens
const allCursesCheckedOutput = findItemAndCreateIfDidntExist(
task.checklist,
AllCursesCheckedChecklistName,
"All curses checked"
);
const haveNoSoonToDosOutput = findItemAndCreateIfDidntExist(
task.checklist,
haveNoSoonToDosChecklistName,
"Have no soon to-dos"
);
const haveNoOverDueTodosOutput = findItemAndCreateIfDidntExist(
task.checklist,
haveNoOverdueToDosChecklistName,
"Have no over Due To-Dos"
);
const haveLessThanTwentyTodosOutput = findItemAndCreateIfDidntExist(
task.checklist,
haveLessThanTwentyTodosName,
"Have a low to-do count"
);
if (allCursesCheckedOutput) {
// Below code until next comment is the auto check for all curses done, I plan on adding more arrays that you can toggle the concatination of to allow for the addon challenge and other difficulties, sorrt for any inconvenience
const defaultEasyChallengePoisons = [
"B Time Magic",
"B Ritual Preparation",
"B Volunteering curse",
"R Old Magician's Curse",
"R Questing for Ingredient",
"B Mana Vampirism Curse",
"B Organizational Curse",
"D Curse of the To-Do Bosses",
"D Procrastinator's Curse",
"D Villian's Curse",
];
let dailyTasks = UrlFetchApp.fetch(
"https://habitica.com/api/v3/tasks/user?type=dailys",
getParams
);
let parsedDT = JSON.parse(dailyTasks.getContentText());
let poisonsDone = true;
for (possibleCurseTask of parsedDT.data) {
let isPoisonTitle = false;
for (poisonTitle of defaultEasyChallengePoisons) {
if (
poisonTitle.trim().toLowerCase() ===
possibleCurseTask.text.trim().toLowerCase()
) {
isPoisonTitle = true;
break;
}
}
if (isPoisonTitle === true && possibleCurseTask.completed === false) {
poisonsDone = false;
break;
}
}
//poisonsDone = true; // this is debug code
Logger.log("Task checklist item is " + AllCursesCheckedChecklistName);
if (poisonsDone == true && allCursesCheckedOutput.completed === false) {
Logger.log("All curses checked, scoring checklist item");
UrlFetchApp.fetch(
`https://habitica.com/api/v3/tasks/${taskId}/checklist/${allCursesCheckedOutput.id}/score`,
postParams
);
allCursesCheckedOutput.completed = true;
}
}
if (haveNoSoonToDosOutput) {
let today = new Date();
let days;
let task;
let soonToDoDate;
function toDoin14Days(value) {
task = value;
soonToDoDate = new Date(task.date).valueOf();
let todayUTC = today;
let td = new Date(todayUTC).valueOf();
let sec = 1000;
let min = 60 * sec;
let hour = 60 * min;
let day = 24 * hour;
let diff = soonToDoDate - td;
days = Math.floor(diff / day);
//days = 17; //this is debug code
Logger.log(
"due date difference from today is" + " " + "%s days",
days
);
let blacklistedTodosDateList;
const findBlacklistedsoonTodosOutput = findBlacklistedTodos(
"Soon to-dos",
soonTodoBlacklist,
blacklistedTodosDateList
);
Logger.log(findBlacklistedsoonTodosOutput);
return days < 13 && task.blacklisted === false;
}
let soonToDosExist = parsedTD.data.some(toDoin14Days);
Logger.log("task checklist item is " + haveNoSoonToDosOutput.text);
if (
soonToDosExist === false &&
haveNoSoonToDosOutput.completed === false
) {
Logger.log("scoring checklist item");
UrlFetchApp.fetch(
`https://habitica.com/api/v3/tasks/${taskId}/checklist/${haveNoSoonToDosOutput.id}/score`,
postParams
);
haveNoSoonToDosOutput.completed = true;
}
} else {
Logger.log("something went wrong");
}
// Below code until next comment is the auto checking for no overdue to-dos
if (haveNoOverDueTodosOutput) {
let overdue = false;
let today;
today = new Date();
today = new Date(
today.getFullYear(),
today.getMonth(),
today.getDate()
);
let sec;
let min;
let hour;
sec = 1000;
min = 60 * sec;
hour = 60 * min;
day = 24 * hour;
let blacklistedTodosOverdueList;
const findBlacklistedOverdueTodosOutput = findBlacklistedTodos(
"Overdue to-dos",
overDueTodoBlacklist,
blacklistedTodosOverdueList
);
Logger.log(findBlacklistedOverdueTodosOutput);
for (possibleOverDueToDo of parsedTD.data) {
if (possibleOverDueToDo.date) {
let ToDoDate = new Date(possibleOverDueToDo.date);
ToDoDate = new Date(
ToDoDate.getFullYear(),
ToDoDate.getMonth(),
ToDoDate.getDate()
);
//ToDoDate = today; // this is debug code
Logger.log({ ToDoDate, today, today });
if (ToDoDate < today && possibleOverDueToDo.blacklisted === false) {
overdue = true;
break;
}
Logger.log("Have no overdue to-dos checklist item is " + haveNoOverDueTodosOutput.text);
if (overdue === false && haveNoOverDueTodosOutput.completed === false) {
Logger.log("scoring checklist item");
UrlFetchApp.fetch(
`https://habitica.com/api/v3/tasks/${taskId}/checklist/${haveNoOverDueTodosOutput.id}/score`,
postParams
);
haveNoOverDueTodosOutput.completed = true;
}
}
}
} else {
Logger.log("something went wrong");
}
if (haveLessThanTwentyTodosOutput) {
let toDoCount = 0;
let toDoLimit = 20; // change this to whatever your goal for your to-do count is
for (task of parsedTD.data) {
if (task.blacklisted === true) {
continue;
}
toDoCount = toDoCount + 1;
}
//toDoCount = 10; // this is debug code
if (
toDoCount < toDoLimit &&
haveLessThanTwentyTodosOutput.completed === false
) {
Logger.log("scoring checklist item");
UrlFetchApp.fetch(
`https://habitica.com/api/v3/tasks/${taskId}/checklist/${haveLessThanTwentyTodosOutput.id}/score`,
postParams
);
haveLessThanTwentyTodosOutput.completed = true;
}
} else {
Logger.log("something went wrong");
}
if (
allCursesCheckedOutput.completed === true &&
haveNoSoonToDosOutput.completed === true &&
haveNoOverDueTodosOutput.completed === true &&
haveLessThanTwentyTodosOutput.completed === true
) {
Logger.log("Scoring daily");
UrlFetchApp.fetch(
`https://habitica.com/api/v3/tasks/${taskId}/score/up`,
postParams
);
}
// All below code until the next comment is for checking checklist items' completion status and ticking the negative habit if they aren't completed.
if (allCursesCheckedOutput.completed == false) {
Logger.log(
JSON.stringify({ allCursesCheckedOutput }) +
" checklist item " +
"failed deducting health..."
);
response = UrlFetchApp.fetch(
"https://habitica.com/api/v3/tasks/user?type=habits",
getParams
);
response = JSON.parse(response.getContentText());
habit = checkHabitExists(response);
if (habit) {
// You can change the amount of habit scoring in this if statement and the below else statement to configure the punishment for failure of each checklist item, be careful with using too many as it can cause rate limiting
scoreHabit(habit.id);
} else {
id = createHabit();
scoreHabit(habit.id);
}
}
if (haveNoSoonToDosOutput.completed == false) {
Logger.log(
JSON.stringify({ haveNoSoonToDosOutput }) +
" Checklist item " +
" failed deducting health..."
);
response = UrlFetchApp.fetch(
"https://habitica.com/api/v3/tasks/user?type=habits",
getParams
);
response = JSON.parse(response.getContentText());
habit = checkHabitExists(response);
if (habit) {
// You can change the amount of habit scoring in this if statement and the below else statement to configure the punishment for failure of each checklist item, be careful with using too many as it can cause rate limiting
scoreHabit(habit.id);
scoreHabit(habit.id);
} else {
id = createHabit();
scoreHabit(habit.id);
scoreHabit(habit.id);
}
}
if (haveNoOverDueTodosOutput.completed == false) {
Logger.log(
JSON.stringify({ haveNoOverDueTodosOutput }) +
" checklist failed deducting health..."
);
response = UrlFetchApp.fetch(
"https://habitica.com/api/v3/tasks/user?type=habits",
getParams
);
response = JSON.parse(response.getContentText());
habit = checkHabitExists(response);
if (habit) {
// You can change the amount of habit scoring in this if statement and the below else statement to configure the punishment for failure of each checklist item, be careful with using too many as it can cause rate limiting
scoreHabit(habit.id);
scoreHabit(habit.id);
scoreHabit(habit.id);
} else {
id = createHabit();
scoreHabit(habit.id);
scoreHabit(habit.id);
scoreHabit(habit.id);
}
}
if (haveLessThanTwentyTodosOutput.completed == false) {
Logger.log(
JSON.stringify({ haveLessThanTwentyTodosOutput }) +
" checklist item " +
"failed deducting health..."
);
response = UrlFetchApp.fetch(
"https://habitica.com/api/v3/tasks/user?type=habits",
getParams
);
response = JSON.parse(response.getContentText());
habit = checkHabitExists(response);
if (habit) {
// You can change the amount of habit scoring in this if statement and the below else statement to configure the punishment for failure of each checklist item, be careful with using too many as it can cause rate limiting
scoreHabit(habit.id);
} else {
id = createHabit();
scoreHabit(habit.id);
}
}
}
}
function findItemAndCreateIfDidntExist(
checklist,
itemName,
itemNameInLogStatement
) {
for (check of checklist) {
if (check.text.trim().toLowerCase() === itemName.trim().toLowerCase()) {
Logger.log(
itemNameInLogStatement +
" item name is" +
" " +
"'" +
check.text +
"'" +
", its ID is: " +
check.id
);
return check;
}
}
const newItem = JSON.parse(JSON.stringify(postParams));
newItem.payload = {
text: itemName,
};
const checklistItemResponse = UrlFetchApp.fetch(
`https://habitica.com/api/v3/tasks/${taskId}/checklist/`,
newItem
);
const Item = JSON.parse(checklistItemResponse.getContentText());
// below code is looking for the checklist item that was just made to properly define the variable
for (checklistItem of Item.data.checklist) {
if (
checklistItem.text.trim().toLowerCase() ===
itemName.trim().toLowerCase()
) {
Logger.log(
itemNameInLogStatement +
" item did not exist, it has been made; it's title is" +
" " +
"'" +
checklistItem.text +
"'" +
", its ID is: " +
checklistItem.id
);
return checklistItem;
}
}
return null;
}
function findBlacklistedTodos(
taskTypeInLogStatement,
blacklistedConfig,
blacklistedTodosArray
) {
blacklistedTodosArray = [];
for (todo of parsedTD.data) {
for (blacklistedTodo of blacklistedConfig) {
if (
blacklistedTodo.trim().toLowerCase() ===
todo.text.trim().toLowerCase()
) {
todo.blacklisted = true;
blacklistedTodosArray.push(todo.text);
}
}
}
Logger.log(
"These are the blacklisted " + taskTypeInLogStatement + " found"
);
return blacklistedConfig;
}
function scoreHabit(id) {
const postParams = {
method: "post",
headers: {
"x-api-user": habId,
"x-api-key": habToken,
},
};
Logger.log("Scoring the habit...");
response = UrlFetchApp.fetch(
`https://habitica.com/api/v3/tasks/${id}/score/down`,
postParams
);
if (response.getResponseCode() === 200) {
Logger.log("Scoring successful");
} else {
Logger.log(
"There was an issue scoring the habit: " + response.getContentText()
);
}
}
function createHabit() {
const postParams = {
method: "post",
headers: {
"x-api-user": habId,
"x-api-key": habToken,
},
payload: {
text: habitToCheck,
type: "habit",
up: false,
},
};
Logger.log("Habit not yet created, creating one...");
response = UrlFetchApp.fetch(
"https://habitica.com/api/v3/tasks/user",
postParams
);
if (response.getResponseCode() === 201) {
Logger.log("Habit created successfully");
response = JSON.parse(response.getContentText());
return response.data["_id"];
}
}
function checkHabitExists(response) {
for (habit of response.data) {
if (
habit.text.trim().toLowerCase() === habitToCheck.trim().toLowerCase()
) {
return habit;
}
}
return null;
}
}