-
Notifications
You must be signed in to change notification settings - Fork 145
/
search.go
197 lines (183 loc) · 4.36 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/ma6254/FictionDown/site"
"gopkg.in/yaml.v2"
"github.com/urfave/cli"
)
var search = cli.Command{
Name: "search",
Aliases: []string{"s"},
Usage: "检索盗版站点",
Flags: []cli.Flag{
cli.StringFlag{
Name: "k,keyword",
Usage: "搜索关键词",
},
cli.BoolFlag{
Name: "put,p",
Usage: "对比并放入缓存文件",
},
cli.StringFlag{
Name: "save,s",
Usage: "searh and save to file 搜索结果存储",
},
cli.StringFlag{
Name: "format,f",
Usage: "save format support json,yaml 存储格式",
},
cli.BoolFlag{
Name: "download,d",
Usage: "直接下载正版",
},
},
Action: func(c *cli.Context) error {
var (
err error
result = make(map[site.SearchBookMeta][]string)
keyword = c.String("keyword")
savepath = c.String("save")
saveformat = c.String("format")
dumpFunc func(v interface{}) ([]byte, error)
)
if savepath != "" {
fmt.Printf("Save File: %s\n", savepath)
if saveformat == "" {
fileext := filepath.Ext(savepath)
switch fileext {
case "", ".json":
saveformat = "json"
case ".yaml", ".yml":
saveformat = "yaml"
default:
return fmt.Errorf("Unsupported file extension %s", fileext)
}
}
fmt.Printf("Save Format: %s\n", saveformat)
switch saveformat {
case "json":
dumpFunc = func(v interface{}) ([]byte, error) {
return json.MarshalIndent(v, "", "\t")
}
case "yaml":
dumpFunc = yaml.Marshal
default:
return fmt.Errorf("unsupport marshal format: %s", saveformat)
}
}
r, err := site.Search(keyword)
if err != nil {
return err
}
for _, v := range r {
nvName := strings.TrimSuffix(v.BookName, "【完结】")
// nvAuthor := strings.TrimLeft(v.Author, `\/`)
nvAuthor := v.Author
result[site.SearchBookMeta{Name: nvName, Author: nvAuthor}] = append(result[site.SearchBookMeta{Name: nvName, Author: nvAuthor}], v.BookURL)
}
rr := site.ConvSearchRequest(result)
if savepath != "" {
var body []byte
if body, err = dumpFunc(rr); err != nil {
return err
}
return ioutil.WriteFile(savepath, body, os.ModePerm)
}
if c.Bool("put") {
if err = initLoadStore(c); err != nil {
return err
}
rrr := result[site.SearchBookMeta{Name: chapter.BookName, Author: chapter.Author}]
chapter.Tmap = rrr
b, err := yaml.Marshal(chapter)
if err != nil {
return err
}
return ioutil.WriteFile(filename, b, os.ModePerm)
}
if c.Bool("download") {
fmt.Printf("开始缓存正版源\n")
vipBookURL, tBookURL :=
SearchMatchSites(rr[0].Urls)
if len(vipBookURL) > 1 {
return fmt.Errorf("多个正版源")
}
fmt.Printf("正版:\n")
for _, u := range vipBookURL {
matchSite, err := site.MatchOne(site.Sitepool, u)
n := matchSite.Name
if err != nil {
n = "无匹配站点"
}
fmt.Printf("\t%#v : %#v\n", n, u)
}
fmt.Printf("盗版:\n")
for _, u := range tBookURL {
matchSite, err := site.MatchOne(site.Sitepool, u)
n := matchSite.Name
if err != nil {
n = "无匹配站点"
}
fmt.Printf("\t%#v : %#v\n", n, u)
}
if len(vipBookURL) == 0 {
return fmt.Errorf("无正版站点")
}
bookurl = vipBookURL[0]
if err = download.Run(c); err != nil {
return err
}
chapter.Tmap = tBookURL
b, err := yaml.Marshal(chapter)
if err != nil {
return err
}
if err = ioutil.WriteFile(filename, b, os.ModePerm); err != nil {
return err
}
if err = download.Run(c); err != nil {
return err
}
return nil
}
fmt.Printf("搜索到%d个内容:\n", len(rr))
for _, v := range rr {
fmt.Printf("书名: %s 作者: %s %d个书源\n", v.Name, v.Author, len(v.Urls))
for _, u := range v.Urls {
matchSite, err := site.MatchOne(site.Sitepool, u)
n := matchSite.Name
if err != nil {
n = "无匹配站点"
}
fmt.Printf("\t%#v : %#v\n", n, u)
}
}
return nil
},
}
func SearchMatchSites(urls []string) (vipURL, tURL []string) {
for _, u := range urls {
matchSite, err := site.MatchOne(site.Sitepool, u)
if err != nil {
continue
}
isvip := false
for _, v := range matchSite.Tags() {
if v == "正版" {
isvip = true
break
}
}
if isvip {
vipURL = append(vipURL, u)
continue
}
tURL = append(tURL, u)
}
return
}