mirror of
https://github.com/ultrasn0w/huso.git
synced 2025-12-14 05:39:52 +01:00
Init
This commit is contained in:
102
knecht.go
Normal file
102
knecht.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gookit/color"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
func GetSeasonDataAll() (*SeasonJikan, []byte, error) {
|
||||
color.Infoln("Aktuelle Season abfragen...")
|
||||
data, bytes, err := GetSeasonDataJikan(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 := GetSeasonDataJikan(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 GetSeasonBytesJikan(page int) ([]byte, error) {
|
||||
if page != 0 {
|
||||
return GetDataJikan(seasonApiJikan + "?page=" + strconv.Itoa(page))
|
||||
}
|
||||
return GetDataJikan(seasonApiJikan)
|
||||
}
|
||||
|
||||
func GetSeasonDataJikan(page int) (*SeasonJikan, []byte, error) {
|
||||
var data SeasonJikan
|
||||
body, err := GetSeasonBytesJikan(page)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
err = json.Unmarshal(body, &data)
|
||||
return &data, body, err
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user