This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHabitica_init.gs
108 lines (93 loc) · 2.54 KB
/
Habitica_init.gs
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
let habiticaTodos = [];
let habiticaTags = [];
class HabiticaTodo{
constructor(text, id, notes, isCompleted){
this.text = text; // name of the task as shown to the user
this.id = id;
this.notes = notes;
this.isCompleted = isCompleted;
}
addDueDate(date){
this.dueDate = date;
}
addTags(tags){
this.tags = tags;
}
addAlias(alias){
this.alias = alias;
}
}
class HabiticaTag{
constructor(tagName, tagId){
this.tagName = tagName;
this.tagId = tagId;
}
}
function buildRequest(method, url, payload){
if (payload === undefined){
var params = {
"method" : method,
"headers" : {
"x-api-user" : PropertiesService.getScriptProperties().getProperty("habitica_userid"),
"x-api-key" : PropertiesService.getScriptProperties().getProperty("habitica_apikey")
},
"muteHttpExceptions": true
}
} else {
var params = {
"method" : method,
"headers" : {
"x-api-user" : PropertiesService.getScriptProperties().getProperty("habitica_userid"),
"x-api-key" : PropertiesService.getScriptProperties().getProperty("habitica_apikey")
},
"payload": payload,
"muteHttpExceptions": true
}
}
var response = UrlFetchApp.fetch(PropertiesService.getScriptProperties().getProperty("habitica_apiurl") + url, params);
return response
}
function getTags(){
let taglistResponse = buildRequest('get', 'tags');
var data = JSON.parse(taglistResponse.getContentText());
var tagList = [];
if (data.success){
for(let t of data.data){
tagList.push(new HabiticaTag(t.name, t.id));
}
} else {
Logger.log("Error in taglistResponse request: %s",taglistResponse);
}
return tagList;
}
function getTodosFromHabitica(){
habiticaTags = getTags();
let todolistResponse = buildRequest('get', 'tasks/user');
var data = JSON.parse(todolistResponse.getContentText());
var taskList = [];
if (data.success){
for(let i of data.data){
if (i.type === "todo"){
var todo = new HabiticaTodo(i.text, i.id, i.notes, false);
if (i.tags.length){
var tags = [];
for(let t of i.tags){
tags.push(new HabiticaTag(getTagNameFromId(t), t));
}
todo.addTags(tags);
}
if (i.alias){
todo.addAlias(i.alias);
}
if (i.date){
todo.addDueDate(i.date);
}
taskList.push(todo);
}
}
} else {
Logger.log("Error in todolistResponse request: %s",todolistResponse);
}
// return taskList;
habiticaTodos = taskList;
}