From f8f08342bbd6ee9b459d4de7846d6e6f926331a4 Mon Sep 17 00:00:00 2001 From: VnPower Date: Sun, 4 Jun 2023 21:59:51 +0700 Subject: [PATCH] Feature: add comments for artwork page (currently limited to 30) --- handler/pixiv.go | 25 ++++++++++- models/models.go | 9 ++++ template/artwork.html | 94 ++++++++++++++++++++++++++--------------- template/css/style.css | 18 ++++++++ template/css/style.scss | 22 ++++++++++ views/routes.go | 15 +++---- 6 files changed, 137 insertions(+), 46 deletions(-) diff --git a/handler/pixiv.go b/handler/pixiv.go index e5b7fcf..aeaea2d 100644 --- a/handler/pixiv.go +++ b/handler/pixiv.go @@ -26,7 +26,8 @@ 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=%d" - ArtworkNewestURL = "https://www.pixiv.net/ajax/illust/new?limit=200&type=%s&r18=%s&lastId=%s" + ArtworkCommentsURL = "https://www.pixiv.net/ajax/illusts/comments/roots?illust_id=%s&limit=100" + ArtworkNewestURL = "https://www.pixiv.net/ajax/illust/new?limit=30&type=%s&r18=%s&lastId=%s" ArtworkRankingURL = "https://www.pixiv.net/ranking.php?format=json&mode=%s&content=%s&p=%s" SearchTagURL = "https://www.pixiv.net/ajax/search/tags/%s" SearchArtworksURL = "https://www.pixiv.net/ajax/search/%s/%s?order=%s&mode=%s&p=%s" @@ -197,6 +198,28 @@ func (p *PixivClient) GetArtworkByID(id string) (*models.Illust, error) { return illust.Illust, nil } +func (p *PixivClient) GetArtworkComments(id string) ([]models.Comment, error) { + var pr models.PixivResponse + var body struct { + Comments []models.Comment `json:"comments"` + } + + s, _ := p.TextRequest(fmt.Sprintf(ArtworkCommentsURL, id)) + + err := json.Unmarshal([]byte(s), &pr) + + if err != nil { + return nil, errors.New(fmt.Sprintf("Failed to extract data from JSON response from server. %s", err)) + } + if pr.Error { + return nil, errors.New(pr.Message) + } + + err = json.Unmarshal([]byte(pr.Body), &body) + + return body.Comments, nil +} + func (p *PixivClient) GetUserArtworksID(id string, page int) (*string, error) { s, _ := p.TextRequest(fmt.Sprintf(UserArtworksURL, id)) diff --git a/models/models.go b/models/models.go index 284a4c6..bf9f788 100644 --- a/models/models.go +++ b/models/models.go @@ -120,6 +120,15 @@ type IllustShort struct { AiType aiType `json:"aiType"` } +type Comment struct { + AuthorID string `json:"userId"` + AuthorName string `json:"userName"` + Avatar string `json:"img"` + Context string `json:"comment"` + Stamp string `json:"stampId"` + Date string `json:"commentDate"` +} + type User struct { ID string `json:"userId"` Name string `json:"name"` diff --git a/template/artwork.html b/template/artwork.html index ff09a88..0765e11 100644 --- a/template/artwork.html +++ b/template/artwork.html @@ -1,5 +1,4 @@ -{{ template "header.html" }} -{{ $parent := . }} +{{ template "header.html" }} {{ $parent := . }}
{{ with .Illust }} @@ -7,50 +6,75 @@
{{ range .Images }} - {{ proxyImage .Original }} + {{ proxyImage .Original }} {{ end }}
-

{{ .Title }}

-

{{ .Description }}

+

{{ .Title }}

+

{{ .Description }}

-
+
+ {{ range .Tags }} {{ if isEmphasize .Name }} + {{ .Name }} + {{ else }} + #{{ .Name }}{{ .TranslatedName }} + {{ end }} {{ end }} +
+
+ {{ .Views }} views | {{ .Bookmarks }} bookmarks +
+ {{ .Date }} - {{ range .Tags }} - {{ if isEmphasize .Name }} - {{ .Name }} - {{ else }} - #{{ .Name }}{{ - .TranslatedName - }} - {{ end }} - {{ end }} + {{ $parent.Artist.Name }} + {{ $parent.Artist.Name }} +
+ {{ range $parent.Artist.Artworks }} +
+ {{ template "thumbnail-dt.html" . }}
-
- {{ .Views }} views | {{ .Bookmarks }} bookmarks -
- {{ .Date }} + {{ end }} +
+
+

Comments

+ {{ range $parent.Comments }} +
+ {{ .AuthorName }} +
+ {{ .AuthorName }} +

+ {{ if .Stamp }} + https://s.pximg.net/common/images/stamp/generated-stamps/{{ .Stamp }}_s.jpg + {{ else }} {{ .Context }} {{ end }} +

- {{ $parent.Artist.Name }} - {{ $parent.Artist.Name }} -
- {{ range $parent.Artist.Artworks }} -
- {{ template "thumbnail-dt.html" . }} -
- {{ end }} + {{ .Date }}
-
-

Comments

- To be added :) +
+ {{ end }}
{{ end }}

Related works

-
- {{ template "small-tn.html" .Related }} -
+
{{ template "small-tn.html" .Related }}
+ {{ template "footer.html" }} diff --git a/template/css/style.css b/template/css/style.css index b10c1fc..50a9888 100644 --- a/template/css/style.css +++ b/template/css/style.css @@ -260,6 +260,24 @@ body { width: auto; max-width: 100%; } +.artwork-page .comment { + display: flex; + margin-bottom: 10px; +} +.artwork-page .comment p { + margin: 0; +} +.artwork-page .comment .comment-avatar { + width: 40px; + height: 40px; + border-radius: 50%; + margin-right: 10px; +} +.artwork-page .comment .stamp { + width: 96px; + height: 96px; + border-radius: 4px; +} .user-background { display: flex; diff --git a/template/css/style.scss b/template/css/style.scss index 288a9a6..d82dbac 100644 --- a/template/css/style.scss +++ b/template/css/style.scss @@ -304,6 +304,28 @@ body { max-width: 100%; } } + + .comment { + display: flex; + margin-bottom: 10px; + + p { + margin: 0; + } + + .comment-avatar { + width: 40px; + height: 40px; + border-radius: 50%; + margin-right: 10px; + } + + .stamp { + width: 96px; + height: 96px; + border-radius: 4px; + } + } } .user-background { diff --git a/views/routes.go b/views/routes.go index 515e221..61bc93d 100644 --- a/views/routes.go +++ b/views/routes.go @@ -7,29 +7,24 @@ import ( "pixivfe/handler" "strconv" "time" - "unicode" "github.com/gin-gonic/gin" ) -func capitalize(str string) string { - runes := []rune(str) - runes[0] = unicode.ToUpper(runes[0]) - return string(runes) -} - var PC *models.PixivClient func artwork_page(c *gin.Context) { id := c.Param("id") illust, _ := PC.GetArtworkByID(id) related, _ := PC.GetRelatedArtworks(id) + comments, _ := PC.GetArtworkComments(id) artist_info, _ := PC.GetUserInformation(illust.UserID, 1) c.HTML(http.StatusOK, "artwork.html", gin.H{ - "Illust": illust, - "Related": related, - "Artist": artist_info, + "Illust": illust, + "Related": related, + "Artist": artist_info, + "Comments": comments, }) }