mirror of
https://codeberg.org/VnPower/PixivFE
synced 2024-12-06 19:16:23 +01:00
Optimization: Clean models and parsing functions
This commit is contained in:
+10
-47
@@ -2,57 +2,20 @@ package entity
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Illust struct {
|
||||
ID int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Caption template.HTML `json:"caption"`
|
||||
Artist UserBrief `json:"user"`
|
||||
Date time.Time `json:"create_date"`
|
||||
Pages int `json:"page_count"`
|
||||
Views int `json:"total_view"`
|
||||
Bookmarks int `json:"total_bookmarks"`
|
||||
SingleImage map[string]string `json:"meta_single_page"`
|
||||
MultipleImage []map[string]map[string]string `json:"meta_pages"`
|
||||
Tags []Tag `json:"tags"`
|
||||
Images map[string]string `json:"image_urls"`
|
||||
ImagesAlt []Image
|
||||
}
|
||||
|
||||
func ProxyImages(url string) string {
|
||||
regex := regexp.MustCompile(`i\.pximg\.net`)
|
||||
proxy := "px2.rainchan.win"
|
||||
|
||||
return regex.ReplaceAllString(url, proxy)
|
||||
|
||||
}
|
||||
|
||||
func (c *Illust) ParseImages() {
|
||||
var images []Image
|
||||
if c.Pages > 1 {
|
||||
for _, imageMap := range c.MultipleImage {
|
||||
var image Image
|
||||
image.Small = imageMap["image_urls"]["square_medium"]
|
||||
image.Medium = imageMap["image_urls"]["medium"]
|
||||
image.Large = imageMap["image_urls"]["large"]
|
||||
image.Original = imageMap["image_urls"]["original"]
|
||||
|
||||
images = append(images, image)
|
||||
}
|
||||
} else {
|
||||
var image Image
|
||||
image.Small = c.Images["square_medium"]
|
||||
image.Medium = c.Images["medium"]
|
||||
image.Large = c.Images["large"]
|
||||
image.Original = c.Images["original"]
|
||||
|
||||
images = append(images, image)
|
||||
}
|
||||
|
||||
c.ImagesAlt = images
|
||||
ID int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Caption template.HTML `json:"caption"`
|
||||
Artist UserBrief `json:"user"`
|
||||
Date time.Time `json:"create_date"`
|
||||
Pages int `json:"page_count"`
|
||||
Views int `json:"total_view"`
|
||||
Bookmarks int `json:"total_bookmarks"`
|
||||
Tags []Tag `json:"tags"`
|
||||
Images []Image
|
||||
}
|
||||
|
||||
type Spotlight struct {
|
||||
|
||||
+29
-64
@@ -33,6 +33,7 @@ func ParseImages(data string) []entity.Image {
|
||||
images = append(images, image)
|
||||
} else {
|
||||
g := GetInnerJSON(data, "meta_pages.#.image_urls")
|
||||
println(g)
|
||||
|
||||
err := json.Unmarshal([]byte(g), &images)
|
||||
if err != nil {
|
||||
@@ -43,91 +44,66 @@ func ParseImages(data string) []entity.Image {
|
||||
return images
|
||||
}
|
||||
|
||||
func ParseIllust(data string) []entity.Illust {
|
||||
var illusts []entity.Illust
|
||||
g := gjson.Get(data, "#")
|
||||
func ParseIllust(data string) entity.Illust {
|
||||
var illust entity.Illust
|
||||
|
||||
for _, handle := range g.Array() {
|
||||
println(handle.String())
|
||||
println("here")
|
||||
images := ParseImages(data)
|
||||
illust.Images = images
|
||||
|
||||
err := json.Unmarshal([]byte(data), &illust)
|
||||
if err != nil {
|
||||
panic("Failed to parse JSON")
|
||||
}
|
||||
|
||||
return illust
|
||||
}
|
||||
|
||||
func ParseIllusts(data string) []entity.Illust {
|
||||
var illusts []entity.Illust
|
||||
g := gjson.Get(data, "illusts").Array()
|
||||
|
||||
for _, illust := range g {
|
||||
println(illust.String())
|
||||
illust := ParseIllust(illust.String())
|
||||
|
||||
illusts = append(illusts, illust)
|
||||
}
|
||||
// g.ForEach(func(key, value gjson.Result) bool {
|
||||
// var illust entity.Illust
|
||||
// v := value.String()
|
||||
// println(v)
|
||||
// err := json.Unmarshal([]byte(v), &illust)
|
||||
//
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
//
|
||||
// illust.Images = ParseImages(v)
|
||||
// illusts = append(illusts, illust)
|
||||
// return true
|
||||
// })
|
||||
//
|
||||
// println(illusts)
|
||||
|
||||
return illusts
|
||||
}
|
||||
|
||||
func GetRecommendedIllust(c *gin.Context) []entity.Illust {
|
||||
URL := "https://hibi.cocomi.cf/api/pixiv/illust_recommended"
|
||||
var illusts []entity.Illust
|
||||
|
||||
s := Request(URL)
|
||||
ParseIllust(s)
|
||||
g := GetInnerJSON(s, "illusts")
|
||||
|
||||
err := json.Unmarshal([]byte(g), &illusts)
|
||||
if err != nil {
|
||||
panic("Failed to parse JSON")
|
||||
}
|
||||
illusts := ParseIllusts(s)
|
||||
|
||||
return illusts
|
||||
}
|
||||
|
||||
func GetRankingIllust(c *gin.Context, mode string) []entity.Illust {
|
||||
URL := "https://hibi.cocomi.cf/api/pixiv/rank?page=1&mode=" + mode
|
||||
var illusts []entity.Illust
|
||||
|
||||
s := Request(URL)
|
||||
g := GetInnerJSON(s, "illusts")
|
||||
|
||||
err := json.Unmarshal([]byte(g), &illusts)
|
||||
if err != nil {
|
||||
panic("Failed to parse JSON")
|
||||
}
|
||||
illusts := ParseIllusts(s)
|
||||
|
||||
return illusts
|
||||
}
|
||||
|
||||
func GetNewestIllust(c *gin.Context) []entity.Illust {
|
||||
URL := "https://hibi.cocomi.cf/api/pixiv/illust_new"
|
||||
var illusts []entity.Illust
|
||||
|
||||
s := Request(URL)
|
||||
g := GetInnerJSON(s, "illusts")
|
||||
|
||||
err := json.Unmarshal([]byte(g), &illusts)
|
||||
if err != nil {
|
||||
panic("Failed to parse JSON")
|
||||
}
|
||||
illusts := ParseIllusts(s)
|
||||
|
||||
return illusts
|
||||
}
|
||||
|
||||
func GetMemberIllust(c *gin.Context, id string) []entity.Illust {
|
||||
URL := "https://hibi.cocomi.cf/api/pixiv/member_illust?id=" + id
|
||||
var illusts []entity.Illust
|
||||
|
||||
s := Request(URL)
|
||||
g := GetInnerJSON(s, "illusts")
|
||||
|
||||
err := json.Unmarshal([]byte(g), &illusts)
|
||||
if err != nil {
|
||||
panic("Failed to parse JSON")
|
||||
}
|
||||
illusts := ParseIllusts(s)
|
||||
|
||||
return illusts
|
||||
}
|
||||
@@ -135,15 +111,9 @@ func GetMemberIllust(c *gin.Context, id string) []entity.Illust {
|
||||
func GetRelatedIllust(c *gin.Context) []entity.Illust {
|
||||
id := c.Param("id")
|
||||
URL := "https://hibi.cocomi.cf/api/pixiv/related?id=" + id
|
||||
var illusts []entity.Illust
|
||||
|
||||
s := Request(URL)
|
||||
g := GetInnerJSON(s, "illusts")
|
||||
|
||||
err := json.Unmarshal([]byte(g), &illusts)
|
||||
if err != nil {
|
||||
panic("Failed to parse JSON")
|
||||
}
|
||||
illusts := ParseIllusts(s)
|
||||
|
||||
return illusts
|
||||
}
|
||||
@@ -151,15 +121,10 @@ func GetRelatedIllust(c *gin.Context) []entity.Illust {
|
||||
func GetIllustByID(c *gin.Context) entity.Illust {
|
||||
id := c.Param("id")
|
||||
URL := "https://hibi.cocomi.cf/api/pixiv/illust?id=" + id
|
||||
var illust entity.Illust
|
||||
|
||||
s := Request(URL)
|
||||
g := GetInnerJSON(s, "illust")
|
||||
|
||||
err := json.Unmarshal([]byte(g), &illust)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
illust := ParseIllust(g)
|
||||
|
||||
return illust
|
||||
}
|
||||
|
||||
+5
-12
@@ -5,18 +5,10 @@
|
||||
{{ with .Illust }}
|
||||
<div class="artwork-content">
|
||||
<div class="artwork-images">
|
||||
<!-- -->
|
||||
{{ if eq .Pages 1 }}
|
||||
<a href="{{ .SingleImage.original_image_url }}" target="_blank">
|
||||
<img src="{{ .Images.large }}" alt="{{ .Title }}" class="artwork-image-page" />
|
||||
{{ range .Images }}
|
||||
<a href="{{ .Original }}" target="_blank">
|
||||
<img src="{{ .Large }}" alt="{{ .Original }}" class="artwork-image-page" />
|
||||
</a>
|
||||
{{ else }}
|
||||
{{ range .MultipleImage }}
|
||||
<a href="{{ .image_urls.original }}" target="_blank">
|
||||
<img src="{{ .image_urls.large }}" alt="{{ .image_urls.original }}" class="artwork-image-page" />
|
||||
</a>
|
||||
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
</div>
|
||||
<h2>{{ .Title }}</h3>
|
||||
@@ -40,7 +32,8 @@
|
||||
{{ range $parent.Recent }}
|
||||
<div class="artwork-thumbnail-small artwork-thumbnail">
|
||||
<a href="#">
|
||||
<img src="{{ .Images.square_medium }}" alt="{{ .Title }}" class="artwork-master-image" />
|
||||
<img src="{{ (index .Images 0).Small }}" alt="{{ (index .Images 0).Small }}"
|
||||
class="artwork-master-image" />
|
||||
</a>
|
||||
</div>
|
||||
{{ end }}
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@
|
||||
<div class="artwork-rank-circle">{{ $i | inc}}</div>
|
||||
<a href="#">
|
||||
<img
|
||||
src="{{ .Images.square_medium }}"
|
||||
src="{{ (index .Images 0).Small }}"
|
||||
alt="{{ .Title }}"
|
||||
class="artwork-master-image"
|
||||
/>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<div class="artwork-thumbnail-small artwork-thumbnail">
|
||||
<a href="#">
|
||||
<img
|
||||
src="{{ .Images.square_medium }}"
|
||||
src="{{ (index .Images 0).Small }}"
|
||||
alt="{{ .Title }}"
|
||||
class="artwork-master-image"
|
||||
/>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<div class="artwork-thumbnail-small artwork-thumbnail">
|
||||
<a href="#">
|
||||
<img
|
||||
src="{{ .Images.square_medium }}"
|
||||
src="{{ (index .Images 0).Small }}"
|
||||
alt="{{ .Title }}"
|
||||
class="artwork-master-image"
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user