Feature: independent pages for user's artworks, illustrations and manga
This commit is contained in:
parent
f3f373f73f
commit
b2c1b03060
3 changed files with 70 additions and 28 deletions
|
@ -248,7 +248,7 @@ func (p *PixivClient) GetArtworkComments(id string) ([]models.Comment, error) {
|
|||
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))
|
||||
|
||||
var pr models.PixivResponse
|
||||
|
@ -266,6 +266,7 @@ func (p *PixivClient) GetUserArtworksID(id string, page int) (*string, error) {
|
|||
var idsString string
|
||||
var body struct {
|
||||
Illusts map[int]string `json:"illusts"`
|
||||
Mangas map[int]string `json:"manga"`
|
||||
}
|
||||
err = json.Unmarshal(pr.Body, &body)
|
||||
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)
|
||||
for k := range body.Illusts {
|
||||
ids = append(ids, k)
|
||||
if category == "illustrations" || category == "artworks" {
|
||||
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
|
||||
|
@ -297,7 +305,7 @@ func (p *PixivClient) GetUserArtworksID(id string, page int) (*string, error) {
|
|||
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))
|
||||
|
||||
var pr models.PixivResponse
|
||||
|
@ -310,16 +318,25 @@ func (p *PixivClient) GetUserArtworksCount(id string) (int, error) {
|
|||
if pr.Error {
|
||||
return -1, errors.New(fmt.Sprintf("Pixiv returned error message: %s", pr.Message))
|
||||
}
|
||||
|
||||
var body struct {
|
||||
Illusts map[int]string `json:"illusts"`
|
||||
Mangas map[int]string `json:"manga"`
|
||||
}
|
||||
err = json.Unmarshal(pr.Body, &body)
|
||||
if err != nil {
|
||||
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) {
|
||||
|
@ -385,11 +402,11 @@ func (p *PixivClient) GetUserArtworks(id string, ids string) ([]models.IllustSho
|
|||
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 pr models.PixivResponse
|
||||
|
||||
ids, err := p.GetUserArtworksID(id, page)
|
||||
ids, err := p.GetUserArtworksID(id, category, page)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -20,23 +20,42 @@
|
|||
<p class="user-comment">{{ raw: User.Comment }}</p>
|
||||
</div>
|
||||
</div>
|
||||
<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 class="switcher">
|
||||
<span class="switch-title">Category</span>
|
||||
<a href="/users/{{ User.ID }}/artworks#checkpoint" class="switch-button"
|
||||
>All</a
|
||||
>
|
||||
<a
|
||||
href="/users/{{ User.ID }}/illustrations#checkpoint"
|
||||
class="switch-button"
|
||||
>Illustrations</a
|
||||
>
|
||||
<a href="/users/{{ User.ID }}/manga#checkpoint" class="switch-button"
|
||||
>Mangas</a
|
||||
>
|
||||
</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 }}
|
||||
</div>
|
||||
<div class="pagination">
|
||||
|
|
|
@ -27,12 +27,13 @@ func artwork_page(c *fiber.Ctx) error {
|
|||
|
||||
related, _ := PC.GetRelatedArtworks(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 {
|
||||
return err
|
||||
}
|
||||
|
||||
// Optimize this
|
||||
return c.Render("artwork", fiber.Map{
|
||||
"Illust": illust,
|
||||
"Related": related,
|
||||
|
@ -61,15 +62,19 @@ func user_page(c *fiber.Ctx) error {
|
|||
if _, err := strconv.Atoi(id); err != nil {
|
||||
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")
|
||||
|
||||
pageInt, _ := strconv.Atoi(page)
|
||||
user, err := PC.GetUserInformation(id, pageInt)
|
||||
user, err := PC.GetUserInformation(id, category, pageInt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
worksCount, _ := PC.GetUserArtworksCount(id)
|
||||
worksCount, _ := PC.GetUserArtworksCount(id, category)
|
||||
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})
|
||||
|
@ -189,8 +194,9 @@ func SetupRoutes(r *fiber.App) {
|
|||
PC.SetUserAgent(configs.UserAgent)
|
||||
|
||||
r.Get("/", index_page)
|
||||
r.Get("artworks/:id", artwork_page)
|
||||
r.Get("users/:id", user_page)
|
||||
r.Get("artworks/:id/", artwork_page)
|
||||
r.Get("users/:id/", user_page)
|
||||
r.Get("users/:id/:category", user_page)
|
||||
r.Get("newest", newest_artworks_page)
|
||||
r.Get("ranking", ranking_page)
|
||||
r.Get("tags/:name", search_page)
|
||||
|
|
Loading…
Reference in a new issue