123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- package core
- import (
- "fmt"
- "regexp"
- "time"
- "codeberg.org/vnpower/pixivfe/v2/session"
- "github.com/goccy/go-json"
- "net/http"
- )
- type Novel struct {
- Bookmarks int `json:"bookmarkCount"`
- CommentCount int `json:"commentCount"`
- MarkerCount int `json:"markerCount"`
- CreateDate time.Time `json:"createDate"`
- UploadDate time.Time `json:"uploadDate"`
- Description string `json:"description"`
- ID string `json:"id"`
- Title string `json:"title"`
- Likes int `json:"likeCount"`
- Pages int `json:"pageCount"`
- UserID string `json:"userId"`
- UserName string `json:"userName"`
- Views int `json:"viewCount"`
- IsOriginal bool `json:"isOriginal"`
- IsBungei bool `json:"isBungei"`
- XRestrict int `json:"xRestrict"`
- Restrict int `json:"restrict"`
- Content string `json:"content"`
- CoverURL string `json:"coverUrl"`
- IsBookmarkable bool `json:"isBookmarkable"`
- BookmarkData *BookmarkData `json:"bookmarkData"`
- LikeData bool `json:"likeData"`
- // PollData any `json:"pollData"`
- // Marker any `json:"marker"`
- Tags struct {
- AuthorID string `json:"authorId"`
- IsLocked bool `json:"isLocked"`
- Tags []struct {
- Name string `json:"tag"`
- } `json:"tags"`
- Writable bool `json:"writable"`
- } `json:"tags"`
- // SeriesNavData any `json:"seriesNavData"`
- HasGlossary bool `json:"hasGlossary"`
- IsUnlisted bool `json:"isUnlisted"`
- // seen values: zh-cn, ja
- Language string `json:"language"`
- CommentOff int `json:"commentOff"`
- CharacterCount int `json:"characterCount"`
- WordCount int `json:"wordCount"`
- UseWordCount bool `json:"useWordCount"`
- ReadingTime int `json:"readingTime"`
- AiType int `json:"aiType"`
- Genre string `json:"genre"`
- Settings struct {
- ViewMode int `json:"viewMode"`
- // ...
- } `json:"suggestedSettings"`
- }
- type NovelBrief struct {
- ID string `json:"id"`
- Title string `json:"title"`
- XRestrict int `json:"xRestrict"`
- Restrict int `json:"restrict"`
- CoverURL string `json:"url"`
- Tags []string `json:"tags"`
- UserID string `json:"userId"`
- UserName string `json:"userName"`
- UserAvatar string `json:"profileImageUrl"`
- TextCount int `json:"textCount"`
- WordCount int `json:"wordCount"`
- ReadingTime int `json:"readingTime"`
- Description string `json:"description"`
- IsBookmarkable bool `json:"isBookmarkable"`
- BookmarkData *BookmarkData `json:"bookmarkData"`
- Bookmarks int `json:"bookmarkCount"`
- IsOriginal bool `json:"isOriginal"`
- CreateDate time.Time `json:"createDate"`
- UpdateDate time.Time `json:"updateDate"`
- IsMasked bool `json:"isMasked"`
- SeriesID string `json:"seriesId"`
- SeriesTitle string `json:"seriesTitle"`
- IsUnlisted bool `json:"isUnlisted"`
- AiType int `json:"aiType"`
- Genre string `json:"genre"`
- }
- func GetNovelByID(r *http.Request, id string) (Novel, error) {
- var novel Novel
- URL := GetNovelURL(id)
- response, err := API_GET_UnwrapJson(r.Context(), URL, "")
- if err != nil {
- return novel, err
- }
- response = session.ProxyImageUrl(r, response)
- err = json.Unmarshal([]byte(response), &novel)
- if err != nil {
- return novel, err
- }
- // Novel embedded illusts
- re_r := regexp.MustCompile("\\[pixivimage:(\\d+.\\d+)\\]")
- re_d := regexp.MustCompile("\\d+.\\d+")
- re_t := regexp.MustCompile(`\"original\":\"(.+?)\"`)
- novel.Content = re_r.ReplaceAllStringFunc(novel.Content, func(s string) string {
- illustid := re_d.FindString(s)
- URL := GetInsertIllustURL(novel.ID, illustid)
- response, err := API_GET_UnwrapJson(r.Context(), URL, "")
- if err != nil {
- return "Cannot insert illust" + illustid
- }
- url := re_t.FindString(response)
- url = session.ProxyImageUrl(r, url[11:]) // truncate the "original":
- return fmt.Sprintf(`<img src=%s alt="%s"/>`, url, s)
- })
- return novel, nil
- }
- func GetNovelRelated(r *http.Request, id string) ([]NovelBrief, error) {
- var novels struct {
- List []NovelBrief `json:"novels"`
- }
- // hard-coded value, may change
- URL := GetNovelRelatedURL(id, 50)
- response, err := API_GET_UnwrapJson(r.Context(), URL, "")
- if err != nil {
- return novels.List, err
- }
- response = session.ProxyImageUrl(r, response)
- err = json.Unmarshal([]byte(response), &novels)
- if err != nil {
- return novels.List, err
- }
- return novels.List, nil
- }
|