diff --git a/entity/models.go b/entity/models.go index 74420f1..0182d76 100644 --- a/entity/models.go +++ b/entity/models.go @@ -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 { diff --git a/handler/illust.go b/handler/illust.go index 18634c8..ffe99ab 100644 --- a/handler/illust.go +++ b/handler/illust.go @@ -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 } diff --git a/template/artwork.html b/template/artwork.html index 037aa44..c3b4f15 100644 --- a/template/artwork.html +++ b/template/artwork.html @@ -5,18 +5,10 @@ {{ with .Illust }}