mirror of
https://github.com/ultrasn0w/huso.git
synced 2025-12-13 15:39:53 +01:00
145 lines
3.9 KiB
Go
145 lines
3.9 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gookit/color"
|
|
"github.com/valyala/fasthttp"
|
|
)
|
|
|
|
func GetUserAnimeListData(username, status string) (*AnimeListMal, []byte, error) {
|
|
var list AnimeListMal
|
|
body, err := GetUserAnimeListBytes(username, status)
|
|
if err != nil {
|
|
return nil, body, err
|
|
}
|
|
err = json.Unmarshal(body, &list)
|
|
return &list, body, err
|
|
}
|
|
|
|
func GetUserAnimeListBytes(username, status string) ([]byte, error) {
|
|
return GetDataMal(userApiMal + username + "/animelist?limit=1000&status=" + status)
|
|
}
|
|
|
|
func GetUserData(username string) (*UserJikan, []byte, error) {
|
|
var user UserJikan
|
|
body, err := GetUserBytesCached(username)
|
|
if err != nil {
|
|
return nil, body, err
|
|
}
|
|
err = json.Unmarshal(body, &user)
|
|
return &user, body, err
|
|
}
|
|
|
|
func GetUserBytesCached(username string) ([]byte, error) {
|
|
key := userApiJikan + username
|
|
data, err := cache.Get(key)
|
|
if err != nil {
|
|
data, err = GetDataJikan(key)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
if strings.Contains(string(data), "BadResponseException") {
|
|
return data, fmt.Errorf("user not found: %s", username)
|
|
}
|
|
cache.Set(key, data)
|
|
return data, err
|
|
}
|
|
return data, err
|
|
}
|
|
|
|
func GetSeasonDataAll() (*SeasonJikan, []byte, error) {
|
|
color.Infoln("Aktuelle Season abfragen...")
|
|
data, bytes, err := GetSeasonData(1)
|
|
if err != nil {
|
|
return data, bytes, err
|
|
}
|
|
color.Infof("%d Anime auf %d Seiten\n", data.Pagination.Items.Total, data.Pagination.LastVisiblePage)
|
|
for i := 2; data.Pagination.HasNextPage; i++ {
|
|
color.Infof("Seite %d abfragen...\n", i)
|
|
time.Sleep(time.Second)
|
|
newData, _, err := GetSeasonData(i)
|
|
if err != nil {
|
|
return data, nil, err
|
|
}
|
|
data.Pagination.CurrentPage = newData.Pagination.CurrentPage
|
|
data.Pagination.HasNextPage = newData.Pagination.HasNextPage
|
|
data.Data = append(data.Data, newData.Data...)
|
|
data.Pagination.Items.Count += newData.Pagination.Items.Count
|
|
}
|
|
color.Infof("%d Anime bekommen\n", len(data.Data))
|
|
bytes, err = json.Marshal(data)
|
|
return data, bytes, err
|
|
}
|
|
|
|
func GetSeasonData(page int) (*SeasonJikan, []byte, error) {
|
|
var data SeasonJikan
|
|
body, err := GetSeasonBytes(page)
|
|
if err != nil {
|
|
return nil, body, err
|
|
}
|
|
err = json.Unmarshal(body, &data)
|
|
return &data, body, err
|
|
}
|
|
|
|
func GetSeasonBytes(page int) ([]byte, error) {
|
|
if page != 0 {
|
|
return GetDataJikan(seasonApiJikan + "?page=" + strconv.Itoa(page))
|
|
}
|
|
return GetDataJikan(seasonApiJikan)
|
|
}
|
|
|
|
func GetDataMal(apiAddr string) ([]byte, error) {
|
|
var body []byte
|
|
req := fasthttp.AcquireRequest()
|
|
resp := fasthttp.AcquireResponse()
|
|
defer fasthttp.ReleaseRequest(req)
|
|
defer fasthttp.ReleaseResponse(resp)
|
|
|
|
req.SetRequestURI(*malApiBaseUri + apiAddr)
|
|
req.Header.SetMethod(fasthttp.MethodGet)
|
|
req.Header.Add("X-MAL-CLIENT-ID", *malApiId)
|
|
err := fasthttp.Do(req, resp)
|
|
contentEncoding := resp.Header.Peek(fasthttp.HeaderContentEncoding)
|
|
if bytes.EqualFold(contentEncoding, []byte("gzip")) {
|
|
body, _ = resp.BodyGunzip()
|
|
} else {
|
|
body = resp.Body()
|
|
}
|
|
|
|
if resp.StatusCode() != fasthttp.StatusOK {
|
|
return body, fmt.Errorf("unexpected response code: %s %d", *malApiBaseUri+apiAddr, resp.StatusCode())
|
|
}
|
|
return body, err
|
|
}
|
|
|
|
func GetDataJikan(apiAddr string) ([]byte, error) {
|
|
var body []byte
|
|
statusCode, body, err := fasthttp.Get(body, *jikanApiBaseUri+apiAddr)
|
|
if statusCode != fasthttp.StatusOK {
|
|
return body, fmt.Errorf("unexpected response code: %s %d", *jikanApiBaseUri+apiAddr, statusCode)
|
|
}
|
|
return body, err
|
|
}
|
|
|
|
func GetCurrentSeasonString() string {
|
|
var now = time.Now()
|
|
switch now.Month() {
|
|
case time.January, time.February, time.March:
|
|
return fmt.Sprintf("%04d/winter", now.Year())
|
|
case time.April, time.May, time.June:
|
|
return fmt.Sprintf("%04d/spring", now.Year())
|
|
case time.July, time.August, time.September:
|
|
return fmt.Sprintf("%04d/summer", now.Year())
|
|
case time.October, time.November, time.December:
|
|
return fmt.Sprintf("%04d/fall", now.Year())
|
|
default:
|
|
return fmt.Sprintf("%04d", now.Year())
|
|
}
|
|
}
|