From 553bf45fa9f30815cfb1ceba138dfaff404abc86 Mon Sep 17 00:00:00 2001 From: VnPower Date: Sat, 20 May 2023 11:16:25 +0700 Subject: [PATCH] Feature: Added PageItems to configurate number of items for each list page --- config.example.yml | 5 ++++- configs/config.go | 3 ++- handler/pixiv.go | 7 ++++--- views/routes.go | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/config.example.yml b/config.example.yml index e2fb9aa..d7476dd 100644 --- a/config.example.yml +++ b/config.example.yml @@ -3,4 +3,7 @@ # It is better to use a decoy account, instead of using your main account's cookie. PHPSESSID: -userAgent: Mozilla/5.0 +UserAgent: Mozilla/5.0 + +# Number of items in each list page +PageItems: 30 diff --git a/configs/config.go b/configs/config.go index b06b09b..871a1ab 100644 --- a/configs/config.go +++ b/configs/config.go @@ -11,7 +11,8 @@ var Configs Conf type Conf struct { PHPSESSID string `yaml:"PHPSESSID"` - UserAgent string `yaml:"userAgent"` + UserAgent string `yaml:"UserAgent"` + PageItems int `yaml:"PageItems"` } func (conf *Conf) ReadConfig() { diff --git a/handler/pixiv.go b/handler/pixiv.go index 098973e..2b34ea7 100644 --- a/handler/pixiv.go +++ b/handler/pixiv.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "math" "net/http" + "pixivfe/configs" "pixivfe/models" "regexp" "sort" @@ -24,7 +25,7 @@ type PixivClient struct { 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=30" + ArtworkRelatedURL = "https://www.pixiv.net/ajax/illust/%s/recommend/init?limit=%d" 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" @@ -228,7 +229,7 @@ func (p *PixivClient) GetUserArtworksID(id string, page int) (*string, error) { sort.Sort(sort.Reverse(sort.IntSlice(ids))) worksNumber := float64(len(ids)) - worksPerPage := 30.0 + worksPerPage := float64(configs.Configs.PageItems) if page < 1 || float64(page) > math.Ceil(worksNumber/worksPerPage)+1.0 { return nil, errors.New("Page overflow") @@ -273,7 +274,7 @@ func (p *PixivClient) GetUserArtworksCount(id string) (int, error) { } func (p *PixivClient) GetRelatedArtworks(id string) ([]models.IllustShort, error) { - url := fmt.Sprintf(ArtworkRelatedURL, id) + url := fmt.Sprintf(ArtworkRelatedURL, id, configs.Configs.PageItems) var pr models.PixivResponse diff --git a/views/routes.go b/views/routes.go index a3bfa47..5dc3446 100644 --- a/views/routes.go +++ b/views/routes.go @@ -51,7 +51,7 @@ func user_page(c *gin.Context) { user, _ := PC.GetUserInformation(id, pageInt) worksCount, _ := PC.GetUserArtworksCount(id) - pageLimit := math.Ceil(float64(worksCount)/30) + 1.0 + pageLimit := math.Ceil(float64(worksCount)/float64(configs.Configs.PageItems)) + 1.0 c.HTML(http.StatusOK, "user.html", gin.H{"User": user, "PageLimit": int(pageLimit), "Page": pageInt}) }