pixivfe/handler/_parser.go

226 lines
5 KiB
Go
Raw Normal View History

package handler
import (
"encoding/json"
2023-05-16 12:59:42 +00:00
"fmt"
"net/http"
"pixivfe/entity"
2023-05-15 23:21:20 +00:00
"regexp"
"io/ioutil"
"github.com/gin-gonic/gin"
"github.com/tidwall/gjson"
)
2023-05-15 23:21:20 +00:00
func ImageProxy(url string) string {
2023-05-15 05:47:51 +00:00
2023-05-15 23:21:20 +00:00
regex := regexp.MustCompile(`i\.pximg\.net`)
proxy := "px2.rainchan.win"
2023-05-15 05:47:51 +00:00
2023-05-15 23:21:20 +00:00
return regex.ReplaceAllString(url, proxy)
}
2023-05-16 12:59:42 +00:00
func ParseImages(data string) ([]entity.Image, error) {
// Parse illusts images
2023-05-15 23:21:20 +00:00
var images []entity.Image
if gjson.Get(data, "meta_single_page.original_image_url").Exists() {
var image entity.Image
2023-05-16 12:35:39 +00:00
image.Small = ImageProxy(GetInnerJSON(data, "image_urls.square_medium"))
image.Medium = ImageProxy(GetInnerJSON(data, "image_urls.medium"))
image.Large = ImageProxy(GetInnerJSON(data, "image_urls.large"))
image.Original = ImageProxy(GetInnerJSON(data, "meta_single_page.original_image_url"))
2023-05-15 23:21:20 +00:00
images = append(images, image)
} else {
g := GetInnerJSON(data, "meta_pages.#.image_urls")
err := json.Unmarshal([]byte(g), &images)
if err != nil {
2023-05-16 12:59:42 +00:00
return nil, fmt.Errorf("Failed to parse JSON for images.\n%s", err)
2023-05-15 23:21:20 +00:00
}
}
2023-05-16 12:59:42 +00:00
return images, nil
2023-05-15 23:21:20 +00:00
}
2023-05-16 12:59:42 +00:00
func ParseIllust(data string) (entity.Illust, error) {
var illust entity.Illust
2023-05-16 12:59:42 +00:00
images, err := ParseImages(data)
if err != nil {
return illust, fmt.Errorf("Failed to parse images from illust.\n%s", err)
}
illust.Images = images
2023-05-16 12:59:42 +00:00
err = json.Unmarshal([]byte(data), &illust)
if err != nil {
2023-05-16 12:59:42 +00:00
return illust, fmt.Errorf("Failed to parse JSON for illust.\n%s", err)
}
2023-05-16 12:59:42 +00:00
return illust, nil
}
2023-05-16 12:59:42 +00:00
func ParseIllusts(data string) ([]entity.Illust, error) {
2023-05-15 23:21:20 +00:00
var illusts []entity.Illust
g := gjson.Get(data, "illusts").Array()
2023-05-15 23:21:20 +00:00
for _, illust := range g {
2023-05-16 12:59:42 +00:00
illust, err := ParseIllust(illust.String())
if err != nil {
return nil, fmt.Errorf("Failed to parse illusts.\n%s", err)
}
illusts = append(illusts, illust)
2023-05-15 05:47:51 +00:00
}
2023-05-16 12:59:42 +00:00
return illusts, nil
2023-05-15 05:47:51 +00:00
}
2023-05-16 12:59:42 +00:00
func GetRecommendedIllust(c *gin.Context) ([]entity.Illust, error) {
URL := "https://hibi.cocomi.cf/api/pixiv/illust_recommended"
2023-05-14 03:56:50 +00:00
s := Request(URL)
2023-05-16 12:59:42 +00:00
illusts, err := ParseIllusts(s)
if err != nil {
return illusts, fmt.Errorf("Failed to get recommended illusts.\n%s", err)
}
2023-05-16 12:59:42 +00:00
return illusts, nil
}
2023-05-16 12:59:42 +00:00
func GetRankingIllust(c *gin.Context, mode string) ([]entity.Illust, error) {
2023-05-14 09:30:17 +00:00
URL := "https://hibi.cocomi.cf/api/pixiv/rank?page=1&mode=" + mode
2023-05-13 12:25:32 +00:00
2023-05-14 03:56:50 +00:00
s := Request(URL)
2023-05-16 12:59:42 +00:00
illusts, err := ParseIllusts(s)
if err != nil {
return illusts, fmt.Errorf("Failed to get recommended illusts.\n%s", err)
}
2023-05-14 04:11:44 +00:00
2023-05-16 12:59:42 +00:00
return illusts, nil
2023-05-14 04:11:44 +00:00
}
2023-05-16 12:59:42 +00:00
func GetNewestIllust(c *gin.Context) ([]entity.Illust, error) {
2023-05-14 04:11:44 +00:00
URL := "https://hibi.cocomi.cf/api/pixiv/illust_new"
s := Request(URL)
2023-05-16 12:59:42 +00:00
illusts, err := ParseIllusts(s)
if err != nil {
return illusts, fmt.Errorf("Failed to get recommended illusts.\n%s", err)
}
2023-05-16 12:59:42 +00:00
return illusts, nil
}
2023-05-13 14:33:40 +00:00
2023-05-16 12:59:42 +00:00
func GetMemberIllust(c *gin.Context, id string) ([]entity.Illust, error) {
URL := "https://hibi.cocomi.cf/api/pixiv/member_illust?id=" + id
s := Request(URL)
2023-05-16 12:59:42 +00:00
illusts, err := ParseIllusts(s)
if err != nil {
return illusts, fmt.Errorf("Failed to get a member's illusts.\n%s", err)
}
2023-05-16 12:59:42 +00:00
return illusts, nil
}
2023-05-16 12:59:42 +00:00
func GetRelatedIllust(c *gin.Context) ([]entity.Illust, error) {
2023-05-14 12:30:03 +00:00
id := c.Param("id")
URL := "https://hibi.cocomi.cf/api/pixiv/related?id=" + id
s := Request(URL)
2023-05-16 12:59:42 +00:00
illusts, err := ParseIllusts(s)
2023-05-14 12:30:03 +00:00
2023-05-16 12:59:42 +00:00
if err != nil {
return illusts, fmt.Errorf("Failed to get related illusts.\n%s", err)
}
return illusts, nil
2023-05-14 12:30:03 +00:00
}
2023-05-16 12:59:42 +00:00
func GetIllustByID(c *gin.Context) (entity.Illust, error) {
2023-05-14 12:30:03 +00:00
id := c.Param("id")
URL := "https://hibi.cocomi.cf/api/pixiv/illust?id=" + id
s := Request(URL)
g := GetInnerJSON(s, "illust")
2023-05-16 12:59:42 +00:00
illust, err := ParseIllust(g)
if err != nil {
return illust, fmt.Errorf("Failed to get illust by ID.\n%s", err)
}
2023-05-14 12:30:03 +00:00
2023-05-16 12:59:42 +00:00
return illust, nil
2023-05-14 12:30:03 +00:00
}
2023-05-13 14:33:40 +00:00
func GetSpotlightArticle(c *gin.Context) []entity.Spotlight {
2023-05-14 12:30:03 +00:00
URL := "https://hibi.cocomi.cf/api/pixiv/spotlights?lang=en"
// URL := "https://now.pixiv.pics/api/pixivision?lang=en"
2023-05-13 14:33:40 +00:00
var articles []entity.Spotlight
2023-05-14 03:56:50 +00:00
s := Request(URL)
2023-05-14 12:30:03 +00:00
g := GetInnerJSON(s, "spotlight_articles")
2023-05-14 03:56:50 +00:00
err := json.Unmarshal([]byte(g), &articles)
2023-05-13 14:33:40 +00:00
if err != nil {
2023-05-14 03:56:50 +00:00
panic("Failed to parse JSON")
2023-05-13 14:33:40 +00:00
}
2023-05-14 03:56:50 +00:00
return articles
}
func GetUserInfo(c *gin.Context) (entity.User, error) {
id := c.Param("id")
URL := "https://hibi.cocomi.cf/api/pixiv/member?id=" + id
var user entity.User
s := Request(URL)
user_string := GetInnerJSON(s, "user")
profile_string := GetInnerJSON(s, "profile")
err := json.Unmarshal([]byte(user_string), &user)
if err != nil {
panic("Failed to parse JSON")
}
err = json.Unmarshal([]byte(profile_string), &user)
if err != nil {
panic("Failed to parse JSON")
}
return user, nil
}
2023-05-14 03:56:50 +00:00
func GetInnerJSON(resp string, key string) string {
// As I see, the API always start its response with { "key": [...] }
return gjson.Get(resp, key).String()
}
func Request(URL string) string {
client := &http.Client{}
req, _ := http.NewRequest("GET", URL, nil)
req.Header.Set("accept-language", "en")
resp, err := client.Do(req)
2023-05-13 14:33:40 +00:00
if err != nil {
2023-05-14 03:56:50 +00:00
panic("Failed to make a request to " + URL)
2023-05-13 14:33:40 +00:00
}
2023-05-14 03:56:50 +00:00
body, err := ioutil.ReadAll(resp.Body)
2023-05-13 14:33:40 +00:00
if err != nil {
2023-05-14 03:56:50 +00:00
panic("Failed to parse response")
2023-05-13 14:33:40 +00:00
}
2023-05-14 03:56:50 +00:00
return string(body)
2023-05-13 14:33:40 +00:00
}