Feature: user's (public) bookmarks

This commit is contained in:
VnPower 2023-06-16 20:08:14 +07:00
parent 5a0c188b0f
commit ef6c72c0f2
Signed by: vnpower
GPG key ID: 881DE3DEB966106C
5 changed files with 91 additions and 33 deletions

View file

@ -72,8 +72,7 @@ func (p *PixivClient) GetArtworkByID(id string) (*models.Illust, error) {
illust.Images = images
// Get recent artworks
var ids []int
idsString := ""
ids := make([]int, len(illust.Recent))
for k := range illust.Recent {
ids = append(ids, k)
@ -81,8 +80,10 @@ func (p *PixivClient) GetArtworkByID(id string) (*models.Illust, error) {
sort.Sort(sort.Reverse(sort.IntSlice(ids)))
count := len(ids)
for i := 0; i < 30 && i < count; i++ {
idsString := ""
count := Min(len(ids), 30)
for i := 0; i < count; i++ {
idsString += fmt.Sprintf("&ids[]=%d", ids[i])
}

View file

@ -15,5 +15,6 @@ const (
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"
UserBookmarksURL = "https://www.pixiv.net/ajax/user/%s/illusts/bookmarks?tag=&offset=%d&limit=48&rest=%s"
FrequentTagsURL = "https://www.pixiv.net/ajax/tags/frequent/illust?%s"
)

View file

@ -126,11 +126,6 @@ func (p *PixivClient) GetUserBasicInformation(id string) (models.UserShort, erro
func (p *PixivClient) GetUserInformation(id string, category string, page int) (*models.User, error) {
var user *models.User
ids, count, err := p.GetUserArtworksID(id, category, page)
if err != nil {
return nil, err
}
URL := fmt.Sprintf(UserInformationURL, id)
response, err := p.PixivRequest(URL)
@ -151,26 +146,77 @@ func (p *PixivClient) GetUserInformation(id string, category string, page int) (
user = body.User
// 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
if category != "bookmarks" {
// Artworks
ids, count, err := p.GetUserArtworksID(id, category, page)
if err != nil {
return nil, err
}
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
// Artworks count
user.ArtworksCount = count
// Frequent tags
user.FrequentTags, err = p.GetFrequentTags(ids)
} else {
// Bookmarks
works, count, err := p.GetUserBookmarks(id, "show", page)
if err != nil {
return nil, err
}
user.Artworks = works
// Public bookmarks count
user.ArtworksCount = count
}
// Background image
if body.Background != nil {
user.BackgroundImage = body.Background["url"].(string)
}
// Artworks count
user.ArtworksCount = count
// Frequent tags
user.FrequentTags, err = p.GetFrequentTags(ids)
return user, nil
}
func (p *PixivClient) GetUserBookmarks(id string, mode string, page int) ([]models.IllustShort, int, error) {
page--
URL := fmt.Sprintf(UserBookmarksURL, id, page*48, mode)
response, err := p.PixivRequest(URL)
if err != nil {
return nil, -1, err
}
var body struct {
Artworks []json.RawMessage `json:"works"`
Total int `json:"total"`
}
err = json.Unmarshal([]byte(response), &body)
if err != nil {
return nil, -1, err
}
artworks := make([]models.IllustShort, len(body.Artworks))
for index, value := range body.Artworks {
var artwork models.IllustShort
err = json.Unmarshal([]byte(value), &artwork)
if err != nil {
continue
}
artworks[index] = artwork
}
return artworks, body.Total, nil
}

View file

@ -33,6 +33,9 @@
<a href="/users/{{ User.ID }}/manga#checkpoint" class="switch-button"
>Mangas</a
>
<a href="/users/{{ User.ID }}/bookmarks#checkpoint" class="switch-button"
>Bookmarks</a
>
</div>
<div>
<h1 id="checkpoint">Illustrations and Mangas</h1>
@ -63,9 +66,13 @@
<a href="#" class="pagination-button disabled">First</a>
<a href="#" class="pagination-button disabled">Previous</a>
{{ else }}
<a href="/users/{{ User.ID }}?page=1" class="pagination-button">First</a>
<a
href="/users/{{ User.ID }}?page={{ dec(Page) }}"
href="/users/{{ User.ID }}/{{ Category }}?page=1"
class="pagination-button"
>First</a
>
<a
href="/users/{{ User.ID }}/{{ Category }}?page={{ dec(Page) }}"
class="pagination-button"
>Previous</a
>
@ -78,12 +85,12 @@
<a href="#" class="pagination-button disabled">Last</a>
{{ else }}
<a
href="/users/{{ User.ID }}?page={{ inc(Page) }}"
href="/users/{{ User.ID }}/{{ Category }}?page={{ inc(Page) }}"
class="pagination-button"
>Next</a
>
<a
href="/users/{{ User.ID }}?page={{ dec(PageLimit) }}"
href="/users/{{ User.ID }}/{{ Category }}?page={{ dec(PageLimit) }}"
class="pagination-button"
>Last</a
>

View file

@ -61,21 +61,24 @@ func user_page(c *fiber.Ctx) error {
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")
if !(category == "artworks" || category == "illustrations" || category == "manga" || category == "bookmarks") {
return errors.New("Invalid work category: only illustrations, manga, artworks and bookmarks are available")
}
page := c.Query("page", "1")
page := c.Query("page", "1")
pageInt, _ := strconv.Atoi(page)
user, err := PC.GetUserInformation(id, category, pageInt)
if err != nil {
return err
}
worksCount := user.ArtworksCount
var worksCount int
worksCount = user.ArtworksCount
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})
return c.Render("user", fiber.Map{"Title": user.Name, "User": user, "Category": category, "PageLimit": int(pageLimit), "Page": pageInt})
}
func ranking_page(c *fiber.Ctx) error {