diff --git a/handler/pixiv.go b/handler/pixiv.go index 2036e96..afd2978 100644 --- a/handler/pixiv.go +++ b/handler/pixiv.go @@ -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 { @@ -22,20 +23,21 @@ type PixivClient struct { } 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" - 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" - ArtworkDiscoveryURL = "https://www.pixiv.net/ajax/discovery/artworks?mode=%s&limit=%d" - SearchTagURL = "https://www.pixiv.net/ajax/search/tags/%s" - 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" - 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" + 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" + 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" + ArtworkDiscoveryURL = "https://www.pixiv.net/ajax/discovery/artworks?mode=%s&limit=%d" + SearchTagURL = "https://www.pixiv.net/ajax/search/tags/%s" + 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" ) func (p *PixivClient) SetHeader(header map[string]string) { @@ -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 diff --git a/models/models.go b/models/models.go index 1deab8e..8a2d6c8 100644 --- a/models/models.go +++ b/models/models.go @@ -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"` diff --git a/template/artwork.jet.html b/template/artwork.jet.html index 6074d98..90c5c23 100644 --- a/template/artwork.jet.html +++ b/template/artwork.jet.html @@ -4,7 +4,11 @@
{{ range index := Illust.Images }} - Page {{ index }} + Page {{ index }} {{ end }}
@@ -17,21 +21,28 @@ {{ end }} {{ range Illust.Tags }} {{ if isEmphasize(.Name) }} {{ .Name }} {{ else }} - #{{ .Name }}{{ - .TranslatedName }} + #{{ .Name }}{{ .TranslatedName }} {{ end }} {{ end }}
- {{ Illust.Views }} views | {{ Illust.Bookmarks }} bookmarks | {{ - Illust.Likes }} likes + {{ Illust.Views }} views | {{ Illust.Bookmarks }} bookmarks | {{ + Illust.Likes }} likes
{{ Illust.Date }} - {{ Artist.Name }} - {{ Artist.Name }} + {{ Illust.User.Name }} + {{ Illust.User.Name }}
- {{ range Artist.Artworks }} + {{ range Illust.RecentWorks }}
{{ include "thumbnail-dt" . }}
@@ -41,13 +52,20 @@

Comments

{{ range Comments }}
- {{ .AuthorName }} + {{ .AuthorName }}
{{ .AuthorName }}

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

diff --git a/views/routes.go b/views/routes.go index 57e94e3..ad8e960 100644 --- a/views/routes.go +++ b/views/routes.go @@ -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, })