bring back some core/user refactor

This commit is contained in:
perennial
2024-10-19 18:26:43 +11:00
parent 6520451b24
commit c5bd4fe797
2 changed files with 48 additions and 61 deletions
+2 -2
View File
@@ -67,13 +67,13 @@ func GetUserBookmarksURL(id, mode string, page int) string {
return fmt.Sprintf(base, id, page*48, mode)
}
func GetFrequentArtworkTagsURL(ids string) string {
func GetUserFrequentArtworkTagsURL(ids string) string {
base := "https://www.pixiv.net/ajax/tags/frequent/illust?%s"
return fmt.Sprintf(base, ids)
}
func GetFrequentNovelTagsURL(ids string) string {
func GetUserFrequentNovelTagsURL(ids string) string {
base := "https://www.pixiv.net/ajax/tags/frequent/novel?%s"
return fmt.Sprintf(base, ids)
+46 -59
View File
@@ -107,14 +107,52 @@ func (s *User) ParseSocial() error {
return nil
}
func GetFrequentTags(r *http.Request, ids string, category UserArtCategory) ([]FrequentTag, error) {
// Work is a generic type constraint.
type Work interface{}
// GetUserWorks is a generic helper function to fetch user works.
func GetUserWorks[T Work](r *http.Request, url string) ([]T, error) {
resp, err := API_GET_UnwrapJson(r.Context(), url, "", r.Header)
if err != nil {
return nil, err
}
resp = session.ProxyImageUrl(r, resp)
// Define a generic body structure.
var body struct {
Works map[int]json.RawMessage `json:"works"`
}
// Unmarshal the response into the body.
if err := json.Unmarshal([]byte(resp), &body); err != nil {
return nil, err
}
// Initialize the slice to hold the works.
var works []T
// Iterate over each work and unmarshal into the specific type.
for _, v := range body.Works {
var work T
if err := json.Unmarshal(v, &work); err != nil {
return nil, err
}
works = append(works, work)
}
return works, nil
}
// GetUserFrequentTags retrieves frequent tags for a user based on category.
func GetUserFrequentTags(r *http.Request, ids string, category UserArtCategory) ([]FrequentTag, error) {
var tags []FrequentTag
var URL string
if category != "novels" {
URL = GetFrequentArtworkTagsURL(ids)
URL = GetUserFrequentArtworkTagsURL(ids)
} else {
URL = GetFrequentNovelTagsURL(ids)
URL = GetUserFrequentNovelTagsURL(ids)
}
response, err := API_GET_UnwrapJson(r.Context(), URL, "", r.Header)
@@ -130,77 +168,26 @@ func GetFrequentTags(r *http.Request, ids string, category UserArtCategory) ([]F
return tags, nil
}
// GetUserArtworkList fetches the list of artworks for a user.
func GetUserArtworkList(r *http.Request, id, ids string) ([]ArtworkBrief, error) {
var works []ArtworkBrief
var illustrations []ArtworkBrief
var manga []ArtworkBrief
URL := GetUserFullArtworkURL(id, ids)
resp, err := API_GET_UnwrapJson(r.Context(), URL, "", r.Header)
works, err := GetUserWorks[ArtworkBrief](r, URL)
if err != nil {
return nil, err
}
resp = session.ProxyImageUrl(r, resp)
var body struct {
Illusts map[int]json.RawMessage `json:"works"`
}
err = json.Unmarshal([]byte(resp), &body)
if err != nil {
return nil, err
}
for _, v := range body.Illusts {
var illust ArtworkBrief
err = json.Unmarshal(v, &illust)
if err != nil {
return nil, err
}
works = append(works, illust)
if illust.IllustType == 0 {
illustrations = append(illustrations, illust)
} else if illust.IllustType == 1 {
manga = append(manga, illust)
}
}
return works, nil
}
// GetUserNovels fetches the list of novels for a user.
func GetUserNovels(r *http.Request, id, ids string) ([]NovelBrief, error) {
// VnPower: we can merge this function into GetUserArtworks (now GetUserArtworkList), but I want to make things simple for now
var works []NovelBrief
URL := GetUserFullNovelURL(id, ids)
resp, err := API_GET_UnwrapJson(r.Context(), URL, "", r.Header)
works, err := GetUserWorks[NovelBrief](r, URL)
if err != nil {
return nil, err
}
resp = session.ProxyImageUrl(r, resp)
var body struct {
Novels map[int]json.RawMessage `json:"works"`
}
err = json.Unmarshal([]byte(resp), &body)
if err != nil {
return nil, err
}
for _, v := range body.Novels {
var novel NovelBrief
err = json.Unmarshal(v, &novel)
if err != nil {
return nil, err
}
works = append(works, novel)
}
return works, nil
}
@@ -462,7 +449,7 @@ func GetUserProfile(r *http.Request, id string, category UserArtCategory, page i
}
if getTags {
user.FrequentTags, err = GetFrequentTags(r, ids, category)
user.FrequentTags, err = GetUserFrequentTags(r, ids, category)
if err != nil {
return user, err
}