Feature: newest artworks

This commit is contained in:
VnPower
2023-05-21 22:09:13 +07:00
parent cbb1752c4c
commit 1fc496fbe2
3 changed files with 63 additions and 7 deletions
+37 -7
View File
@@ -26,6 +26,7 @@ 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"
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"
@@ -259,18 +260,12 @@ func (p *PixivClient) GetUserArtworksCount(id string) (int, error) {
return -1, errors.New(pr.Message)
}
var count int
var body struct {
Illusts map[int]string `json:"illusts"`
}
err = json.Unmarshal(pr.Body, &body)
// Get the keys, because Pixiv only returns IDs (very evil)
for range body.Illusts {
count++
}
return count, nil
return len(body.Illusts), nil
}
func (p *PixivClient) GetRelatedArtworks(id string) ([]models.IllustShort, error) {
@@ -376,3 +371,38 @@ func (p *PixivClient) GetUserInformation(id string, page int) (*models.User, err
return user, nil
}
func (p *PixivClient) GetNewestArtworks(worktype string, r18 string) ([]models.IllustShort, error) {
var pr models.PixivResponse
var newWorks []models.IllustShort
lastID := "0"
for i := 0; i < 10; i++ {
url := fmt.Sprintf(ArtworkNewestURL, worktype, r18, lastID)
s, err := p.TextRequest(url)
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(s), &pr)
var body struct {
Illusts []models.IllustShort `json:"illusts"`
LastID string `json:"lastId"`
}
err = json.Unmarshal([]byte(pr.Body), &body)
if err != nil {
return nil, err
}
newWorks = append(newWorks, body.Illusts...)
lastID = body.LastID
}
println(len(newWorks))
return newWorks, nil
}
+8
View File
@@ -0,0 +1,8 @@
{{ template "header.html" }}
<div class="container">
<h2>{{ .Title }}</h2>
<div>{{ template "small-tn.html" .Items }}</div>
</div>
{{ template "footer.html" }}
+18
View File
@@ -56,6 +56,23 @@ func user_page(c *gin.Context) {
c.HTML(http.StatusOK, "user.html", gin.H{"User": user, "PageLimit": int(pageLimit), "Page": pageInt})
}
func newestArtworksPage(c *gin.Context) {
worktype, ok := c.GetQuery("type")
if !ok {
worktype = "illust"
}
r18, ok := c.GetQuery("r18")
if !ok {
r18 = "false"
}
works, _ := PC.GetNewestArtworks(worktype, r18)
c.HTML(http.StatusOK, "list.html", gin.H{"Title": "Newest works from all users", "Items": works})
}
func NewPixivClient(timeout int) *models.PixivClient {
transport := &http.Transport{Proxy: http.ProxyFromEnvironment}
client := &http.Client{
@@ -80,4 +97,5 @@ func SetupRoutes(r *gin.Engine) {
// r.GET("/", index_page)
r.GET("artworks/:id", artwork_page)
r.GET("users/:id", user_page)
r.GET("newest", newestArtworksPage)
}