2023-05-13 10:03:13 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"pixivfe/entity"
|
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetRecommendedIllust(c *gin.Context) []entity.Illust {
|
|
|
|
URL := "https://hibi.cocomi.cf/api/pixiv/illust_recommended"
|
|
|
|
var illusts []entity.Illust
|
|
|
|
|
2023-05-14 03:56:50 +00:00
|
|
|
s := Request(URL)
|
|
|
|
g := GetInnerJSON(s, "illusts")
|
2023-05-13 10:03:13 +00:00
|
|
|
|
2023-05-14 03:56:50 +00:00
|
|
|
err := json.Unmarshal([]byte(g), &illusts)
|
2023-05-13 10:03:13 +00:00
|
|
|
if err != nil {
|
2023-05-14 03:56:50 +00:00
|
|
|
panic("Failed to parse JSON")
|
2023-05-13 10:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return illusts
|
|
|
|
}
|
|
|
|
|
2023-05-13 12:25:32 +00:00
|
|
|
func GetRankingIllust(c *gin.Context, mode string) []entity.Illust {
|
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
|
|
|
var illusts []entity.Illust
|
|
|
|
|
2023-05-14 03:56:50 +00:00
|
|
|
s := Request(URL)
|
|
|
|
g := GetInnerJSON(s, "illusts")
|
2023-05-13 12:25:32 +00:00
|
|
|
|
2023-05-14 03:56:50 +00:00
|
|
|
err := json.Unmarshal([]byte(g), &illusts)
|
2023-05-14 04:11:44 +00:00
|
|
|
if err != nil {
|
|
|
|
panic("Failed to parse JSON")
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2023-05-13 12:25:32 +00:00
|
|
|
if err != nil {
|
2023-05-14 03:56:50 +00:00
|
|
|
panic("Failed to parse JSON")
|
2023-05-13 10:03:13 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 12:25:32 +00:00
|
|
|
return illusts
|
2023-05-13 10:03:13 +00:00
|
|
|
}
|
2023-05-13 14:33:40 +00:00
|
|
|
|
|
|
|
func GetSpotlightArticle(c *gin.Context) []entity.Spotlight {
|
2023-05-14 03:56:50 +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)
|
|
|
|
g := GetInnerJSON(s, "articles")
|
|
|
|
|
|
|
|
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 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
|
|
|
}
|