Feature: independent pages for user's artworks, illustrations and manga

This commit is contained in:
VnPower 2023-06-14 20:46:34 +07:00
parent f3f373f73f
commit b2c1b03060
Signed by: vnpower
GPG key ID: 881DE3DEB966106C
3 changed files with 70 additions and 28 deletions

View file

@ -248,7 +248,7 @@ func (p *PixivClient) GetArtworkComments(id string) ([]models.Comment, error) {
return body.Comments, nil return body.Comments, nil
} }
func (p *PixivClient) GetUserArtworksID(id string, page int) (*string, error) { func (p *PixivClient) GetUserArtworksID(id string, category string, page int) (*string, error) {
s, _ := p.TextRequest(fmt.Sprintf(UserArtworksURL, id)) s, _ := p.TextRequest(fmt.Sprintf(UserArtworksURL, id))
var pr models.PixivResponse var pr models.PixivResponse
@ -266,6 +266,7 @@ func (p *PixivClient) GetUserArtworksID(id string, page int) (*string, error) {
var idsString string var idsString string
var body struct { var body struct {
Illusts map[int]string `json:"illusts"` Illusts map[int]string `json:"illusts"`
Mangas map[int]string `json:"manga"`
} }
err = json.Unmarshal(pr.Body, &body) err = json.Unmarshal(pr.Body, &body)
if err != nil { if err != nil {
@ -273,8 +274,15 @@ func (p *PixivClient) GetUserArtworksID(id string, page int) (*string, error) {
} }
// Get the keys, because Pixiv only returns IDs (very evil) // Get the keys, because Pixiv only returns IDs (very evil)
for k := range body.Illusts { if category == "illustrations" || category == "artworks" {
ids = append(ids, k) for k := range body.Illusts {
ids = append(ids, k)
}
}
if category == "manga" || category == "artworks" {
for k := range body.Mangas {
ids = append(ids, k)
}
} }
// Reverse sort the ids // Reverse sort the ids
@ -297,7 +305,7 @@ func (p *PixivClient) GetUserArtworksID(id string, page int) (*string, error) {
return &idsString, nil return &idsString, nil
} }
func (p *PixivClient) GetUserArtworksCount(id string) (int, error) { func (p *PixivClient) GetUserArtworksCount(id string, category string) (int, error) {
s, _ := p.TextRequest(fmt.Sprintf(UserArtworksURL, id)) s, _ := p.TextRequest(fmt.Sprintf(UserArtworksURL, id))
var pr models.PixivResponse var pr models.PixivResponse
@ -310,16 +318,25 @@ func (p *PixivClient) GetUserArtworksCount(id string) (int, error) {
if pr.Error { if pr.Error {
return -1, errors.New(fmt.Sprintf("Pixiv returned error message: %s", pr.Message)) return -1, errors.New(fmt.Sprintf("Pixiv returned error message: %s", pr.Message))
} }
var body struct { var body struct {
Illusts map[int]string `json:"illusts"` Illusts map[int]string `json:"illusts"`
Mangas map[int]string `json:"manga"`
} }
err = json.Unmarshal(pr.Body, &body) err = json.Unmarshal(pr.Body, &body)
if err != nil { if err != nil {
return -1, err return -1, err
} }
return len(body.Illusts), nil count := 0
if category == "illustrations" || category == "artworks" {
count += len(body.Illusts)
}
if category == "manga" || category == "artworks" {
count += len(body.Mangas)
}
return count, nil
} }
func (p *PixivClient) GetRelatedArtworks(id string) ([]models.IllustShort, error) { func (p *PixivClient) GetRelatedArtworks(id string) ([]models.IllustShort, error) {
@ -385,11 +402,11 @@ func (p *PixivClient) GetUserArtworks(id string, ids string) ([]models.IllustSho
return works, nil return works, nil
} }
func (p *PixivClient) GetUserInformation(id string, page int) (*models.User, error) { func (p *PixivClient) GetUserInformation(id string, category string, page int) (*models.User, error) {
var user *models.User var user *models.User
var pr models.PixivResponse var pr models.PixivResponse
ids, err := p.GetUserArtworksID(id, page) ids, err := p.GetUserArtworksID(id, category, page)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -20,23 +20,42 @@
<p class="user-comment">{{ raw: User.Comment }}</p> <p class="user-comment">{{ raw: User.Comment }}</p>
</div> </div>
</div> </div>
<div> <div class="switcher">
{{ range User.FrequentTags }} <span class="switch-title">Category</span>
<a href="/tags/{{ .Name }}"> <a href="/users/{{ User.ID }}/artworks#checkpoint" class="switch-button"
<div class="tag-container" style="background-color: {{ randomColor() }}"> >All</a
{{ if ! .TranslatedName }} >
<div class="main">{{ .Name }}</div> <a
<div class="sub">#</div> href="/users/{{ User.ID }}/illustrations#checkpoint"
{{ else }} class="switch-button"
<div class="main">{{ .TranslatedName }}</div> >Illustrations</a
<div class="sub">#{{ .Name }}</div> >
{{ end }} <a href="/users/{{ User.ID }}/manga#checkpoint" class="switch-button"
</div> >Mangas</a
</a> >
{{ end }}
</div> </div>
<div> <div>
<h1>Illustrations and Mangas</h1> <h1 id="checkpoint">Illustrations and Mangas</h1>
<div>
{{ range User.FrequentTags }}
<a href="/tags/{{ .Name }}">
<div
class="tag-container"
style="background-color: {{ randomColor() }}"
>
{{ if ! .TranslatedName }}
<div class="main">{{ .Name }}</div>
<div class="sub">#</div>
{{ else }}
<div class="main">{{ .TranslatedName }}</div>
<div class="sub">#{{ .Name }}</div>
{{ end }}
</div>
</a>
{{ end }}
</div>
{{ include "small-tn" User.Artworks }} {{ include "small-tn" User.Artworks }}
</div> </div>
<div class="pagination"> <div class="pagination">

View file

@ -27,12 +27,13 @@ func artwork_page(c *fiber.Ctx) error {
related, _ := PC.GetRelatedArtworks(id) related, _ := PC.GetRelatedArtworks(id)
comments, _ := PC.GetArtworkComments(id) comments, _ := PC.GetArtworkComments(id)
artist_info, err := PC.GetUserInformation(illust.UserID, 1) artist_info, err := PC.GetUserInformation(illust.UserID, "all", 1)
if err != nil { if err != nil {
return err return err
} }
// Optimize this
return c.Render("artwork", fiber.Map{ return c.Render("artwork", fiber.Map{
"Illust": illust, "Illust": illust,
"Related": related, "Related": related,
@ -61,15 +62,19 @@ func user_page(c *fiber.Ctx) error {
if _, err := strconv.Atoi(id); err != nil { if _, err := strconv.Atoi(id); err != nil {
return err return err
} }
category := c.Params("category", "artworks")
if !(category == "artworks" || category == "illustrations" || category == "manga") {
return errors.New("Invalid work category: only illustrations, manga and artworks are available")
}
page := c.Query("page", "1") page := c.Query("page", "1")
pageInt, _ := strconv.Atoi(page) pageInt, _ := strconv.Atoi(page)
user, err := PC.GetUserInformation(id, pageInt) user, err := PC.GetUserInformation(id, category, pageInt)
if err != nil { if err != nil {
return err return err
} }
worksCount, _ := PC.GetUserArtworksCount(id) worksCount, _ := PC.GetUserArtworksCount(id, category)
pageLimit := math.Ceil(float64(worksCount)/30.0) + 1.0 pageLimit := math.Ceil(float64(worksCount)/30.0) + 1.0
return c.Render("user", fiber.Map{"Title": user.Name, "User": user, "PageLimit": int(pageLimit), "Page": pageInt}) return c.Render("user", fiber.Map{"Title": user.Name, "User": user, "PageLimit": int(pageLimit), "Page": pageInt})
@ -189,8 +194,9 @@ func SetupRoutes(r *fiber.App) {
PC.SetUserAgent(configs.UserAgent) PC.SetUserAgent(configs.UserAgent)
r.Get("/", index_page) r.Get("/", index_page)
r.Get("artworks/:id", artwork_page) r.Get("artworks/:id/", artwork_page)
r.Get("users/:id", user_page) r.Get("users/:id/", user_page)
r.Get("users/:id/:category", user_page)
r.Get("newest", newest_artworks_page) r.Get("newest", newest_artworks_page)
r.Get("ranking", ranking_page) r.Get("ranking", ranking_page)
r.Get("tags/:name", search_page) r.Get("tags/:name", search_page)