/
search.go
180 lines (147 loc) · 4.83 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
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
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"github.com/spf13/cobra"
)
var searxEngines []string
type SearchResult struct {
URL string `json:"url"`
Title string `json:"title"`
Seed string `json:"seed"`
Leech string `json:"leech"`
MagnetLink string `json:"magnetlink,omitempty"`
Template string `json:"template"`
PublishedDate string `json:"publishedDate,omitempty"`
FileSize int `json:"filesize"`
Engine string `json:"engine"`
ParsedURL []string `json:"parsed_url"`
Engines []string `json:"engines"`
Positions []int `json:"positions"`
Score float64 `json:"score"`
Category string `json:"category"`
PrettyURL string `json:"pretty_url"`
PubDate string `json:"pubdate,omitempty"`
OpenGroup bool `json:"open_group,omitempty"`
CloseGroup bool `json:"close_group,omitempty"`
}
type SearxResponse struct {
Query string `json:"query"`
NumberOfResults int `json:"number_of_results"`
Results SearchResultList `json:"results"`
Answers []interface{} `json:"answers"`
Corrections []interface{} `json:"corrections"`
Infoboxes []interface{} `json:"infoboxes"`
Suggestions []interface{} `json:"suggestions"`
UnresponsiveEngines [][]string `json:"unresponsive_engines"`
}
type SearchResultList []SearchResult
func (e SearchResultList) Len() int {
return len(e)
}
func (e SearchResultList) Less(i, j int) bool {
one, err := strconv.Atoi(e[i].Seed)
two, err := strconv.Atoi(e[j].Seed)
if err != nil {
return false
}
return one > two
}
func (e SearchResultList) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}
type LoggableSearchResult struct {
URL string `json:"url"`
Title string `json:"title"`
Seed string `json:"seed"`
Leech string `json:"leech"`
MagnetLink string `json:"magnetlink,omitempty"`
FileSize int `json:"filesize"`
Engine string `json:"engine"`
Score float64 `json:"score"`
}
func mapSearchResults(sr []SearchResult) []LoggableSearchResult {
var results []LoggableSearchResult
for _, r := range sr {
if r.MagnetLink != "" {
results = append(results, LoggableSearchResult{
URL: r.URL,
Title: r.Title,
Seed: r.Seed,
Leech: r.Leech,
MagnetLink: r.MagnetLink,
FileSize: r.FileSize,
Engine: r.Engine,
Score: r.Score,
})
}
}
return results
}
// searchCmd represents the search command
var searchCmd = &cobra.Command{
Use: "search [search term]",
Short: "Search for a magnet link using searx",
Run: func(cmd *cobra.Command, args []string) {
searchTerm := args[0]
searxInstance := cmd.Flag("searx-instance").Value.String()
u, err := url.ParseRequestURI(searxInstance)
if err != nil {
panic(err)
}
searxInstance = u.String()
if (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
panic("invalid searx instance")
}
fmt.Errorf("Searching for \"%s\" using %s (%s)\n", searchTerm, strings.Join(searxEngines, ","), searxInstance)
params := url.Values{}
params.Add("q", searchTerm)
params.Add("engines", cmd.Flag("searx-engines").Value.String())
params.Add("format", "json")
params.Add("categories", "files")
u.RawQuery = params.Encode()
searxInstance = u.String()
res, err := http.Get(searxInstance)
if err != nil {
panic(err)
}
defer res.Body.Close()
bodyBytes, _ := ioutil.ReadAll(res.Body)
// bodyString := string(bodyBytes)
// fmt.Println("API Response as String:\n" + bodyString)
var searxResponse SearxResponse
json.Unmarshal(bodyBytes, &searxResponse)
if cmd.Flag("json").Value.String() == "true" {
loggableSearchResult, err := json.Marshal(mapSearchResults(searxResponse.Results))
if err != nil {
panic(err)
}
fmt.Printf("%s", loggableSearchResult)
} else {
results := searxResponse.Results
sort.Sort(results)
for _, sr := range results {
fmt.Printf("%s\n", sr.MagnetLink)
}
}
},
}
func init() {
rootCmd.AddCommand(searchCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// searchCmd.PersistentFlags().String("searx-instance", "i", "SearX instance to use")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
searchCmd.Flags().StringP("searx-instance", "i", "", "SearX instance to use")
cobra.MarkFlagRequired(searchCmd.Flags(), "searx-instance")
searchCmd.Flags().StringSliceVarP(&searxEngines, "searx-engines", "e", []string{"1337x", "nyaa", "kickass", "piratebay"}, "SearX engines to use")
searchCmd.Flags().Bool("json", false, "Output in JSON format")
}