From 1fc496fbe221ecb2d3c050247cea459633a9f683 Mon Sep 17 00:00:00 2001 From: VnPower Date: Sun, 21 May 2023 22:09:13 +0700 Subject: [PATCH] Feature: newest artworks --- handler/pixiv.go | 44 +++++++++++++++++++++++++++++++++++++------- template/list.html | 8 ++++++++ views/routes.go | 18 ++++++++++++++++++ 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/handler/pixiv.go b/handler/pixiv.go index 2b34ea7..9853cc8 100644 --- a/handler/pixiv.go +++ b/handler/pixiv.go @@ -26,6 +26,7 @@ const ( ArtworkInformationURL = "https://www.pixiv.net/ajax/illust/%s" ArtworkImagesURL = "https://www.pixiv.net/ajax/illust/%s/pages" ArtworkRelatedURL = "https://www.pixiv.net/ajax/illust/%s/recommend/init?limit=%d" + ArtworkNewestURL = "https://www.pixiv.net/ajax/illust/new?limit=200&type=%s&r18=%s&lastId=%s" UserInformationURL = "https://www.pixiv.net/ajax/user/%s?full=1" UserArtworksURL = "https://www.pixiv.net/ajax/user/%s/profile/all" UserArtworksFullURL = "https://www.pixiv.net/ajax/user/%s/profile/illusts?work_category=illustManga&is_first_page=0&lang=en%s" @@ -259,18 +260,12 @@ func (p *PixivClient) GetUserArtworksCount(id string) (int, error) { return -1, errors.New(pr.Message) } - var count int var body struct { Illusts map[int]string `json:"illusts"` } err = json.Unmarshal(pr.Body, &body) - // Get the keys, because Pixiv only returns IDs (very evil) - for range body.Illusts { - count++ - } - - return count, nil + return len(body.Illusts), nil } func (p *PixivClient) GetRelatedArtworks(id string) ([]models.IllustShort, error) { @@ -376,3 +371,38 @@ func (p *PixivClient) GetUserInformation(id string, page int) (*models.User, err return user, nil } + +func (p *PixivClient) GetNewestArtworks(worktype string, r18 string) ([]models.IllustShort, error) { + + var pr models.PixivResponse + var newWorks []models.IllustShort + lastID := "0" + + for i := 0; i < 10; i++ { + url := fmt.Sprintf(ArtworkNewestURL, worktype, r18, lastID) + + s, err := p.TextRequest(url) + if err != nil { + return nil, err + } + + err = json.Unmarshal([]byte(s), &pr) + + var body struct { + Illusts []models.IllustShort `json:"illusts"` + LastID string `json:"lastId"` + } + + err = json.Unmarshal([]byte(pr.Body), &body) + if err != nil { + return nil, err + } + newWorks = append(newWorks, body.Illusts...) + + lastID = body.LastID + } + + println(len(newWorks)) + + return newWorks, nil +} diff --git a/template/list.html b/template/list.html index e69de29..1c5c7c7 100644 --- a/template/list.html +++ b/template/list.html @@ -0,0 +1,8 @@ +{{ template "header.html" }} + +
+

{{ .Title }}

+
{{ template "small-tn.html" .Items }}
+
+ +{{ template "footer.html" }} diff --git a/views/routes.go b/views/routes.go index 5dc3446..f182add 100644 --- a/views/routes.go +++ b/views/routes.go @@ -56,6 +56,23 @@ func user_page(c *gin.Context) { c.HTML(http.StatusOK, "user.html", gin.H{"User": user, "PageLimit": int(pageLimit), "Page": pageInt}) } +func newestArtworksPage(c *gin.Context) { + worktype, ok := c.GetQuery("type") + + if !ok { + worktype = "illust" + } + r18, ok := c.GetQuery("r18") + + if !ok { + r18 = "false" + } + + works, _ := PC.GetNewestArtworks(worktype, r18) + + c.HTML(http.StatusOK, "list.html", gin.H{"Title": "Newest works from all users", "Items": works}) +} + func NewPixivClient(timeout int) *models.PixivClient { transport := &http.Transport{Proxy: http.ProxyFromEnvironment} client := &http.Client{ @@ -80,4 +97,5 @@ func SetupRoutes(r *gin.Engine) { // r.GET("/", index_page) r.GET("artworks/:id", artwork_page) r.GET("users/:id", user_page) + r.GET("newest", newestArtworksPage) }