This repository has been archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
go-pinboard.go
322 lines (298 loc) · 9.37 KB
/
go-pinboard.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
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
package main
import (
"fmt"
"net/url"
"os"
"os/exec"
"path"
"regexp"
"strconv"
"strings"
Alfred "bitbucket.org/listboss/go-alfred"
cli "github.com/codegangsta/cli"
)
var (
AccountName string = "accountName"
TagsCacheFN string = "tags_cache"
PostsCachFn string = "posts_cache"
MaxNoResults_Tags int = 10
MaxNoResults_Bookmarks int = 10
hostURLPinboard string = "api.pinboard.in"
hostURLScheme string = "https"
commentCharacter string = ";"
AutoUpdate string = "no"
)
type pinboardPayload struct {
url string
description string
extended string
tags string
replace string
shared string
toread string
auth_token string
}
func Init() (ga *Alfred.GoAlfred) {
var err error
ga = Alfred.NewAlfred("go-pinboard")
// ga.Set("shared", "no")
ga.Set("replace", "yes")
AccountName, err = ga.Get("username")
if err != nil {
ga.MakeError(err)
ga.WriteToAlfred()
os.Exit(1)
}
if AccountName == "" {
AccountName = "deleteMe"
}
tags_cache_fn := path.Join(ga.CacheDir,
strings.Join([]string{TagsCacheFN, AccountName}, "_"))
posts_cache_fn := path.Join(ga.CacheDir,
strings.Join([]string{PostsCachFn, AccountName}, "_"))
ga.Set("tags_cache_fn", tags_cache_fn)
ga.Set("posts_cache_fn", posts_cache_fn)
v, _ := ga.Get("max_tags")
if v == "" {
v = "10"
}
tmp, err := strconv.ParseInt(v, 10, 32)
if err != nil {
MaxNoResults_Tags = 10
} else {
MaxNoResults_Tags = int(tmp)
}
v, _ = ga.Get("max_bookmarks")
if v == "" {
v = "10"
}
tmp, err = strconv.ParseInt(v, 10, 32)
if err != nil {
MaxNoResults_Bookmarks = 10
} else {
MaxNoResults_Bookmarks = int(tmp)
}
v, err = ga.Get("auto_update")
if err == nil && v != "" {
AutoUpdate = v
}
return ga
}
func main() {
ga := Init()
app := cli.NewApp()
app.Name = "alfred_pinboard"
app.Usage = "Alfred Workflow to manage Pinboard pins."
app.Action = func(ctx *cli.Context) {
foo := `NAME:
alfred_pinboard - Alfred Workflow helper to manage Pinboard pins.
enter "alfred_pinboard help" for more information`
os.Stdout.Write([]byte(foo))
}
updateBookmarksCache := cli.Command{
Name: "update",
Usage: "Fetches all the bookmarks and updates the tags cache.",
Action: func(c *cli.Context) {
err := update_tags_cache(ga)
if err != nil {
os.Stdout.WriteString(err.Error())
} else {
os.Stdout.WriteString("Successfully Updated Local Cache.")
}
},
}
setOptions := cli.Command{
Name: "setoptions",
Usage: "Sets token and other options",
Description: "set Workflow related options.",
Flags: []cli.Flag{
cli.StringFlag{Name: "auth", Value: "", Usage: "Set authorization token in form of username:token"},
cli.StringFlag{Name: "fuzzy,f", Value: "", Usage: "Enable fuzzy search"},
cli.StringFlag{Name: "shared", Value: "", Usage: "Set sharing/private status for posted bookmarks"},
cli.StringFlag{Name: "tag-only-search", Value: "", Usage: "Only search through tags when looking up bookmarks"},
cli.StringFlag{Name: "auto-update", Value: "", Usage: "Automatically update bookmarks cache after posting a bookmark."},
cli.IntFlag{Name: "max-tags", Value: -1, Usage: "Set max. number of tags to show."},
cli.IntFlag{Name: "max-bookmarks", Value: -1, Usage: "Set max. number of bookmarks to show."},
},
Action: func(c *cli.Context) {
// Set max number of tags/bookmarks to show
if mt := c.Int("max-tags"); mt != -1 {
mtags := strconv.Itoa(mt)
ga.Set("max_tags", mtags)
os.Stdout.WriteString("Max no. tags to show: " + mtags)
}
if mb := c.Int("max-bookmarks"); mb != -1 {
mbook := strconv.Itoa(mb)
ga.Set("max_bookmarks", mbook)
os.Stdout.WriteString("Max no. bookmarks to show: " + mbook)
}
// Set sharing/private status for bookmarks
if s := c.String("shared"); s != "" {
ga.Set("shared", s)
os.Stdout.WriteString("Sharing bookmarks: " + s)
}
// Set search option for using tags only
if ts := c.String("tag-only-search"); ts != "" {
ga.Set("tag_only_search", ts)
os.Stdout.WriteString("Tag-only search: " + ts)
}
// Set auto-update option
if au := c.String("auto-update"); au != "" {
ga.Set("auto_update", au)
os.Stdout.WriteString("Cache auto update: " + au)
}
// Set authorization tokens
if t := c.String("auth"); t != "" {
ga.Set("oauth", t)
_username := strings.Split(t, ":")[0]
ga.Set("username", _username)
tags_cache_fn := path.Join(ga.CacheDir,
strings.Join([]string{TagsCacheFN, _username}, "_"))
posts_cache_fn := path.Join(ga.CacheDir,
strings.Join([]string{PostsCachFn, _username}, "_"))
ga.Set("tags_cache_fn", tags_cache_fn)
ga.Set("posts_cache_fn", posts_cache_fn)
if err := update_tags_cache(ga); err != nil {
os.Stdout.WriteString("Err Setting Auth. " + err.Error())
} else {
os.Stdout.WriteString("Successfully Set Auth. Token.")
}
}
// Enable/Disable fuzzy search
if value := c.String("fuzzy"); value != "" {
if value == "yes" || value == "no" {
if err := ga.Set("fuzzy_search", value); err != nil {
os.Stdout.WriteString("Err setting fuzzy. " + err.Error())
} else {
os.Stdout.WriteString("Successfully changed\nfuzzy search setting.")
}
}
}
},
}
postBookmark := cli.Command{
Name: "post",
Usage: "post tag1 tag2 ; extra notes",
Description: "Posts a bookmark to cloud for the current page of the active browser.",
Action: func(c *cli.Context) {
query := strings.Join(c.Args(), " ")
binfo, err := postToCloud(query, ga)
if err == nil {
os.Stdout.WriteString(binfo[1])
} else {
os.Stdout.WriteString(err.Error())
}
au := strings.ToLower(AutoUpdate)
if au == "yes" || au == "1" || au == "on" {
err := update_tags_cache(ga)
if err != nil {
os.Stdout.WriteString(err.Error())
}
}
},
}
showTagsCommand := cli.Command{
Name: "showtags",
Usage: "Shows list of available tags based on intut arguments.",
Action: func(c *cli.Context) {
args := []string(c.Args())
showtags(args, ga)
},
}
showBookmarksCommand := cli.Command{
Name: "showbookmarks",
Usage: "Show list of bookmarks that contain 'all' the search keywords",
Action: func(c *cli.Context) {
args := []string(c.Args())
showBookmarks(args, ga)
},
}
showSettingsCommand := cli.Command{
Name: "showsettings",
Usage: "Show Workflow's settings",
Action: func(c *cli.Context) {
showSettings(ga)
},
}
app.Commands = []cli.Command{updateBookmarksCache, setOptions,
postBookmark, showTagsCommand, showBookmarksCommand,
showSettingsCommand}
app.Run(os.Args)
}
func showSettings(ga *Alfred.GoAlfred) {
max_tags, _ := ga.Get("max_tags")
max_bookmarks, _ := ga.Get("max_bookmarks")
fuzzy_search, _ := ga.Get("fuzzy_search")
tag_search, _ := ga.Get("tag_only_search")
shared, _ := ga.Get("shared")
// ga.AddItem(uid, title, subtitle, valid, auto, rtype, arg, icon, check_valid)
ga.AddItem("", "No. Tags: "+max_tags, "No. of tags to show.", "yes", "",
"", "pset tags", Alfred.NewIcon("tag_icon.icns", ""), false)
ga.AddItem("", "No. Bookmarks: "+max_bookmarks, "No. of bookmarks to show.",
"yes", "", "", "pset bmarks",
Alfred.NewIcon("pin.png", ""),
false)
ga.AddItem("", "Fuzzy search: "+fuzzy_search, "Use fuzzy search.", "yes",
"", "", "pset fuzzy", Alfred.NewIcon("fuzzy_search.icns", ""), false)
ga.AddItem("", "Sharing boookmarks: "+shared,
"Private or Shared bookmarking", "yes", "", "", "pset shared",
Alfred.NewIcon("shared_bookmarking.png", ""), false)
ga.AddItem("", "Auto update bookmarks cache: "+AutoUpdate,
"Download all bookmarks after posting a new bookmark.",
"yes", "", "", "pset auto", Alfred.NewIcon("auto_update.png", ""),
false)
ga.AddItem("", "Tag-only search: "+tag_search, "Search only the tags?",
"yes", "", "", "pset tagonly",
Alfred.NewIcon("tag_only.png", ""), false)
ga.WriteToAlfred()
}
func encodeURL(payload pinboardPayload, pathURL string) (req url.URL) {
u := url.URL{}
u.Scheme = hostURLScheme
u.Host = hostURLPinboard // path.Join(hostURLPinboard, pathURL)
u.Path = pathURL
q := u.Query()
q.Set("url", payload.url)
q.Set("description", payload.description)
q.Set("extended", payload.extended)
q.Set("replace", payload.replace)
q.Set("shared", payload.shared)
q.Set("tags", payload.tags)
q.Set("auth_token", payload.auth_token)
q.Set("toread", payload.toread)
u.RawQuery = q.Encode()
return u
}
func buildRegExp(s string) (re *regexp.Regexp) {
regexp_exp := ""
for _, v := range s {
regexp_exp += string(v) + "+.*"
}
re = regexp.MustCompile(regexp_exp)
return
}
func getBrowserInfo(ga *Alfred.GoAlfred) (pinInfo []string, err error) {
// Use external applescript and some tricky hacks to prevent non-running browsers
// from being startet when posting a new url to pinboard
// @see http://stackoverflow.com/a/16071855
b, err := exec.Command("osascript", "-s", "so", "get-current-url.applescript").Output()
// fmt.Printf("error> '%v'\n", err)
if err != nil {
fmt.Println("%v", err)
return nil, err
}
out := string(b)
// fmt.Printf("--> '%v'\n", out)
foo0 := strings.Trim(out, "{}\n")
foo1 := strings.Split(foo0, "@@@@@")
pinURL := strings.Trim(foo1[0], "\" ")
pinDesc := ""
if len(foo1) > 1 {
pinDesc = strings.Trim(foo1[1], "\" ")
}
// If the current page doesn't have title set it to the URL
if pinDesc == "" {
pinDesc = pinURL
}
return []string{pinURL, pinDesc}, err
}