forked from nikitavoloboev/alfred-web-searches
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
76 lines (60 loc) · 2.1 KB
/
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
package main
import (
"encoding/csv"
"log"
"os"
"regexp"
"strings"
)
// doSearch makes a search for websites and returns results to Alfred.
func doSearch() error {
showUpdateStatus()
log.Printf("query=%s", query)
links := parseCSV()
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(githubIcon)
} 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)
}
}
if query != "" {
wf.Filter(query)
}
wf.WarnEmpty("No matching items", "Try a different query?")
wf.SendFeedback()
return nil
}
// parseCSV parses CSV for links and arguments.
func parseCSV() map[string]string {
var err error
// Load values from file 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]
}
return links
}