Optimization: split the request function

This commit is contained in:
VnPower
2023-06-15 21:47:27 +07:00
parent 49112b8aa0
commit 59689ab87e
5 changed files with 70 additions and 120 deletions
+15 -33
View File
@@ -1,7 +1,6 @@
package handler
import (
"errors"
"fmt"
"sort"
"strconv"
@@ -11,21 +10,17 @@ import (
)
func (p *PixivClient) GetArtworkImages(id string) ([]models.Image, error) {
s, _ := p.TextRequest(fmt.Sprintf(ArtworkImagesURL, id))
var pr models.PixivResponse
var resp []models.ImageResponse
var images []models.Image
err := json.Unmarshal([]byte(s), &pr)
URL := fmt.Sprintf(ArtworkImagesURL, id)
response, err := p.PixivRequest(URL)
if err != nil {
return images, err
}
if pr.Error {
return images, errors.New(fmt.Sprintf("Pixiv returned error message: %s", pr.Message))
return nil, err
}
err = json.Unmarshal([]byte(pr.Body), &resp)
err = json.Unmarshal([]byte(response), &resp)
if err != nil {
return images, err
}
@@ -46,19 +41,14 @@ func (p *PixivClient) GetArtworkImages(id string) ([]models.Image, error) {
}
func (p *PixivClient) GetArtworkByID(id string) (*models.Illust, error) {
s, _ := p.TextRequest(fmt.Sprintf(ArtworkInformationURL, id))
var pr models.PixivResponse
var images []models.Image
// Parse Pixiv response body
err := json.Unmarshal([]byte(s), &pr)
URL := fmt.Sprintf(ArtworkInformationURL, id)
response, err := p.PixivRequest(URL)
if err != nil {
return nil, err
}
if pr.Error {
return nil, errors.New(fmt.Sprintf("Pixiv returned error message: %s", pr.Message))
}
var illust struct {
*models.Illust
@@ -68,7 +58,7 @@ func (p *PixivClient) GetArtworkByID(id string) (*models.Illust, error) {
}
// Parse basic illust information
err = json.Unmarshal([]byte(pr.Body), &illust)
err = json.Unmarshal([]byte(response), &illust)
if err != nil {
return nil, err
}
@@ -140,43 +130,35 @@ func (p *PixivClient) GetArtworkByID(id string) (*models.Illust, error) {
}
func (p *PixivClient) GetArtworkComments(id string) ([]models.Comment, error) {
var pr models.PixivResponse
var body struct {
Comments []models.Comment `json:"comments"`
}
s, _ := p.TextRequest(fmt.Sprintf(ArtworkCommentsURL, id))
err := json.Unmarshal([]byte(s), &pr)
URL := fmt.Sprintf(ArtworkCommentsURL, id)
response, err := p.PixivRequest(URL)
if err != nil {
return nil, err
}
if pr.Error {
return nil, errors.New(fmt.Sprintf("Pixiv returned error message: %s", pr.Message))
}
err = json.Unmarshal([]byte(pr.Body), &body)
err = json.Unmarshal([]byte(response), &body)
return body.Comments, nil
}
func (p *PixivClient) GetRelatedArtworks(id string) ([]models.IllustShort, error) {
url := fmt.Sprintf(ArtworkRelatedURL, id, 30)
var pr models.PixivResponse
var body struct {
Illusts []models.IllustShort `json:"illusts"`
}
s, _ := p.TextRequest(url)
URL := fmt.Sprintf(ArtworkCommentsURL, id)
err := json.Unmarshal([]byte(s), &pr)
response, err := p.PixivRequest(URL)
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(pr.Body), &body)
err = json.Unmarshal([]byte(response), &body)
if err != nil {
return nil, err
}
+24
View File
@@ -1,10 +1,12 @@
package handler
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"pixivfe/models"
)
type PixivClient struct {
@@ -68,6 +70,7 @@ func (p *PixivClient) Request(URL string) (*http.Response, error) {
}
func (p *PixivClient) TextRequest(URL string) (string, error) {
/// Make a request to a URL and return the response's string body
resp, err := p.Request(URL)
if err != nil {
return "", err
@@ -81,3 +84,24 @@ func (p *PixivClient) TextRequest(URL string) (string, error) {
return string(body), nil
}
func (p *PixivClient) PixivRequest(URL string) (json.RawMessage, error) {
/// Make a request to a Pixiv API URL with a standard response, handle errors and return the raw JSON response
var response models.PixivResponse
body, err := p.TextRequest(URL)
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(body), &response)
if err != nil {
return nil, err
}
if response.Error {
// Pixiv returned an error
return nil, errors.New("Pixiv responded: " + response.Message)
}
return response.Body, nil
}
+10 -29
View File
@@ -1,7 +1,6 @@
package handler
import (
"errors"
"fmt"
"pixivfe/models"
"strings"
@@ -10,19 +9,13 @@ import (
)
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)
URL := fmt.Sprintf(ArtworkNewestURL, worktype, r18, lastID)
s, err := p.TextRequest(url)
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(s), &pr)
response, err := p.PixivRequest(URL)
if err != nil {
return nil, err
}
@@ -32,7 +25,7 @@ func (p *PixivClient) GetNewestArtworks(worktype string, r18 string) ([]models.I
LastID string `json:"lastId"`
}
err = json.Unmarshal([]byte(pr.Body), &body)
err = json.Unmarshal([]byte(response), &body)
if err != nil {
return nil, err
}
@@ -65,20 +58,15 @@ func (p *PixivClient) GetRanking(mode string, content string, page string) (mode
}
func (p *PixivClient) GetSearch(artworkType string, name string, order string, age_settings string, page string) (*models.SearchResult, error) {
var pr models.PixivResponse
url := fmt.Sprintf(SearchArtworksURL, artworkType, name, order, age_settings, page)
s, err := p.TextRequest(url)
err = json.Unmarshal([]byte(s), &pr)
URL := fmt.Sprintf(SearchArtworksURL, artworkType, name, order, age_settings, page)
response, err := p.PixivRequest(URL)
if err != nil {
return nil, err
}
// IDK how to do better than this lol
temp := strings.ReplaceAll(string(pr.Body), `"illust"`, `"works"`)
temp := strings.ReplaceAll(string(response), `"illust"`, `"works"`)
temp = strings.ReplaceAll(temp, `"manga"`, `"works"`)
temp = strings.ReplaceAll(temp, `"illustManga"`, `"works"`)
@@ -110,29 +98,22 @@ func (p *PixivClient) GetDiscoveryArtwork(mode string, count int) ([]models.Illu
var artworks []models.IllustShort
for count > 0 {
var pr models.PixivResponse
itemsForRequest := Min(100, count)
count -= itemsForRequest
url := fmt.Sprintf(ArtworkDiscoveryURL, mode, itemsForRequest)
s, err := p.TextRequest(url)
URL := fmt.Sprintf(ArtworkDiscoveryURL, mode, itemsForRequest)
response, err := p.PixivRequest(URL)
if err != nil {
return artworks, err
}
err = json.Unmarshal([]byte(s), &pr)
if pr.Error {
return artworks, errors.New(pr.Message)
return nil, err
}
var thumbnail struct {
Data json.RawMessage `json:"thumbnails"`
}
err = json.Unmarshal([]byte(pr.Body), &thumbnail)
err = json.Unmarshal([]byte(response), &thumbnail)
if err != nil {
return nil, err
}
+8 -24
View File
@@ -1,31 +1,22 @@
package handler
import (
"errors"
"fmt"
"github.com/goccy/go-json"
"pixivfe/models"
)
func (p *PixivClient) GetTagData(name string) (models.TagDetail, error) {
var pr models.PixivResponse
var tag models.TagDetail
url := fmt.Sprintf(SearchTagURL, name)
s, err := p.TextRequest(url)
err = json.Unmarshal([]byte(s), &pr)
URL := fmt.Sprintf(SearchTagURL, name)
response, err := p.PixivRequest(URL)
if err != nil {
return tag, err
}
if pr.Error {
return tag, errors.New(fmt.Sprintf("Pixiv returned error message: %s", pr.Message))
}
err = json.Unmarshal([]byte(pr.Body), &tag)
err = json.Unmarshal([]byte(response), &tag)
if err != nil {
return tag, err
}
@@ -34,20 +25,13 @@ func (p *PixivClient) GetTagData(name string) (models.TagDetail, error) {
}
func (p *PixivClient) GetFrequentTags(ids string) ([]models.FrequentTag, error) {
s, _ := p.TextRequest(fmt.Sprintf(FrequentTagsURL, ids))
var pr models.PixivResponse
var tags []models.FrequentTag
// Parse Pixiv response body
err := json.Unmarshal([]byte(s), &pr)
if err != nil {
return nil, err
}
if pr.Error {
return nil, errors.New(fmt.Sprintf("Pixiv returned error message: %s", pr.Message))
}
err = json.Unmarshal([]byte(pr.Body), &tags)
URL := fmt.Sprintf(FrequentTagsURL, ids)
response, err := p.PixivRequest(URL)
err = json.Unmarshal([]byte(response), &tags)
if err != nil {
return nil, err
}
+13 -34
View File
@@ -12,18 +12,12 @@ import (
)
func (p *PixivClient) GetUserArtworksID(id string, category string, page int) (string, int, error) {
s, _ := p.TextRequest(fmt.Sprintf(UserArtworksURL, id))
var pr models.PixivResponse
err := json.Unmarshal([]byte(s), &pr)
URL := fmt.Sprintf(UserArtworksURL, id)
response, err := p.PixivRequest(URL)
if err != nil {
return "", -1, err
}
if pr.Error {
return "", -1, errors.New(fmt.Sprintf("Pixiv returned error message: %s", pr.Message))
}
var ids []int
var idsString string
@@ -32,7 +26,7 @@ func (p *PixivClient) GetUserArtworksID(id string, category string, page int) (s
Mangas json.RawMessage `json:"manga"`
}
err = json.Unmarshal(pr.Body, &body)
err = json.Unmarshal(response, &body)
if err != nil {
return "", -1, err
}
@@ -83,17 +77,11 @@ func (p *PixivClient) GetUserArtworksID(id string, category string, page int) (s
}
func (p *PixivClient) GetUserArtworks(id string, ids string) ([]models.IllustShort, error) {
url := fmt.Sprintf(UserArtworksFullURL, id, ids)
var pr models.PixivResponse
var works []models.IllustShort
s, err := p.TextRequest(url)
if err != nil {
return nil, err
}
URL := fmt.Sprintf(UserArtworksFullURL, id, ids)
err = json.Unmarshal([]byte(s), &pr)
response, err := p.PixivRequest(URL)
if err != nil {
return nil, err
}
@@ -102,7 +90,7 @@ func (p *PixivClient) GetUserArtworks(id string, ids string) ([]models.IllustSho
Illusts map[int]json.RawMessage `json:"works"`
}
err = json.Unmarshal(pr.Body, &body)
err = json.Unmarshal(response, &body)
if err != nil {
return nil, err
}
@@ -118,20 +106,16 @@ func (p *PixivClient) GetUserArtworks(id string, ids string) ([]models.IllustSho
}
func (p *PixivClient) GetUserBasicInformation(id string) (models.UserShort, error) {
var pr models.PixivResponse
var user models.UserShort
s, _ := p.TextRequest(fmt.Sprintf(UserBasicInformationURL, id))
URL := fmt.Sprintf(UserBasicInformationURL, id)
err := json.Unmarshal([]byte(s), &pr)
response, err := p.PixivRequest(URL)
if err != nil {
return user, err
}
if pr.Error {
return user, errors.New(fmt.Sprintf("Pixiv returned error message: %s", pr.Message))
}
err = json.Unmarshal([]byte(pr.Body), &user)
err = json.Unmarshal([]byte(response), &user)
if err != nil {
return user, err
}
@@ -141,22 +125,17 @@ func (p *PixivClient) GetUserBasicInformation(id string) (models.UserShort, erro
func (p *PixivClient) GetUserInformation(id string, category string, page int) (*models.User, error) {
var user *models.User
var pr models.PixivResponse
ids, count, err := p.GetUserArtworksID(id, category, page)
if err != nil {
return nil, err
}
s, _ := p.TextRequest(fmt.Sprintf(UserInformationURL, id))
err = json.Unmarshal([]byte(s), &pr)
URL := fmt.Sprintf(UserInformationURL, id)
response, err := p.PixivRequest(URL)
if err != nil {
return nil, err
}
if pr.Error {
return nil, errors.New(fmt.Sprintf("Pixiv returned error message: %s", pr.Message))
return user, err
}
var body struct {
@@ -165,7 +144,7 @@ func (p *PixivClient) GetUserInformation(id string, category string, page int) (
}
// Basic user information
err = json.Unmarshal([]byte(pr.Body), &body)
err = json.Unmarshal([]byte(response), &body)
if err != nil {
return nil, err
}