forked from krau/mcmod-classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (156 loc) · 4.02 KB
/
main.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
package main
import (
"io"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/gookit/slog"
"github.com/gookit/slog/handler"
"github.com/imroc/req/v3"
"github.com/schollz/progressbar/v3"
)
const (
ServerMust = "服务端需装"
ClientMust = "客户端需装"
ServerInvalid = "服务端无效"
ClientInvalid = "客户端无效"
ServerOptional = "服务端可选"
ClientOptional = "客户端可选"
Unknown = "未识别"
)
var (
BracketsPattern = regexp.MustCompile(`\[([^]]+)\]`)
VersionPattern = regexp.MustCompile(`\b\d+(\.\d+)+\b|\bv\d+(\.\d+)+\b`)
Client = req.C()
WikiURL = "https://search.mcmod.cn/s"
Log *slog.Logger
TargetPath = []string{ServerMust, ClientMust, ServerInvalid, ClientInvalid, ServerOptional, ClientOptional, Unknown}
)
func init() {
for _, path := range TargetPath {
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, os.ModePerm)
}
}
slog.DefaultChannelName = "mcmod-classifier"
newLogger := slog.New()
defer newLogger.Flush()
fileH, err := handler.NewFileHandler("mcmod-classifier.log")
if err != nil {
panic(err)
}
newLogger.AddHandler(fileH)
Log = newLogger
}
func GetModName(path string) string {
s := filepath.Base(path)
s = strings.TrimSuffix(s, ".jar")
s = strings.ToLower(s)
if BracketsPattern.FindStringSubmatch(s) != nil {
s = BracketsPattern.FindStringSubmatch(s)[1]
}
if strings.Contains(s, "-forge") {
s = strings.Split(s, "-forge")[0]
}
if strings.Contains(s, "-fabric") {
s = strings.Split(s, "-fabric")[0]
}
if strings.Contains(s, "-quilt") {
s = strings.Split(s, "-quilt")[0]
}
if strings.Contains(s, "-neoforge") {
s = strings.Split(s, "-neoforge")[0]
}
if strings.Contains(s, "-") {
parts := VersionPattern.Split(s, -1)
s = strings.TrimSuffix(parts[0], "-")
}
Log.Debugf("Mod name: %s", s)
return s
}
func CopyFile(src, dst string) {
srcFile, err := os.Open(src)
if err != nil {
Log.Error(err)
return
}
defer srcFile.Close()
dstFile, err := os.Create(dst)
if err != nil {
Log.Error(err)
return
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
Log.Error(err)
return
}
Log.Debugf("Copied %s to %s", src, dst)
}
func main() {
matches, err := filepath.Glob("mods/*.jar")
if err != nil {
slog.Error(err)
return
}
slog.Infof("Found %d mods\n", len(matches))
bar := progressbar.Default(int64((len(matches))))
Client.ImpersonateChrome()
Client.SetMaxConnsPerHost(1)
for _, match := range matches {
bar.Add(1)
time.Sleep(1 * time.Second)
modName := GetModName(match)
r, err := Client.R().SetQueryParam("key", modName).Get(WikiURL)
if err != nil {
slog.Error(err)
CopyFile(match, filepath.Join(Unknown, filepath.Base(match)))
continue
}
doc, err := goquery.NewDocumentFromReader(r.Body)
if err != nil {
slog.Error(err)
CopyFile(match, filepath.Join(Unknown, filepath.Base(match)))
continue
}
// 获取模组页面链接
val, exists := doc.Find(".head").First().Find("a").Eq(1).Attr("href")
if !exists {
Log.Warnf("Mod %s not found", modName)
CopyFile(match, filepath.Join(Unknown, filepath.Base(match)))
continue
}
// 获取模组页面
Log.Debugf("Mod %s found, url: %s", modName, val)
r, err = Client.R().Get(val)
if err != nil {
slog.Error(err)
CopyFile(match, filepath.Join(Unknown, filepath.Base(match)))
continue
}
doc, err = goquery.NewDocumentFromReader(r.Body)
if err != nil {
slog.Error(err)
CopyFile(match, filepath.Join(Unknown, filepath.Base(match)))
continue
}
// 获取模组运行环境
modRunEnv := doc.Find(".class-info-left").Find(".col-lg-12").Find(".col-lg-4").Eq(2).Text()
matched := false
for _, path := range TargetPath {
if strings.Contains(modRunEnv, path) {
CopyFile(match, filepath.Join(path, filepath.Base(match)))
matched = true
}
}
if !matched {
Log.Warnf("Mod %s not matched", modName)
CopyFile(match, filepath.Join(Unknown, filepath.Base(match)))
}
}
slog.Info("Done")
}