misc.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package handler
  2. import (
  3. "fmt"
  4. "html/template"
  5. "net/http"
  6. "strings"
  7. "time"
  8. "codeberg.org/vnpower/pixivfe/models"
  9. "github.com/goccy/go-json"
  10. "golang.org/x/net/html"
  11. )
  12. func get_weekday(n time.Weekday) int {
  13. switch n {
  14. case time.Sunday:
  15. return 1
  16. case time.Monday:
  17. return 2
  18. case time.Tuesday:
  19. return 3
  20. case time.Wednesday:
  21. return 4
  22. case time.Thursday:
  23. return 5
  24. case time.Friday:
  25. return 6
  26. case time.Saturday:
  27. return 7
  28. }
  29. return 0
  30. }
  31. func (p *PixivClient) GetNewestArtworks(worktype string, r18 string) ([]models.IllustShort, error) {
  32. var newWorks []models.IllustShort
  33. lastID := "0"
  34. for i := 0; i < 10; i++ {
  35. URL := fmt.Sprintf(ArtworkNewestURL, worktype, r18, lastID)
  36. response, err := p.PixivRequest(URL)
  37. if err != nil {
  38. return nil, err
  39. }
  40. var body struct {
  41. Illusts []models.IllustShort `json:"illusts"`
  42. LastID string `json:"lastId"`
  43. }
  44. err = json.Unmarshal([]byte(response), &body)
  45. if err != nil {
  46. return nil, err
  47. }
  48. newWorks = append(newWorks, body.Illusts...)
  49. lastID = body.LastID
  50. }
  51. return newWorks, nil
  52. }
  53. func (p *PixivClient) GetRanking(mode string, content string, date string, page string) (models.RankingResponse, error) {
  54. // Ranking data is formatted differently
  55. var pr models.RankingResponse
  56. if len(date) > 0 {
  57. date = "&date=" + date
  58. }
  59. url := fmt.Sprintf(ArtworkRankingURL, mode, content, date, page)
  60. s, err := p.TextRequest(url)
  61. if err != nil {
  62. return pr, err
  63. }
  64. err = json.Unmarshal([]byte(s), &pr)
  65. if err != nil {
  66. return pr, err
  67. }
  68. pr.PrevDate = strings.ReplaceAll(string(pr.PrevDateRaw[:]), "\"", "")
  69. pr.NextDate = strings.ReplaceAll(string(pr.NextDateRaw[:]), "\"", "")
  70. return pr, nil
  71. }
  72. func (p *PixivClient) GetSearch(artworkType string, name string, order string, age_settings string, page string) (*models.SearchResult, error) {
  73. URL := fmt.Sprintf(SearchArtworksURL, artworkType, name, order, age_settings, page)
  74. response, err := p.PixivRequest(URL)
  75. if err != nil {
  76. return nil, err
  77. }
  78. // IDK how to do better than this lol
  79. temp := strings.ReplaceAll(string(response), `"illust"`, `"works"`)
  80. temp = strings.ReplaceAll(temp, `"manga"`, `"works"`)
  81. temp = strings.ReplaceAll(temp, `"illustManga"`, `"works"`)
  82. var resultRaw struct {
  83. *models.SearchResult
  84. ArtworksRaw json.RawMessage `json:"works"`
  85. }
  86. var artworks models.SearchArtworks
  87. var result *models.SearchResult
  88. err = json.Unmarshal([]byte(temp), &resultRaw)
  89. if err != nil {
  90. return nil, err
  91. }
  92. result = resultRaw.SearchResult
  93. err = json.Unmarshal([]byte(resultRaw.ArtworksRaw), &artworks)
  94. if err != nil {
  95. return nil, err
  96. }
  97. result.Artworks = artworks
  98. return result, nil
  99. }
  100. func (p *PixivClient) GetDiscoveryArtwork(mode string, count int) ([]models.IllustShort, error) {
  101. var artworks []models.IllustShort
  102. for count > 0 {
  103. itemsForRequest := min(100, count)
  104. count -= itemsForRequest
  105. URL := fmt.Sprintf(ArtworkDiscoveryURL, mode, itemsForRequest)
  106. response, err := p.PixivRequest(URL)
  107. if err != nil {
  108. return nil, err
  109. }
  110. var thumbnail struct {
  111. Data json.RawMessage `json:"thumbnails"`
  112. }
  113. err = json.Unmarshal([]byte(response), &thumbnail)
  114. if err != nil {
  115. return nil, err
  116. }
  117. var body struct {
  118. Artworks []models.IllustShort `json:"illust"`
  119. }
  120. err = json.Unmarshal([]byte(thumbnail.Data), &body)
  121. if err != nil {
  122. return nil, err
  123. }
  124. artworks = append(artworks, body.Artworks...)
  125. }
  126. return artworks, nil
  127. }
  128. func (p *PixivClient) GetRankingLog(mode string, year, month int, image_proxy string) (template.HTML, error) {
  129. url := fmt.Sprintf("https://www.pixiv.net/ranking_log.php?mode=%s&date=%d%02d", mode, year, month)
  130. resp, err := http.Get(url)
  131. if err != nil {
  132. return "", err
  133. }
  134. defer resp.Body.Close()
  135. // Use the html package to parse the response body from the request
  136. doc, err := html.Parse(resp.Body)
  137. if err != nil {
  138. return "", err
  139. }
  140. // Find and print all links on the web page
  141. var links []string
  142. var link func(*html.Node)
  143. link = func(n *html.Node) {
  144. if n.Type == html.ElementNode && n.Data == "img" {
  145. for _, a := range n.Attr {
  146. if a.Key == "data-src" {
  147. // adds a new link entry when the attribute matches
  148. links = append(links, models.ProxyImage(a.Val, image_proxy))
  149. }
  150. }
  151. }
  152. // traverses the HTML of the webpage from the first child node
  153. for c := n.FirstChild; c != nil; c = c.NextSibling {
  154. link(c)
  155. }
  156. }
  157. link(doc)
  158. // now := time.Now()
  159. // yearNow := now.Year()
  160. // monthNow := now.Month()
  161. lastMonth := time.Date(year, time.Month(month), 0, 0, 0, 0, 0, time.UTC)
  162. thisMonth := time.Date(year, time.Month(month+1), 0, 0, 0, 0, 0, time.UTC)
  163. renderString := ""
  164. for i := 0; i < get_weekday(lastMonth.Weekday()); i++ {
  165. renderString += "<div class=\"calendar-node calendar-node-empty\"></div>"
  166. }
  167. for i := 0; i < thisMonth.Day(); i++ {
  168. date := fmt.Sprintf("%d%02d%02d", year, month, i+1)
  169. if len(links) > i {
  170. renderString += fmt.Sprintf(`<a href="/ranking?mode=%s&date=%s"><div class="calendar-node" style="background-image: url(%s)"><span>%d</span></div></a>`, mode, date, links[i], i+1)
  171. } else {
  172. renderString += fmt.Sprintf(`<div class="calendar-node"><span>%d</span></div>`, i+1)
  173. }
  174. }
  175. return template.HTML(renderString), nil
  176. }