Optimization: optimize the artwork page speed

This commit is contained in:
VnPower
2023-08-16 20:40:18 +07:00
parent abfba0cc35
commit c8414fbddf
4 changed files with 169 additions and 123 deletions
+86 -50
View File
@@ -63,69 +63,105 @@ func (p *PixivClient) GetArtworkByID(id string) (*models.Illust, error) {
return nil, err
}
// Get illust images
images, err = p.GetArtworkImages(id)
if err != nil {
return nil, err
}
// Begin testing here
illust.Images = images
c1 := make(chan []models.Image)
c2 := make(chan []models.IllustShort)
c3 := make(chan models.UserShort)
c4 := make(chan []models.Tag)
c5 := make(chan []models.IllustShort)
c6 := make(chan []models.Comment)
// Get recent artworks
ids := make([]int, 0)
go func() {
// Get illust images
images, err = p.GetArtworkImages(id)
if err != nil {
c1 <- nil
}
c1 <- images
}()
for k := range illust.Recent {
ids = append(ids, k)
}
go func() {
// Get recent artworks
ids := make([]int, 0)
sort.Sort(sort.Reverse(sort.IntSlice(ids)))
for k := range illust.Recent {
ids = append(ids, k)
}
idsString := ""
count := min(len(ids), 20)
sort.Sort(sort.Reverse(sort.IntSlice(ids)))
for i := 0; i < count; i++ {
idsString += fmt.Sprintf("&ids[]=%d", ids[i])
}
idsString := ""
count := min(len(ids), 20)
recent, err := p.GetUserArtworks(illust.UserID, idsString)
if err != nil {
return nil, err
}
sort.Slice(recent[:], func(i, j int) bool {
left, _ := strconv.Atoi(recent[i].ID)
right, _ := strconv.Atoi(recent[j].ID)
return left > right
})
for i := 0; i < count; i++ {
idsString += fmt.Sprintf("&ids[]=%d", ids[i])
}
illust.RecentWorks = recent
recent, err := p.GetUserArtworks(illust.UserID, idsString)
if err != nil {
c2 <- nil
}
sort.Slice(recent[:], func(i, j int) bool {
left, _ := strconv.Atoi(recent[i].ID)
right, _ := strconv.Atoi(recent[j].ID)
return left > right
})
c2 <- 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
go func() {
// Get basic user information (the URL above does not contain avatars)
userInfo, err := p.GetUserBasicInformation(illust.UserID)
if err != nil {
//
}
c3 <- userInfo
}()
// Extract tags
var tags struct {
Tags []struct {
Tag string `json:"tag"`
Translation map[string]string `json:"translation"`
} `json:"tags"`
}
err = json.Unmarshal(illust.RawTags, &tags)
if err != nil {
return nil, err
}
go func() {
var tagsList []models.Tag
// Extract tags
var tags struct {
Tags []struct {
Tag string `json:"tag"`
Translation map[string]string `json:"translation"`
} `json:"tags"`
}
err = json.Unmarshal(illust.RawTags, &tags)
if err != nil {
c4 <- nil
}
for _, tag := range tags.Tags {
var newTag models.Tag
newTag.Name = tag.Tag
newTag.TranslatedName = tag.Translation["en"]
for _, tag := range tags.Tags {
var newTag models.Tag
newTag.Name = tag.Tag
newTag.TranslatedName = tag.Translation["en"]
illust.Tags = append(illust.Tags, newTag)
}
tagsList = append(tagsList, newTag)
}
c4 <- tagsList
}()
go func() {
related, _ := p.GetRelatedArtworks(id)
// Error handling...
c5 <- related
}()
go func() {
comments, _ := p.GetArtworkComments(id)
// Error handling...
c6 <- comments
}()
illust.Images = <-c1
illust.RecentWorks = <-c2
illust.User = <-c3
illust.Tags = <-c4
illust.RelatedWorks = <-c5
illust.CommentsList = <-c6
return illust.Illust, nil
}
+5 -1
View File
@@ -113,9 +113,11 @@ type Illust struct {
AiType aiType `json:"aiType"`
User UserShort
RecentWorks []IllustShort
RelatedWorks []IllustShort
CommentsList []Comment
}
func (s Illust) ProxyImages(proxy string) {
func (s *Illust) ProxyImages(proxy string) {
for i := range s.Images {
s.Images[i].Small = ProxyImage(s.Images[i].Small, proxy)
s.Images[i].Medium = ProxyImage(s.Images[i].Medium, proxy)
@@ -125,6 +127,8 @@ func (s Illust) ProxyImages(proxy string) {
for i := range s.RecentWorks {
s.RecentWorks[i].Thumbnail = ProxyImage(s.RecentWorks[i].Thumbnail, proxy)
}
s.RelatedWorks = ProxyShortArtworkSlice(s.RelatedWorks, proxy)
s.CommentsList = ProxyCommentsSlice(s.CommentsList, proxy)
s.User.Avatar = ProxyImage(s.User.Avatar, proxy)
}
+78 -61
View File
@@ -1,67 +1,84 @@
<div class="container">
<div class="artwork-page">
<div class="artwork-content">
<div class="artwork-images">
{{ range index := Illust.Images }}
<a href="{{ .Original }}" target="_blank">
<img src="{{ .Large }}" alt="Page {{ index }}" class="artwork-image-page" />
</a>
{{ end }}
</div>
<h2>{{ Illust.Title }}</h2>
<p>{{ raw: Illust.Description }}</p>
<div class="artwork-page">
<div class="artwork-content">
<div class="artwork-images">
{{ range index := Illust.Images }}
<a href="{{ .Original }}" target="_blank">
<img
src="{{ .Large }}"
alt="Page {{ index }}"
class="artwork-image-page"
/>
</a>
{{ end }}
</div>
<h2>{{ Illust.Title }}</h2>
<p>{{ raw: Illust.Description }}</p>
<div class="artwork-tags">
{{ if Illust.AiType == 2 }}
<span class="artwork-tag-name emphasize">AI-generated</span>
{{ 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>
{{ end }} {{ end }}
</div>
<br />
<span>{{ Illust.Views }} views | {{ Illust.Bookmarks }} bookmarks | {{
Illust.Likes }} likes</span>
<br />
<small>{{ Illust.Date }}</small>
<div class="artwork-tags">
{{ if Illust.AiType == 2 }}
<span class="artwork-tag-name emphasize">AI-generated</span>
{{ 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>
{{ end }} {{ end }}
</div>
<br />
<span
>{{ Illust.Views }} views | {{ Illust.Bookmarks }} bookmarks | {{
Illust.Likes }} likes</span
>
<br />
<small>{{ Illust.Date }}</small>
<a href="/users/{{ Illust.User.ID }}" class="artwork-artist flex"><img src="{{ Illust.User.Avatar }}"
alt="{{ Illust.User.Name }}" class="artwork-artist-avatar border-rounded" />
{{ Illust.User.Name }}</a>
<div class="thumbnail-container">
{{ range Illust.RecentWorks }}
<div class="artwork-thumbnail-small artwork-thumbnail">
{{ include "components/thumbnail-dt" . }}
</div>
{{ end }}
</div>
<hr />
<h2>Comments</h2>
{{ if Illust.CommentDisabled == 1 }}
<p>The creator turned comments off</p>
{{ else if Illust.Comments == 0 }}
<p>There is no comment yet</p>
{{ else }} {{ range Comments }}
<div class="comment">
<img class="comment-avatar" src="{{ .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" />
{{ else }} {{ raw: parseEmojis(.Context) }} {{ end }}
</p>
<small>{{ .Date }}</small>
</div>
</div>
{{ end }} {{ end }}
<a href="/users/{{ Illust.User.ID }}" class="artwork-artist flex"
><img
src="{{ Illust.User.Avatar }}"
alt="{{ Illust.User.Name }}"
class="artwork-artist-avatar border-rounded"
/>
{{ Illust.User.Name }}</a
>
<div class="thumbnail-container">
{{ range Illust.RecentWorks }}
<div class="artwork-thumbnail-small artwork-thumbnail">
{{ include "components/thumbnail-dt" . }}
</div>
{{ end }}
</div>
<hr />
<h2>Comments</h2>
{{ if Illust.CommentDisabled == 1 }}
<p>The creator turned comments off</p>
{{ else if Illust.Comments == 0 }}
<p>There is no comment yet</p>
{{ else }} {{ range Illust.CommentsList }}
<div class="comment">
<img
class="comment-avatar"
src="{{ .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"
/>
{{ else }} {{ raw: parseEmojis(.Context) }} {{ end }}
</p>
<small>{{ .Date }}</small>
</div>
</div>
{{ end }} {{ end }}
</div>
<h2>Related works</h2>
<div>{{ include "components/small-tn" Related }}</div>
</div>
<h2>Related works</h2>
<div>{{ include "components/small-tn" Illust.RelatedWorks }}</div>
</div>
-11
View File
@@ -43,20 +43,9 @@ func artwork_page(c *fiber.Ctx) error {
illust.ProxyImages(*image_proxy)
related, err := PC.GetRelatedArtworks(id)
if err != nil {
return err
}
related = models.ProxyShortArtworkSlice(related, *image_proxy)
comments, _ := PC.GetArtworkComments(id)
comments = models.ProxyCommentsSlice(comments, *image_proxy)
// Optimize this
return c.Render("pages/artwork", fiber.Map{
"Illust": illust,
"Related": related,
"Comments": comments,
"Title": illust.Title,
"PageType": "artwork",
})