forked from nikitavoloboev/alfred-web-searches
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (107 loc) · 3.43 KB
/
main.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
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"regexp"
"strings"
"git.deanishe.net/deanishe/awgo"
"git.deanishe.net/deanishe/awgo/update"
"gopkg.in/alecthomas/kingpin.v2"
)
// name of the background job that checks for updates
const updateJobName = "checkForUpdate"
var (
// kingpin and script
app *kingpin.Application
// application commands
filterWebsitesCmd *kingpin.CmdClause
// script options
query string
// icons
redditIcon = &aw.Icon{Value: "icons/reddit.png"}
docIcon = &aw.Icon{Value: "icons/doc.png"}
gitubIcon = &aw.Icon{Value: "icons/github.png"}
forumsIcon = &aw.Icon{Value: "icons/forums.png"}
translateIcon = &aw.Icon{Value: "icons/translate.png"}
stackIcon = &aw.Icon{Value: "icons/stack.png"}
iconAvailable = &aw.Icon{Value: "icons/update-available.png"}
repo = "nikitavoloboev/alfred-web-searches"
// workflow
wf *aw.Workflow
)
func init() {
wf = aw.New(update.GitHub(repo))
app = kingpin.New("web-searches", "Search through customised list of websites")
app.HelpFlag.Short('h')
app.Version(wf.Version())
filterWebsitesCmd = app.Command("websites", "filters websites")
for _, cmd := range []*kingpin.CmdClause{filterWebsitesCmd} {
cmd.Flag("query", "search query").Short('q').StringVar(&query)
}
// list action commands
app.DefaultEnvars()
}
// _actions
// fills Alfred with hash map values and shows keys
func filterWebsites(links map[string]string) {
var re1 = regexp.MustCompile(`.: `)
var re2 = regexp.MustCompile(`(all)`)
for key, value := range links {
if strings.Contains(key, "r: ") {
wf.NewItem(key).Valid(true).Var("URL", value).Var("ARG", re1.ReplaceAllString(key, ``)).UID(key).Icon(redditIcon).Var("RECENT", re2.ReplaceAllString(value, `week`)).Subtitle("⌃ = search past week")
} else if strings.Contains(key, "d: ") {
wf.NewItem(key).Valid(true).Var("URL", value).Var("ARG", re1.ReplaceAllString(key, ``)).UID(key).Icon(docIcon)
} else if strings.Contains(key, "g: ") {
wf.NewItem(key).Valid(true).Var("URL", value).Var("ARG", re1.ReplaceAllString(key, ``)).UID(key).Icon(gitubIcon)
} else if strings.Contains(key, "s: ") {
wf.NewItem(key).Valid(true).Var("URL", value).Var("ARG", re1.ReplaceAllString(key, ``)).UID(key).Icon(stackIcon)
} else if strings.Contains(key, "f: ") {
wf.NewItem(key).Valid(true).Var("URL", value).Var("ARG", re1.ReplaceAllString(key, ``)).UID(key).Icon(forumsIcon)
} else if strings.Contains(key, "t: ") {
wf.NewItem(key).Valid(true).Var("URL", value).Var("ARG", re1.ReplaceAllString(key, ``)).UID(key).Icon(translateIcon)
} else {
wf.NewItem(key).Valid(true).Var("URL", value).Var("ARG", re1.ReplaceAllString(key, ``)).UID(key)
}
}
wf.Filter(query)
wf.SendFeedback()
}
// :TODO: add auto update
func run() {
var err error
// load values from websites.csv to a hash map
f, err := os.Open("websites.csv")
if err != nil {
panic(err)
}
defer f.Close()
r := csv.NewReader(f)
records, err := r.ReadAll()
if err != nil {
log.Fatal(err)
}
// holds user's search arguments and an appropriate search URL
links := make(map[string]string)
for _, record := range records {
links[record[0]] = record[1]
}
// _arg parsing
cmd, err := app.Parse(wf.Args())
if err != nil {
wf.FatalError(err)
}
switch cmd {
case filterWebsitesCmd.FullCommand():
filterWebsites(links)
default:
err = fmt.Errorf("unknown command: %s", cmd)
}
if err != nil {
wf.FatalError(err)
}
}
func main() {
aw.Run(run)
}