-
Notifications
You must be signed in to change notification settings - Fork 8
/
searchengines.go
153 lines (145 loc) · 4.01 KB
/
searchengines.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"github.com/Tensai75/nzb-monkey-go/nzbparser"
)
// search engine structure
type SearchEngine struct {
name string
searchURL string
downloadURL string
regexString string
jsonPath string
groupNo int
search func(engine SearchEngine, name string) error
}
// search engines map
type SearchEngines map[string]SearchEngine
// global searchEngines map
var searchEngines = SearchEngines{
"nzbindex": SearchEngine{
name: "NZBIndex",
searchURL: "https://nzbindex.com/search/json?sort=agedesc&hidespam=1&q=%s",
downloadURL: "https://nzbindex.com/download/%s/",
jsonPath: "results.0.id",
search: jsonSearch,
},
"nzbking": SearchEngine{
name: "NZBKing",
searchURL: "https://nzbking.com/?q=%s",
downloadURL: "https://nzbking.com/nzb:%s/",
regexString: `href="\/nzb:(.+?)\/"`,
groupNo: 1,
search: htmlSearch,
},
"binsearch": SearchEngine{
name: "Binsearch (most popular groups)",
searchURL: "https://binsearch.info/?max=100&adv_age=1100&q=%s",
downloadURL: "https://binsearch.info/?action=nzb&%s=1",
regexString: `name="(\d{9,})"`,
groupNo: 1,
search: htmlSearch,
},
"binsearch_alternative": SearchEngine{
name: "Binsearch (other groups)",
searchURL: "https://binsearch.info/?max=100&adv_age=1100&server=2&q=%s",
downloadURL: "https://binsearch.info/?action=nzb&%s=1&server=2",
regexString: `name="(\d{9,})"`,
groupNo: 1,
search: htmlSearch,
},
"directsearch": SearchEngine{
name: "NZB direct search",
search: nzbdirectsearch,
},
}
// default search function for html response
func htmlSearch(engine SearchEngine, name string) error {
var err error
var body string
var searchRegexp *regexp.Regexp
var match []string
if body, err = loadURL(fmt.Sprintf(engine.searchURL, args.Header)); err == nil {
if searchRegexp, err = regexp.Compile(engine.regexString); err == nil {
if match = searchRegexp.FindStringSubmatch(body); match != nil {
if len(match) >= engine.groupNo+1 {
if body, err = loadURL(fmt.Sprintf(engine.downloadURL, match[engine.groupNo])); err == nil {
if nzb, err := nzbparser.ParseString(body); err != nil {
return err
} else {
processResult(nzb, name)
}
}
}
} else {
return fmt.Errorf("No results found")
}
}
}
return err
}
// default search function for json response
func jsonSearch(engine SearchEngine, name string) error {
var err error
var body string
var result interface{}
var value string
if body, err = loadURL(fmt.Sprintf(engine.searchURL, args.Header)); err == nil {
if err = json.Unmarshal([]byte(body), &result); err == nil {
for _, value := range strings.Split(engine.jsonPath, ".") {
if number, err := strconv.Atoi(value); err == nil {
if len(result.([]interface{})) > number {
result = result.([]interface{})[number]
} else {
return fmt.Errorf("No results found")
}
} else {
if _, ok := result.(map[string]interface{})[value]; ok {
result = result.(map[string]interface{})[value]
} else {
return fmt.Errorf("No results found")
}
}
}
if fmt.Sprintf("%T", result) == "float64" {
value = fmt.Sprintf("%d", int(result.(float64)))
} else if fmt.Sprintf("%T", result) == "string" {
value = fmt.Sprintf("%s", result.(string))
} else {
return fmt.Errorf("No results found")
}
if body, err = loadURL(fmt.Sprintf(engine.downloadURL, value)); err == nil {
if nzb, err := nzbparser.ParseString(body); err != nil {
return err
} else {
processResult(nzb, name)
}
} else {
return err
}
} else {
return err
}
} else {
return err
}
return nil
}
func loadURL(url string) (string, error) {
if resp, err := http.Get(url); err != nil {
return "", err
} else {
defer resp.Body.Close()
if body, err := ioutil.ReadAll(resp.Body); err != nil {
return "", err
} else {
return string(body), nil
}
}
}