novel.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package core
  2. import (
  3. "fmt"
  4. "regexp"
  5. "time"
  6. "codeberg.org/vnpower/pixivfe/v2/session"
  7. "github.com/goccy/go-json"
  8. "net/http"
  9. )
  10. type Novel struct {
  11. Bookmarks int `json:"bookmarkCount"`
  12. CommentCount int `json:"commentCount"`
  13. MarkerCount int `json:"markerCount"`
  14. CreateDate time.Time `json:"createDate"`
  15. UploadDate time.Time `json:"uploadDate"`
  16. Description string `json:"description"`
  17. ID string `json:"id"`
  18. Title string `json:"title"`
  19. Likes int `json:"likeCount"`
  20. Pages int `json:"pageCount"`
  21. UserID string `json:"userId"`
  22. UserName string `json:"userName"`
  23. Views int `json:"viewCount"`
  24. IsOriginal bool `json:"isOriginal"`
  25. IsBungei bool `json:"isBungei"`
  26. XRestrict int `json:"xRestrict"`
  27. Restrict int `json:"restrict"`
  28. Content string `json:"content"`
  29. CoverURL string `json:"coverUrl"`
  30. IsBookmarkable bool `json:"isBookmarkable"`
  31. BookmarkData *BookmarkData `json:"bookmarkData"`
  32. LikeData bool `json:"likeData"`
  33. // PollData any `json:"pollData"`
  34. // Marker any `json:"marker"`
  35. Tags struct {
  36. AuthorID string `json:"authorId"`
  37. IsLocked bool `json:"isLocked"`
  38. Tags []struct {
  39. Name string `json:"tag"`
  40. } `json:"tags"`
  41. Writable bool `json:"writable"`
  42. } `json:"tags"`
  43. // SeriesNavData any `json:"seriesNavData"`
  44. HasGlossary bool `json:"hasGlossary"`
  45. IsUnlisted bool `json:"isUnlisted"`
  46. // seen values: zh-cn, ja
  47. Language string `json:"language"`
  48. CommentOff int `json:"commentOff"`
  49. CharacterCount int `json:"characterCount"`
  50. WordCount int `json:"wordCount"`
  51. UseWordCount bool `json:"useWordCount"`
  52. ReadingTime int `json:"readingTime"`
  53. AiType int `json:"aiType"`
  54. Genre string `json:"genre"`
  55. Settings struct {
  56. ViewMode int `json:"viewMode"`
  57. // ...
  58. } `json:"suggestedSettings"`
  59. }
  60. type NovelBrief struct {
  61. ID string `json:"id"`
  62. Title string `json:"title"`
  63. XRestrict int `json:"xRestrict"`
  64. Restrict int `json:"restrict"`
  65. CoverURL string `json:"url"`
  66. Tags []string `json:"tags"`
  67. UserID string `json:"userId"`
  68. UserName string `json:"userName"`
  69. UserAvatar string `json:"profileImageUrl"`
  70. TextCount int `json:"textCount"`
  71. WordCount int `json:"wordCount"`
  72. ReadingTime int `json:"readingTime"`
  73. Description string `json:"description"`
  74. IsBookmarkable bool `json:"isBookmarkable"`
  75. BookmarkData *BookmarkData `json:"bookmarkData"`
  76. Bookmarks int `json:"bookmarkCount"`
  77. IsOriginal bool `json:"isOriginal"`
  78. CreateDate time.Time `json:"createDate"`
  79. UpdateDate time.Time `json:"updateDate"`
  80. IsMasked bool `json:"isMasked"`
  81. SeriesID string `json:"seriesId"`
  82. SeriesTitle string `json:"seriesTitle"`
  83. IsUnlisted bool `json:"isUnlisted"`
  84. AiType int `json:"aiType"`
  85. Genre string `json:"genre"`
  86. }
  87. func GetNovelByID(r *http.Request, id string) (Novel, error) {
  88. var novel Novel
  89. URL := GetNovelURL(id)
  90. response, err := API_GET_UnwrapJson(r.Context(), URL, "")
  91. if err != nil {
  92. return novel, err
  93. }
  94. response = session.ProxyImageUrl(r, response)
  95. err = json.Unmarshal([]byte(response), &novel)
  96. if err != nil {
  97. return novel, err
  98. }
  99. // Novel embedded illusts
  100. re_r := regexp.MustCompile("\\[pixivimage:(\\d+.\\d+)\\]")
  101. re_d := regexp.MustCompile("\\d+.\\d+")
  102. re_t := regexp.MustCompile(`\"original\":\"(.+?)\"`)
  103. novel.Content = re_r.ReplaceAllStringFunc(novel.Content, func(s string) string {
  104. illustid := re_d.FindString(s)
  105. URL := GetInsertIllustURL(novel.ID, illustid)
  106. response, err := API_GET_UnwrapJson(r.Context(), URL, "")
  107. if err != nil {
  108. return "Cannot insert illust" + illustid
  109. }
  110. url := re_t.FindString(response)
  111. url = session.ProxyImageUrl(r, url[11:]) // truncate the "original":
  112. return fmt.Sprintf(`<img src=%s alt="%s"/>`, url, s)
  113. })
  114. return novel, nil
  115. }
  116. func GetNovelRelated(r *http.Request, id string) ([]NovelBrief, error) {
  117. var novels struct {
  118. List []NovelBrief `json:"novels"`
  119. }
  120. // hard-coded value, may change
  121. URL := GetNovelRelatedURL(id, 50)
  122. response, err := API_GET_UnwrapJson(r.Context(), URL, "")
  123. if err != nil {
  124. return novels.List, err
  125. }
  126. response = session.ProxyImageUrl(r, response)
  127. err = json.Unmarshal([]byte(response), &novels)
  128. if err != nil {
  129. return novels.List, err
  130. }
  131. return novels.List, nil
  132. }