Use own struct

This commit is contained in:
daru
2022-04-16 18:13:32 +02:00
parent 412cf2df2e
commit ffc7d5624d
3 changed files with 99 additions and 46 deletions

View File

@@ -4,48 +4,46 @@ import "time"
type Anime struct { type Anime struct {
Anime int64 `json:"anime"` Anime int64 `json:"anime"`
Data AnimeDetail `json:"data"`
Users []WatchUser `json:"users"` Users []WatchUser `json:"users"`
Data AnimeDetail `json:"data"`
} }
type AnimeDetail struct { type AnimeDetail struct {
Title string `json:"title"` Title string `json:"title"`
TitleEn string `json:"title_en"` TitleEn string `json:"title_en"`
TitleJp string `json:"title_jp"` TitleJp string `json:"title_jp"`
Image struct { ImageMediumURL string `json:"image_medium"`
ImageURL string `json:"medium"` ImageLargeURL string `json:"image_large"`
LargeURL string `json:"large"` ImageThumbURL string `json:"image_thumb"`
ThumbURL string `json:"thumb"` Type string `json:"type"`
} `json:"image"` Status string `json:"status"`
Type string `json:"type"` Episodes int `json:"episodes"`
Status string `json:"status"` Synopsis string `json:"synopsis"`
Episodes int `json:"episodes"` Genres []AnimeGenre `json:"genres"`
Synopsis string `json:"synopsis"` StartDate time.Time `json:"start_date"`
Genres []struct { EndDate time.Time `json:"end_date"`
ID int `json:"id"` Year int `json:"year"`
Name string `json:"name"` Season string `json:"season"`
} `json:"genres"` Score float64 `json:"score"`
StartDate time.Time `json:"start_date"` ScoredBy int `json:"scored_by"`
EndDate time.Time `json:"end_date"` Rank int `json:"rank"`
Season struct { Popularity int `json:"popularity"`
Year int `json:"year"` Members int `json:"members"`
Season string `json:"season"` Source string `json:"source"`
} `json:"season"` DayOfTheWeek string `json:"day_of_the_week"`
Score float64 `json:"score"` StartTime string `json:"start_time"`
ScoredBy int `json:"scored_by"` Studios []AnimeStudio `json:"studios"`
Rank int `json:"rank"` TrailerURL string `json:"trailer_url"`
Popularity int `json:"popularity"` }
Members int `json:"members"`
Source string `json:"source"` type AnimeGenre struct {
Broadcast struct { ID int `json:"id"`
DayOfTheWeek string `json:"day_of_the_week"` Name string `json:"name"`
StartTime string `json:"start_time"` }
} `json:"broadcast"`
Studios []struct { type AnimeStudio struct {
ID int `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
} `json:"studios"`
TrailerURL string `json:"trailer_url"`
} }
type WatchUser struct { type WatchUser struct {

View File

@@ -77,28 +77,42 @@ func GetUserBytesCached(username string) ([]byte, error) {
return data, err return data, err
} }
func GetSeasonDataAll() (*SeasonJikan, []byte, error) { func GetSeasonDataAll() ([]Anime, []byte, error) {
color.Infoln("Aktuelle Season abfragen...") color.Infoln("Aktuelle Season abfragen...")
data, bytes, err := GetSeasonData(1) data, _, err := GetSeasonData(1)
if err != nil { if err != nil {
return data, bytes, err return nil, nil, err
} }
color.Infof("%d Anime auf %d Seiten\n", data.Pagination.Items.Total, data.Pagination.LastVisiblePage) color.Infof("%d Anime auf %d Seiten\n", data.Pagination.Items.Total, data.Pagination.LastVisiblePage)
animes := make([]Anime, 0)
// convert to anime
for _, a := range data.Data {
animes = append(animes, Anime{
Anime: a.MalID,
Data: JikanConvert(&a),
})
}
for i := 2; data.Pagination.HasNextPage; i++ { for i := 2; data.Pagination.HasNextPage; i++ {
color.Infof("Seite %d abfragen...\n", i) color.Infof("Seite %d abfragen...\n", i)
time.Sleep(time.Second) time.Sleep(time.Second)
newData, _, err := GetSeasonData(i) newData, _, err := GetSeasonData(i)
if err != nil { if err != nil {
return data, nil, err return nil, nil, err
} }
data.Pagination.CurrentPage = newData.Pagination.CurrentPage data.Pagination.CurrentPage = newData.Pagination.CurrentPage
data.Pagination.HasNextPage = newData.Pagination.HasNextPage data.Pagination.HasNextPage = newData.Pagination.HasNextPage
data.Data = append(data.Data, newData.Data...)
data.Pagination.Items.Count += newData.Pagination.Items.Count data.Pagination.Items.Count += newData.Pagination.Items.Count
// convert to anime
for _, a := range newData.Data {
animes = append(animes, Anime{
Anime: a.MalID,
Data: JikanConvert(&a),
})
}
} }
color.Infof("%d Anime bekommen\n", len(data.Data)) color.Infof("%d Anime bekommen\n", len(animes))
bytes, err = json.Marshal(data) bytes, err := json.Marshal(animes)
return data, bytes, err return animes, bytes, err
} }
func GetSeasonData(page int) (*SeasonJikan, []byte, error) { func GetSeasonData(page int) (*SeasonJikan, []byte, error) {

View File

@@ -5,6 +5,47 @@ import (
"errors" "errors"
) )
func JikanConvert(jik *SeasonAnimeJikan) AnimeDetail {
res := AnimeDetail{
Title: jik.Title,
TitleEn: jik.TitleEnglish,
TitleJp: jik.TitleJapanese,
ImageMediumURL: jik.Images.Jpg.ImageURL,
ImageLargeURL: jik.Images.Jpg.LargeImageURL,
ImageThumbURL: jik.Images.Jpg.SmallImageURL,
Type: jik.Type,
Status: jik.Status,
Episodes: jik.Episodes,
Synopsis: jik.Synopsis,
StartDate: jik.Aired.From,
EndDate: jik.Aired.To,
Year: jik.Year,
Season: jik.Season,
Score: jik.Score,
ScoredBy: jik.ScoredBy,
Rank: jik.Rank,
Popularity: jik.Popularity,
Members: jik.Members,
Source: jik.Source,
DayOfTheWeek: jik.Broadcast.Day,
StartTime: jik.Broadcast.Time,
TrailerURL: jik.Trailer.URL,
}
for _, g := range jik.Genres {
res.Genres = append(res.Genres, AnimeGenre{
ID: g.MalID,
Name: g.Name,
})
}
for _, s := range jik.Studios {
res.Studios = append(res.Studios, AnimeStudio{
ID: s.MalID,
Name: s.Name,
})
}
return res
}
func GetSeasonCache() (*SeasonJikan, error) { func GetSeasonCache() (*SeasonJikan, error) {
data, err := cache.Get(seasonApiJikan) data, err := cache.Get(seasonApiJikan)
if err != nil { if err != nil {