Blog/api/memos.go

77 lines
3.5 KiB
Go

package api
import (
"blog/config"
"encoding/json"
"fmt"
"net/http"
"regexp"
)
type MemoInfo struct {
Id int `json:"id"`
Name string `json:"name"`
RowStatus string `json:"rowStatus"`
CreatorId int `json:"creatorId"`
CreatedTs int `json:"createdTs"`
UpdatedTs int `json:"updatedTs"`
DisplayTs int `json:"displayTs"`
Content string `json:"content"`
Visibility string `json:"visibility"`
Pinned bool `json:"pinned"`
CreatorName string `json:"creatorName"`
CreatorUsername string `json:"creatorUsername"`
ResourceList []interface{} `json:"resourceList"`
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")
if 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 err != nil {
http.Error(w, "无法解析源数据", http.StatusInternalServerError)
return
}
return
}
Memos.ErrorInfoView(w, "无法解析页面请求"+id)
} else {
Memos.ErrorInfoView(w, "id为空")
}
return
}