-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuckDuckGoModule.go
More file actions
89 lines (75 loc) · 1.76 KB
/
Copy pathDuckDuckGoModule.go
File metadata and controls
89 lines (75 loc) · 1.76 KB
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
package main
import (
"log"
"os"
)
type DuckDuckGoModule struct {
}
func NewDuckDuckGoModule() *DuckDuckGoModule {
return new(DuckDuckGoModule)
}
func (d *DuckDuckGoModule) Name() string {
return "DuckDuckGo"
}
func (d *DuckDuckGoModule) Description() string {
return "Provides web search via ddg"
}
func (d *DuckDuckGoModule) CanBeDisabled() bool {
return true
}
func (d *DuckDuckGoModule) UpdateSettings() {
// this intentionally empty
}
func (d *DuckDuckGoModule) NeedsExternalData() bool {
return false
}
func (d *DuckDuckGoModule) UpdateExternalData() {
// this intentionally empty
}
func (d *DuckDuckGoModule) WriteExternalData(_ *os.File) {
// this intentionally empty
}
func (d *DuckDuckGoModule) ReadExternalData(_ []byte) error {
// this intentionally empty
return nil
}
type DuckDuckGoSearchAction struct {
query string
queryLabel string
}
func (d DuckDuckGoSearchAction) GetLabel() string {
return "[ddg[] SEARCH " + d.queryLabel
}
func (d DuckDuckGoSearchAction) Run() string {
url := "https://duckduckgo.com/?q=" + d.query
if err := launchUrl(url); err != nil {
log.Fatalf("Could not browse %s: %v", url, err)
}
return "Opened DDG search for " + d.queryLabel
}
func (d *DuckDuckGoModule) CreateActions(tags []Tag) []action {
if len(tags) != 0 {
for _, tag := range tags {
if tag.matchMode != Contains {
// Looks like the user is looking for something spectific - not
// very likely they wanted to ddg
return []action{}
}
}
query := tags[0].value
queryLabel := tags[0].value
for i, tag := range tags {
if i != 0 {
query += "+" + tag.value
queryLabel += " " + tag.value
}
}
return []action{
DuckDuckGoSearchAction{
query: query,
queryLabel: queryLabel,
},
}
}
return []action{}
}