forked from ystyle/kaf-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.go
200 lines (186 loc) · 3.85 KB
/
tools.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
198
199
200
package kafcli
import (
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
)
func parseLang(lang string) string {
if lang == "" {
return "en"
}
var langs = "en,de,fr,it,es,zh,ja,pt,ru,nl"
if strings.Contains(langs, lang) {
return lang
}
return "en"
}
func run(command string, args ...string) error {
cmd := exec.Command(command, args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
}
func lookKindlegen() string {
command := "kindlegen"
if runtime.GOOS == "windows" {
command = "kindlegen.exe"
}
kindlegen, err := exec.LookPath(command)
if err != nil {
currentDir, err := os.Executable()
if err != nil {
return ""
}
kindlegen = filepath.Join(filepath.Dir(currentDir), command)
if exist, _ := isExists(kindlegen); !exist {
return ""
}
fmt.Println("kindlegen: ", kindlegen)
}
return kindlegen
}
func converToMobi(bookname, lang string) {
command := lookKindlegen()
fmt.Printf("\n检测到Kindle格式转换器: %s,正在把书籍转换成Kindle格式...\n", command)
fmt.Println("转换mobi比较花时间, 大约耗时1-10分钟, 请等待...")
start := time.Now()
run(command, "-dont_append_source", "-locale", lang, "-c1", bookname)
// 计算耗时
end := time.Now().Sub(start)
fmt.Println("转换为mobi格式耗时:", end)
}
func isExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func getClientID() string {
clientID := fmt.Sprintf("%d", rand.Uint32())
config, err := os.UserConfigDir()
if err != nil {
return clientID
}
filepath := fmt.Sprintf("%s/kaf-wifi/config", config)
if exist, _ := isExists(filepath); exist {
bs, err := ioutil.ReadFile(filepath)
if err != nil {
return clientID
}
clientID = string(bs)
} else {
err := os.MkdirAll(fmt.Sprintf("%s/kaf-wifi", config), 0700)
if err != nil {
return clientID
}
_ = os.WriteFile(filepath, []byte(clientID), 0700)
}
return clientID
}
func GetEnv(key, defaultvalue string) string {
if v := os.Getenv(key); v != "" {
return v
}
return defaultvalue
}
var colors = []string{
"#61005e",
"#70706d",
"#890029",
"#c4000e",
"#6d001d",
"#6a00bd",
"#f10000",
"#0071b1",
"#f9bc00",
"#2c0077",
"#ba009a",
"#009047",
"#009d9e",
"#222e85",
"#bd002e",
"#009d1a",
"#75a500",
}
func ParseInt(v string) int {
v = strings.ReplaceAll(v, ",", "")
i, err := strconv.ParseInt(v, 0, 32)
if err != nil {
return 0
}
return int(i)
}
func GenCover(title, author, color string, img int) (string, error) {
query := url.Values{}
query.Add("title", title)
query.Add("author", author)
query.Add("g_loc", "BR")
query.Add("top_text", "kaf")
query.Add("g_text", "")
if img >= 0 && img <= 41 {
query.Add("img_id", fmt.Sprintf("%d", img))
} else {
query.Add("img_id", fmt.Sprintf("%d", rand.Intn(41)))
}
if strings.HasPrefix(color, "#") {
query.Add("color", strings.TrimLeft(color, "#"))
} else {
i := ParseInt(color)
if i == 0 {
i = rand.Intn(17)
}
color := colors[i]
query.Add("color", strings.TrimLeft(color, "#"))
}
uri := fmt.Sprintf("https://orly.nanmu.me/api/generate?%s", query.Encode())
res, err := http.Get(uri)
if err != nil {
return "", err
}
tempDir, err := os.MkdirTemp("", "kaf-cli")
bs, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
coverfile := filepath.Join(tempDir, fmt.Sprintf("%s.jpg", title))
err = os.WriteFile(coverfile, bs, 0666)
if err != nil {
return "", err
}
return coverfile, nil
}
type Number interface {
~int | ~uint
}
func defaultString(src, dst string) string {
if src == "" {
return dst
}
return src
}
func defalutInt[T Number](src, dst T) T {
if src == 0 {
return dst
}
return src
}
func defaultBool(src, dst bool) bool {
if src {
return src
}
return dst
}