UP MDTApi

This commit is contained in:
JiXieShi
2024-06-03 16:15:23 +08:00
parent 4b29709791
commit 352666f059
9 changed files with 214 additions and 70 deletions

View File

@@ -1,11 +1,9 @@
package api
import (
"net/http"
)
import "net/http"
func InitApi() {
BilibiliInit()
http.HandleFunc("/api/mdt", GetInfo)
http.HandleFunc("/api/memos", GetMemosJson)
http.HandleFunc(Mindustry.Url, GetMindustryInfo)
http.HandleFunc(Memos.Url, GetMemosJson)
}

View File

@@ -6,12 +6,18 @@ import (
"net/http"
)
func GetInfo(w http.ResponseWriter, r *http.Request) {
var Mindustry = Api{
Mode: "GET",
Url: "/api/mdt",
Args: []Args{{Name: "host", Required: true, Description: "服务器地址"}},
SampleResponse: "{\n \"host\": \"p4.simpfun.cn\",\n \"port\": 8952,\n \"status\": \"Online\",\n \"name\": \"[#00ff00]镜影若滴の低配备用服\",\n \"maps\": \"未知\",\n \"players\": 0,\n \"version\": 146,\n \"wave\": 1,\n \"vertype\": \"official\",\n \"gamemode\": {\n \"name\": \"生存\",\n \"id\": 0\n },\n \"description\": \"[#00ff00]低配但稳定的备用服,[#ffaaff]欢迎加入QQ群726525226\",\n \"modename\": \"\",\n \"limit\": 0,\n \"ping\": 40\n}",
}
func GetMindustryInfo(w http.ResponseWriter, r *http.Request) {
var (
err error
info utils.ServerInfo
)
if err = r.ParseForm(); err != nil {
http.Error(w, "参数解析错误", http.StatusInternalServerError)
}
@@ -25,7 +31,8 @@ func GetInfo(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=utf-8")
err = json.NewEncoder(w).Encode(info)
} else {
http.Error(w, "参数为空", http.StatusInternalServerError)
Mindustry.ErrorInfoView(w, "host为空")
return
}
if err != nil {
http.Error(w, "无法解析服务器数据", http.StatusInternalServerError)

31
api/info.go Normal file
View File

@@ -0,0 +1,31 @@
package api
import (
"blog/config"
"blog/models"
"io"
)
type Api struct {
Mode string
Url string
Args []Args
SampleResponse string
}
type Args struct {
Name string
Required bool
Description string
}
func ApiViewBuild(api Api, err interface{}) map[string]interface{} {
return map[string]interface{}{
"Api": api,
"Err": err,
"Config": config.Cfg,
}
}
func (a Api) ErrorInfoView(w io.Writer, err interface{}) {
models.Api.Info.WriteData(w, ApiViewBuild(a, err))
}

View File

@@ -25,37 +25,52 @@ type MemoInfo struct {
RelationList []interface{} `json:"relationList"`
}
var Memos = Api{
Url: "/api/memos",
Mode: "GET",
SampleResponse: "[{\n \"bookSourceName\": \"69书吧自制\",\n \"bookSourceType\": 0,\n \"bookSourceUrl\": \"https://www.69shu.cc\",\n \"customOrder\": 45,\n \"enabled\": true,\n \"enabledCookieJar\": true,\n \"enabledExplore\": true,\n \"lastUpdateTime\": 1713454345818,\n \"respondTime\": 6224,\n \"ruleBookInfo\": {\n \"author\": \"h1@text##(.*) / (.*)##$2\",\n \"coverUrl\": \"img@src\",\n \"intro\": \".bookinfo_intro@text\",\n \"kind\": \".nav-mbx > a:nth-child(3)@text\",\n \"lastChapter\": \".update > a@text\",\n \"name\": \"h1@text##(.*) / (.*)##$1\"\n },\n \"ruleContent\": {\n \"content\": \"id.htmlContent@html##.*最快更新.*\"\n },\n \"ruleExplore\": {},\n \"ruleReview\": {},\n \"ruleSearch\": {\n \"author\": \"td.2@text\",\n \"bookList\": \".grid@tbody@tr!0\",\n \"bookUrl\": \"td.0@a@href\",\n \"checkKeyWord\": \"我的\",\n \"lastChapter\": \"td.1@text\",\n \"name\": \"td.0@text\",\n \"wordCount\": \"td.3@text\"\n },\n \"ruleToc\": {\n \"chapterList\": \"class.chapterlist.1@tag.li!0\",\n \"chapterName\": \"a.0@text\",\n \"chapterUrl\": \"a.0@href\"\n },\n \"searchUrl\": \"/modules/article/search.php?searchkey={{key}},{\\n\\\"charset\\\": \\\"gbk\\\"}\",\n \"weight\": 0\n}\n]",
Args: []Args{{
Name: "id",
Required: true,
Description: "有json代码块的文章id",
}},
}
func GetMemosJson(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, "参数解析错误", http.StatusInternalServerError)
}
id := r.Form.Get("id")
resp, err := http.Get(config.Cfg.MemosURL + "/api/v1/memo/" + id)
if err != nil {
http.Error(w, "无法获取文章数据", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
var info MemoInfo
err = json.NewDecoder(resp.Body).Decode(&info) // 使用json.NewDecoder解码JSON数据
if err != nil {
http.Error(w, "无法解析文章数据", http.StatusInternalServerError)
return
}
compileRegex := regexp.MustCompile("(?s)```json\n(.*?)```")
matchArr := compileRegex.FindStringSubmatch(info.Content)
if len(matchArr) > 0 {
source := make([]byte, 4096)
source = []byte(fmt.Sprintf("[%v]", matchArr[len(matchArr)-1]))
w.Header().Add("Content-Type", "application/octet-stream;charset=utf-8")
w.Header().Add("Content-Disposition", "attachment;filename="+info.Name+".json")
_, err = w.Write(source)
if id != "" {
resp, err := http.Get(config.Cfg.MemosURL + "/api/v1/memo/" + id)
if err != nil {
http.Error(w, "无法解析源数据", http.StatusInternalServerError)
http.Error(w, "无法获取文章数据", http.StatusInternalServerError)
return
}
return
defer resp.Body.Close()
var info MemoInfo
err = json.NewDecoder(resp.Body).Decode(&info) // 使用json.NewDecoder解码JSON数据
if err != nil {
http.Error(w, "无法解析文章数据", http.StatusInternalServerError)
return
}
compileRegex := regexp.MustCompile("(?s)```json\n(.*?)```")
matchArr := compileRegex.FindStringSubmatch(info.Content)
if len(matchArr) > 0 {
source := make([]byte, 4096)
source = []byte(fmt.Sprintf("[%v]", matchArr[len(matchArr)-1]))
w.Header().Add("Content-Type", "application/octet-stream;charset=utf-8")
w.Header().Add("Content-Disposition", "attachment;filename="+info.Name+".json")
_, err = w.Write(source)
if err != nil {
http.Error(w, "无法解析源数据", http.StatusInternalServerError)
return
}
return
}
Memos.ErrorInfoView(w, "无法解析页面请求"+id)
} else {
Memos.ErrorInfoView(w, "id为空")
}
http.Error(w, "无法解析页面请求"+id, http.StatusInternalServerError)
return
}