Optimize: artwork page
This commit is contained in:
parent
c1b4de778d
commit
3ae1f9b1fc
4 changed files with 120 additions and 39 deletions
|
@ -3,7 +3,6 @@ package handler
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/goccy/go-json"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"net/http"
|
||||
|
@ -11,6 +10,8 @@ import (
|
|||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
type PixivClient struct {
|
||||
|
@ -33,6 +34,7 @@ const (
|
|||
SearchArtworksURL = "https://www.pixiv.net/ajax/search/%s/%s?order=%s&mode=%s&p=%s"
|
||||
SearchTopURL = "https://www.pixiv.net/ajax/search/top/%s"
|
||||
UserInformationURL = "https://www.pixiv.net/ajax/user/%s?full=1"
|
||||
UserBasicInformationURL = "https://www.pixiv.net/ajax/user/%s"
|
||||
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"
|
||||
FrequentTagsURL = "https://www.pixiv.net/ajax/tags/frequent/illust?%s"
|
||||
|
@ -186,6 +188,8 @@ func (p *PixivClient) GetArtworkByID(id string) (*models.Illust, error) {
|
|||
|
||||
var illust struct {
|
||||
*models.Illust
|
||||
|
||||
Recent map[int]any `json:"userIllusts"`
|
||||
RawTags json.RawMessage `json:"tags"`
|
||||
}
|
||||
|
||||
|
@ -203,6 +207,36 @@ func (p *PixivClient) GetArtworkByID(id string) (*models.Illust, error) {
|
|||
|
||||
illust.Images = images
|
||||
|
||||
// Get recent artworks
|
||||
var ids []int
|
||||
idsString := ""
|
||||
|
||||
for k := range illust.Recent {
|
||||
ids = append(ids, k)
|
||||
}
|
||||
|
||||
sort.Sort(sort.Reverse(sort.IntSlice(ids)))
|
||||
|
||||
count := len(ids)
|
||||
for i := 0; i < 30 && i < count; i++ {
|
||||
idsString += fmt.Sprintf("&ids[]=%d", ids[i])
|
||||
}
|
||||
|
||||
recent, err := p.GetUserArtworks(illust.UserID, idsString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
illust.RecentWorks = recent
|
||||
|
||||
// Get basic user information (the URL above does not contain avatars)
|
||||
userInfo, err := p.GetUserBasicInformation(illust.UserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
illust.User = userInfo
|
||||
|
||||
// Extract tags
|
||||
var tags struct {
|
||||
Tags []struct {
|
||||
|
@ -343,15 +377,17 @@ func (p *PixivClient) GetRelatedArtworks(id string) ([]models.IllustShort, error
|
|||
}
|
||||
|
||||
func (p *PixivClient) GetUserArtworks(id string, ids string) ([]models.IllustShort, error) {
|
||||
|
||||
url := fmt.Sprintf(UserArtworksFullURL, id, ids)
|
||||
|
||||
var pr models.PixivResponse
|
||||
var works []models.IllustShort
|
||||
|
||||
s, _ := p.TextRequest(url)
|
||||
s, err := p.TextRequest(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err := json.Unmarshal([]byte(s), &pr)
|
||||
err = json.Unmarshal([]byte(s), &pr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -372,16 +408,31 @@ func (p *PixivClient) GetUserArtworks(id string, ids string) ([]models.IllustSho
|
|||
works = append(works, illust)
|
||||
}
|
||||
|
||||
// IDK but the order got shuffled even though Pixiv sorted the IDs in the response
|
||||
sort.Slice(works[:], func(i, j int) bool {
|
||||
left, _ := strconv.Atoi(works[i].ID)
|
||||
right, _ := strconv.Atoi(works[j].ID)
|
||||
return left > right
|
||||
})
|
||||
|
||||
return works, nil
|
||||
}
|
||||
|
||||
func (p *PixivClient) GetUserBasicInformation(id string) (models.UserShort, error) {
|
||||
var pr models.PixivResponse
|
||||
var user models.UserShort
|
||||
|
||||
s, _ := p.TextRequest(fmt.Sprintf(UserBasicInformationURL, id))
|
||||
|
||||
err := json.Unmarshal([]byte(s), &pr)
|
||||
if err != nil {
|
||||
return user, err
|
||||
}
|
||||
if pr.Error {
|
||||
return user, errors.New(fmt.Sprintf("Pixiv returned error message: %s", pr.Message))
|
||||
}
|
||||
|
||||
err = json.Unmarshal([]byte(pr.Body), &user)
|
||||
if err != nil {
|
||||
return user, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (p *PixivClient) GetUserInformation(id string, category string, page int) (*models.User, error) {
|
||||
var user *models.User
|
||||
var pr models.PixivResponse
|
||||
|
@ -417,6 +468,12 @@ func (p *PixivClient) GetUserInformation(id string, category string, page int) (
|
|||
|
||||
// Artworks
|
||||
works, _ := p.GetUserArtworks(id, ids)
|
||||
// IDK but the order got shuffled even though Pixiv sorted the IDs in the response
|
||||
sort.Slice(works[:], func(i, j int) bool {
|
||||
left, _ := strconv.Atoi(works[i].ID)
|
||||
right, _ := strconv.Atoi(works[j].ID)
|
||||
return left > right
|
||||
})
|
||||
user.Artworks = works
|
||||
|
||||
// Background image
|
||||
|
|
|
@ -109,6 +109,8 @@ type Illust struct {
|
|||
Views int `json:"viewCount"`
|
||||
XRestrict xRestrict `json:"xRestrict"`
|
||||
AiType aiType `json:"aiType"`
|
||||
User UserShort
|
||||
RecentWorks []IllustShort
|
||||
}
|
||||
|
||||
type IllustShort struct {
|
||||
|
@ -147,6 +149,12 @@ type User struct {
|
|||
FrequentTags []FrequentTag
|
||||
}
|
||||
|
||||
type UserShort struct {
|
||||
ID string `json:"userId"`
|
||||
Name string `json:"name"`
|
||||
Avatar string `json:"imageBig"`
|
||||
}
|
||||
|
||||
type RankedArtwork struct {
|
||||
ID int `json:"illust_id"`
|
||||
Title string `json:"title"`
|
||||
|
|
|
@ -4,7 +4,11 @@
|
|||
<div class="artwork-images">
|
||||
{{ range index := Illust.Images }}
|
||||
<a href="{{ proxyImage(.Original) }}" target="_blank">
|
||||
<img src="{{ proxyImage(.Large) }}" alt="Page {{ index }}" class="artwork-image-page" />
|
||||
<img
|
||||
src="{{ proxyImage(.Large) }}"
|
||||
alt="Page {{ index }}"
|
||||
class="artwork-image-page"
|
||||
/>
|
||||
</a>
|
||||
{{ end }}
|
||||
</div>
|
||||
|
@ -17,21 +21,28 @@
|
|||
{{ end }} {{ range Illust.Tags }} {{ if isEmphasize(.Name) }}
|
||||
<span class="artwork-tag-name emphasize">{{ .Name }}</span>
|
||||
{{ else }}
|
||||
<a href="/tags/{{ .Name }}" class="artwork-tag-name">#{{ .Name }}</a><span class="artwork-tag-altname">{{
|
||||
.TranslatedName }}</span>
|
||||
<a href="/tags/{{ .Name }}" class="artwork-tag-name">#{{ .Name }}</a
|
||||
><span class="artwork-tag-altname">{{ .TranslatedName }}</span>
|
||||
{{ end }} {{ end }}
|
||||
</div>
|
||||
<br />
|
||||
<span>{{ Illust.Views }} views | {{ Illust.Bookmarks }} bookmarks | {{
|
||||
Illust.Likes }} likes</span>
|
||||
<span
|
||||
>{{ Illust.Views }} views | {{ Illust.Bookmarks }} bookmarks | {{
|
||||
Illust.Likes }} likes</span
|
||||
>
|
||||
<br />
|
||||
<small>{{ Illust.Date }}</small>
|
||||
|
||||
<a href="/users/{{ Artist.ID }}" class="artwork-artist flex"><img src="{{ proxyImage(Artist.Avatar) }}"
|
||||
alt="{{ Artist.Name }}" class="artwork-artist-avatar border-rounded" />
|
||||
{{ Artist.Name }}</a>
|
||||
<a href="/users/{{ Illust.User.ID }}" class="artwork-artist flex"
|
||||
><img
|
||||
src="{{ proxyImage(Illust.User.Avatar) }}"
|
||||
alt="{{ Illust.User.Name }}"
|
||||
class="artwork-artist-avatar border-rounded"
|
||||
/>
|
||||
{{ Illust.User.Name }}</a
|
||||
>
|
||||
<div class="thumbnail-container">
|
||||
{{ range Artist.Artworks }}
|
||||
{{ range Illust.RecentWorks }}
|
||||
<div class="artwork-thumbnail-small artwork-thumbnail">
|
||||
{{ include "thumbnail-dt" . }}
|
||||
</div>
|
||||
|
@ -41,13 +52,20 @@
|
|||
<h2>Comments</h2>
|
||||
{{ range Comments }}
|
||||
<div class="comment">
|
||||
<img class="comment-avatar" src="{{ proxyImage(.Avatar) }}" alt="{{ .AuthorName }}" />
|
||||
<img
|
||||
class="comment-avatar"
|
||||
src="{{ proxyImage(.Avatar) }}"
|
||||
alt="{{ .AuthorName }}"
|
||||
/>
|
||||
<div class="comment-context">
|
||||
<b>{{ .AuthorName }}</b>
|
||||
<p>
|
||||
{{ if .Stamp }}
|
||||
<img class="stamp" src="https://s.pximg.net/common/images/stamp/generated-stamps/{{ .Stamp }}_s.jpg"
|
||||
alt="https://s.pximg.net/common/images/stamp/generated-stamps/{{ .Stamp }}_s.jpg" />
|
||||
<img
|
||||
class="stamp"
|
||||
src="https://s.pximg.net/common/images/stamp/generated-stamps/{{ .Stamp }}_s.jpg"
|
||||
alt="https://s.pximg.net/common/images/stamp/generated-stamps/{{ .Stamp }}_s.jpg"
|
||||
/>
|
||||
{{ else }} {{ raw: parseEmojis(.Context) }} {{ end }}
|
||||
</p>
|
||||
|
||||
|
|
|
@ -27,7 +27,6 @@ func artwork_page(c *fiber.Ctx) error {
|
|||
|
||||
related, _ := PC.GetRelatedArtworks(id)
|
||||
comments, _ := PC.GetArtworkComments(id)
|
||||
artist_info, err := PC.GetUserInformation(illust.UserID, "artworks", 1)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -37,7 +36,6 @@ func artwork_page(c *fiber.Ctx) error {
|
|||
return c.Render("artwork", fiber.Map{
|
||||
"Illust": illust,
|
||||
"Related": related,
|
||||
"Artist": artist_info,
|
||||
"Comments": comments,
|
||||
"Title": illust.Title,
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue