-
Notifications
You must be signed in to change notification settings - Fork 14
/
raindrop_search.go
235 lines (202 loc) · 7.71 KB
/
raindrop_search.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
/*
Functions for searching and browsing Raindrop.io bookmarks in Alfred
By Andreas Westerlind in 2021
*/
package main
import (
"encoding/json"
"strconv"
"strings"
aw "github.com/deanishe/awgo"
)
func search(variant string, query string, collection_json string, from string, descr_in_list bool, favs_first bool) {
var collection_search bool = false
var collection_search_id int = 0
var collection_search_name string
var collection_search_icon string
var collection_from_list bool = false
if variant == "collection" {
collection_search = true
var collection_info map[string]string
json.Unmarshal([]byte(collection_json), &collection_info)
collection_search_name = collection_info["name"]
collection_search_id, _ = strconv.Atoi(collection_info["id"])
collection_search_icon = collection_info["icon"]
if from == "collections" {
collection_from_list = true
}
}
var tag_search bool = false
var tag string = ""
if variant == "tag" {
tag_search = true
tag = query
}
if collection_search {
if collection_from_list {
// We are browsing a collection, and came here from the collection browser
alfred_item := wf.NewItem("Bookmarks in "+collection_search_name).
Var("goto", "browse").
Subtitle("⬅︎ Go back to collection browser").
Valid(true).
Icon(&aw.Icon{Value: collection_search_icon, Type: ""})
alfred_item.Alt().
Var("goto", "back").
Subtitle("⬅︎ Go back to collection browser")
} else {
// We are browsing a collection, and came here from the main bookmark search
alfred_item := wf.NewItem("Bookmarks in "+collection_search_name).
Var("goto", "back").
Subtitle("⬅︎ Go back to search all bookmarks").
Valid(true).
Icon(&aw.Icon{Value: collection_search_icon, Type: ""})
alfred_item.Alt().
Var("goto", "back").
Subtitle("⬅︎ Go back to search all bookmarks")
}
}
if tag_search {
// We are browsing bookmarks with a specific tag
alfred_item := wf.NewItem("Bookmarks tagged with #"+tag).
Var("goto", "back").
Subtitle("⬅︎ Go back to search all bookmarks").
Valid(true).
Icon(&aw.Icon{Value: "tag.png", Type: ""})
alfred_item.Alt().
Var("goto", "back").
Subtitle("⬅︎ Go back to search all bookmarks")
}
// Try to read token, and initiate authentication mekanism if it fails
token := read_token()
if token.Error != "" {
init_auth()
return
}
var raindrop_results []interface{}
var err error
if query != "" {
// Query Raindrop.io
raindrop_results, err = search_request(query, token, collection_search_id, tag)
if err != nil {
new_token := refresh_token(token)
if new_token.Error != "" {
// Refreshing the token failed, so all we can do now is let the user authenticate again
// We will also remove the old token, so that the Workflow knows that an authentication
// is needed next time it's initiated
wf.Keychain.Delete("raindrop_token")
init_auth()
} else {
// Try to query Raindrop again, and assume it will work now as we just got a fresh new token to authenticate with
token = new_token
raindrop_results, _ = search_request(query, token, collection_search_id, tag)
}
}
// Get collection list from cache
raindrop_collections := reverse_interface_array(get_collections(token, false, "trust"))
raindrop_collections_sublevel := reverse_interface_array(get_collections(token, true, "trust"))
// Search for collections and tags that matches the search query, but only if we are not already doing a search in a collection or a tag
if !collection_search && !tag_search {
// Render collections
var current_object []string
render_collections(raindrop_collections, raindrop_collections_sublevel, "paths", "searching", 0, current_object, -1, "", "")
// Get tag list from cache
raindrop_tags := get_tags(token, "check")
// Render tags
for _, item_interface := range raindrop_tags {
item := item_interface.(map[string]interface{})
alfred_item := wf.NewItem(item["_id"].(string)).
Var("current_tag", item["_id"].(string)).
Var("goto", "tag").
Valid(true).
Icon(&aw.Icon{Value: "tag.png", Type: ""})
alfred_item.Alt().
Var("current_tag", item["_id"].(string)).
Var("goto", "tag").
Subtitle("")
}
// Filter collections and tags by search query
wf.Filter(strings.ToLower(query))
}
} else {
// We got no search query
check_token_lifetime(token)
// If we are searching for bookmarks inside a collection or with a specific tag
if collection_search || tag_search {
raindrop_results, _ = search_request("", token, collection_search_id, tag)
} else {
// If we are are in standard search mode
// Cache collection and tag lists to make searching faster when the user types a search query
get_collections(token, false, "check")
get_collections(token, true, "check")
// Default results if nothing is searched for. Just go to Raindrop.io itself
alfred_item := wf.NewItem("Search your Raindrop.io bookmarks").
Arg("https://app.raindrop.io/").
Var("goto", "open").
Subtitle("Or press enter to open Raindrop.io").
Valid(true)
alfred_item.Alt().
Arg("https://app.raindrop.io/").
Var("goto", "open").
Subtitle("Or press enter to open Raindrop.io")
alfred_item2 := wf.NewItem("Browse your Raindrop.io collections").
Var("goto", "browse").
Subtitle("").
Valid(true).
Icon(&aw.Icon{Value: "folder.png", Type: ""})
alfred_item2.Alt().
Var("goto", "browse").
Subtitle("")
}
}
if query != "" || collection_search || tag_search {
// Get collection list from cache (REVERSING OF THE ARRAYS MIGHT NEED TO BE DONE HERE)
raindrop_collections := reverse_interface_array(get_collections(token, false, "check"))
raindrop_collections_sublevel := reverse_interface_array(get_collections(token, true, "check"))
var current_object []string
collection_names := collection_paths(raindrop_collections, raindrop_collections_sublevel, make(map[int]string), 0, current_object, -1)
var render_favourites string = "all"
// Prepare favourites for being viewed in Alfred (if favourites_first is enabled)
if favs_first {
render_results(raindrop_results, "only", collection_names, descr_in_list)
render_favourites = "none"
}
// Prepare the rest of the results (or all results if favourites_first is disabled) for being viewed in Alfred
render_results(raindrop_results, render_favourites, collection_names, descr_in_list)
}
}
func browse(query string, full_collection_paths bool) {
token := read_token()
alfred_item := wf.NewItem("Raindrop.io Bookmark Collections").
Var("goto", "back").
Subtitle("⬅︎ Go back to search all bookmarks").
Valid(true).
Icon(&aw.Icon{Value: "icon.png", Type: ""})
alfred_item.Alt().
Var("goto", "back").
Subtitle("⬅︎ Go back to search all bookmarks")
alfred_item2 := wf.NewItem("Unsorted").
Var("collection_info", "{\"icon\":\"folder.png\",\"id\":\"-1\",\"name\":\"Unsorted\"}").
Var("goto", "collection").
Subtitle("").
Valid(true).
Icon(&aw.Icon{Value: "folder.png", Type: ""})
alfred_item2.Alt().
Var("collection_info", "{\"icon\":\"folder.png\",\"id\":\"-1\",\"name\":\"Unsorted\"}").
Var("goto", "collection").
Subtitle("")
render_style := "tree"
if full_collection_paths {
render_style = "paths"
}
var raindrop_collections []interface{}
var raindrop_collections_sublevel []interface{}
// Get collection list
raindrop_collections = reverse_interface_array(get_collections(token, false, "check"))
raindrop_collections_sublevel = reverse_interface_array(get_collections(token, true, "check"))
// Render collections
var current_object []string
render_collections(raindrop_collections, raindrop_collections_sublevel, render_style, "searching", 0, current_object, -1, "", "")
if query != "" {
wf.Filter(query)
}
}