-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.bak
192 lines (164 loc) · 3.79 KB
/
main.bak
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
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
"gopkg.in/ini.v1"
)
type RespData struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data string `json:"data"`
}
type RespMsg struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
type ArtReq struct {
Title string `json:"title"`
Keyword string `json:"keyword"`
Content string `json:"content"`
IsLock int `json:"islock"`
IsPub int `json:"ispub"`
}
const (
OPEN_URL = "https://www.91demo.top/api/open"
)
var (
title string
keyword string
filename string
ispub int // 1 pub
islock int // 1 lock
)
func init() {
flag.StringVar(&title, "b", "", "文章题目")
flag.StringVar(&keyword, "k", "", "文章关键字")
flag.StringVar(&filename, "f", "", "MD文件")
flag.IntVar(&islock, "l", 0, "是否加锁")
flag.IntVar(&ispub, "p", 0, "是否开放")
}
func printHelp() {
fmt.Println("介绍")
fmt.Println("./upart.exe -b title -k keyword -f filename -l islock -p ispub")
}
func main() {
flag.Parse()
var err error
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
fmt.Println("获取系统目录错误,", err)
os.Exit(1)
}
confFile := filepath.Join(dir, "conf.ini")
cfg, err := ini.Load(confFile)
if err != nil {
fmt.Println("读取配置文件错误,", err)
os.Exit(1)
}
icode := cfg.Section("User").Key("icode").String()
isecret := cfg.Section("User").Key("isecret").String()
token := cfg.Section("User").Key("token").String()
expireAt := cfg.Section("User").Key("expire_at").MustInt64()
// 刷新token
now := time.Now().Unix()
if now > expireAt {
token, err = getToken(icode, isecret)
if err != nil {
fmt.Println("获取token错误,", err)
os.Exit(1)
}
expireAtStr := strconv.FormatInt(now+7000, 10)
cfg.Section("User").Key("token").SetValue(token)
cfg.Section("User").Key("expire_at").SetValue(expireAtStr)
cfg.SaveTo(confFile)
}
if token == "" || title == "" || filename == "" {
printHelp()
fmt.Println("题目和Markdown文件名不能为空")
os.Exit(1)
}
// 上传文章
err = uploadArt(token, title, keyword, filename, ispub, islock)
if err != nil {
fmt.Println("上传文章错误,", err)
os.Exit(1)
}
fmt.Println("成功上传")
}
func getToken(icode string, isecret string) (string, error) {
url := fmt.Sprintf("%s/vtoken?icode=%s&isecret=%s", OPEN_URL, icode, isecret)
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
var result RespData
err = json.Unmarshal(data, &result)
if err != nil {
return "", err
}
if result.Code == 1 {
return result.Data, nil
} else {
return "", errors.New(result.Msg)
}
}
func uploadArt(token string, title string, keyword string, filename string, ispub int, islock int) error {
if token == "" {
return errors.New("token不能为空")
}
url := fmt.Sprintf("%s/upVArt?token=%s", OPEN_URL, token)
filecontent, err := os.ReadFile(filename)
if err != nil {
return err
}
snippet := string(filecontent)
reqBody := new(bytes.Buffer)
art := ArtReq{
Title: title,
Keyword: keyword,
Content: snippet,
IsPub: ispub,
IsLock: islock,
}
json.NewEncoder(reqBody).Encode(art)
req, err := http.NewRequest("POST", url, reqBody)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
var result RespMsg
err = json.Unmarshal(data, &result)
if err != nil {
return err
}
if result.Code == 1 {
return nil
} else {
return errors.New(result.Msg)
}
}
// 构建 go build -ldflags '-s -w'