/
workflowy.go
237 lines (216 loc) · 6.96 KB
/
workflowy.go
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
package workflowy
import (
"net/http"
"net/url"
"fmt"
"errors"
"strings"
"encoding/base32"
"crypto/rand"
"io/ioutil"
"github.com/Jeffail/gabs"
"html"
)
type WorkflowyClient struct {
session string
client string
most_recent_transaction string
owner int
json *gabs.Container
pending_operations [](*gabs.Container)
}
type WorkflowyItem struct {
Id string
Name string
Completed bool
Priority int
Description *string
Children_names []string
}
func GetSession(username string, password string) (string, error) {
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
form := url.Values{}
form.Add("username", username)
form.Add("password", password)
resp, err := client.PostForm("https://workflowy.com/accounts/login/", form)
if (resp.StatusCode != 302) {
return "", errors.New("Expected status 302 when getting session")
}
if err != nil {
return "", err
}
for _, cookie := range resp.Cookies() {
if cookie.Name == "sessionid" {
return cookie.Value, nil
}
}
return "", errors.New("Unable to find session within returned cookies")
}
func NewClient(session string) (*WorkflowyClient, error) {
workflowyClient := &WorkflowyClient{session: session, pending_operations: [](*gabs.Container){}}
err := workflowyClient.fetchItemsFromServer()
return workflowyClient, err
}
func (client *WorkflowyClient) LookupItem(path ... string) (WorkflowyItem, error) {
return lookupItemFromJson(client.json.Path("projectTreeData.mainProjectTreeInfo.rootProjectChildren"), path)
}
func (client *WorkflowyClient) AddCreate(name string, priority int, parent *string, description *string) {
item_id := makeItemId()
parentString := "None"
if parent != nil {
parentString = *parent
}
op := gabs.New()
op.Set("create", "type")
op.Set(item_id, "data", "projectid")
op.Set(parentString, "data", "parentid")
op.Set(priority, "data", "priority")
client.pending_operations = append(client.pending_operations, op)
client.AddUpdate(item_id, &name, &priority, parent, description)
}
func (client *WorkflowyClient) AddUpdate(item_id string, name *string, priority *int, parent *string, description *string) {
op := gabs.New()
op.Set("edit", "type")
op.Set(item_id, "data", "projectid")
if name != nil {
op.Set(html.EscapeString(*name), "data", "name")
}
if description != nil {
op.Set(html.EscapeString(*description), "data", "description")
}
if parent != nil {
op.Set(*parent, "data", "parentid")
}
if priority != nil {
op.Set(*priority, "data", "priority")
}
client.pending_operations = append(client.pending_operations, op)
}
func (client *WorkflowyClient) AddDelete(item_id string) {
op := gabs.New()
op.Set("delete", "type")
op.Set(item_id, "data", "projectid")
client.pending_operations = append(client.pending_operations, op)
}
func (client *WorkflowyClient) AddComplete(item_id string) {
op := gabs.New()
op.Set("complete", "type")
op.Set(item_id, "data", "projectid")
client.pending_operations = append(client.pending_operations, op)
}
func (client *WorkflowyClient) AddUncomplete(item_id string) {
op := gabs.New()
op.Set("uncomplete", "type")
op.Set(item_id, "data", "projectid")
client.pending_operations = append(client.pending_operations, op)
}
func (client *WorkflowyClient) ApplyAndRefresh() error {
form := url.Values{}
operationList := makeOperationList(client.most_recent_transaction, client.pending_operations)
form.Add("client_id", client.client)
form.Add("crosscheck_user_id", string(client.owner))
form.Add("push_poll_id", makeUpdateId())
form.Add("client_version", "18")
form.Add("push_poll_data", operationList.String())
req, _ := http.NewRequest("POST", "https://workflowy.com/push_and_poll", strings.NewReader(form.Encode()))
req.Header.Add("Cookie", "sessionid="+client.session)
_, err := http.DefaultClient.Do(req)
if (err != nil) {
return err
}
client.pending_operations = [](*gabs.Container){}
client.fetchItemsFromServer()
return nil
}
func (client *WorkflowyClient) fetchItemsFromServer() error {
req, err := http.NewRequest("GET", "https://workflowy.com/get_initialization_data?client_version=18", nil)
req.Header.Add("Cookie", "sessionid="+client.session)
resp, err := http.DefaultClient.Do(req)
if (err != nil) {
return err
} else {
readall, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
json, err := gabs.ParseJSON(readall)
if err != nil {
return err
}
client.client = json.Path("projectTreeData.clientId").Data().(string)
client.owner = int(json.Path("projectTreeData.mainProjectTreeInfo.ownerId").Data().(float64))
client.most_recent_transaction = json.Path("projectTreeData.mainProjectTreeInfo.initialMostRecentOperationTransactionId").Data().(string)
client.json = json
return nil
}
}
func lookupItemFromJson(json *gabs.Container, path []string) (WorkflowyItem, error) {
children, err := json.Children()
if err != nil {
return WorkflowyItem{}, errors.New("could not find children")
}
for priority, child := range children {
childName := child.Path("nm").Data().(string)
if childName == path[0] {
if len(path) == 1 {
item_id := child.Path("id").Data().(string)
name := child.Path("nm").Data().(string)
completed := child.Exists("cp")
children_names := []string{}
// if this item has children, gather their names
if child.Exists("ch") {
metaChildren, _ := child.Path("ch").Children()
for _, metaChild := range metaChildren {
children_names = append(children_names, metaChild.Path("nm").Data().(string))
}
}
var description *string = nil
if child.Exists("no") {
descriptionString := child.Path("no").Data().(string)
description = &descriptionString
}
return WorkflowyItem{
Id: item_id,
Name: name,
Priority: priority,
Children_names: children_names,
Completed: completed,
Description: description,
}, nil
} else {
return lookupItemFromJson(child.Path("ch"), path[1:])
}
}
}
return WorkflowyItem{}, errors.New("Could not find node " + path[0])
}
func makeOperationList(most_recent_transaction string, operations [](*gabs.Container)) *gabs.Container {
operationList := gabs.New()
operationList.Set(most_recent_transaction, "most_recent_operation_transaction_id")
operationList.Array("operations")
for _, operation := range operations {
operationList.ArrayAppend(operation.Data(), "operations")
}
containingArray := gabs.New()
containingArray.Array()
containingArray.ArrayAppend(operationList.Data())
return containingArray
}
func makeItemId() string {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
fmt.Println("Error: ", err)
return ""
}
return strings.ToLower(fmt.Sprintf("%X-%X-%X-%X-%X", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]))
}
func makeUpdateId() string {
b := make([]byte, 64)
rand.Read(b)
return base32.StdEncoding.EncodeToString(b)[0:7]
}