mirror of
https://github.com/ultrasn0w/huso.git
synced 2025-12-13 15:39:53 +01:00
166 lines
4.2 KiB
Go
166 lines
4.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
func JikanConvert(jik *SeasonAnimeJikan) Anime {
|
|
res := Anime{
|
|
Anime: jik.MalID,
|
|
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,
|
|
Weekday: jik.Broadcast.Day,
|
|
TrailerURL: jik.Trailer.URL,
|
|
TrailerEmbedURL: jik.Trailer.EmbedURL,
|
|
}
|
|
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 MalConvert(mal *AnimeDetailMal) Anime {
|
|
res := Anime{
|
|
Anime: mal.ID,
|
|
Title: mal.Title,
|
|
TitleEn: mal.AlternativeTitles.En,
|
|
TitleJp: mal.AlternativeTitles.Ja,
|
|
ImageMediumURL: mal.MainPicture.Medium,
|
|
ImageLargeURL: mal.MainPicture.Large,
|
|
ImageThumbURL: "",
|
|
Type: mal.MediaType,
|
|
Status: mal.Status,
|
|
Episodes: mal.NumEpisodes,
|
|
Synopsis: mal.Synopsis,
|
|
Year: mal.StartSeason.Year,
|
|
Season: mal.StartSeason.Season,
|
|
Score: mal.Mean,
|
|
ScoredBy: mal.NumScoringUsers,
|
|
Rank: mal.Rank,
|
|
Popularity: mal.Popularity,
|
|
Members: mal.NumListUsers,
|
|
Source: mal.Source,
|
|
Weekday: mal.Broadcast.DayOfTheWeek,
|
|
TrailerURL: "",
|
|
}
|
|
for _, g := range mal.Genres {
|
|
res.Genres = append(res.Genres, AnimeGenre{
|
|
ID: g.ID,
|
|
Name: g.Name,
|
|
})
|
|
}
|
|
for _, s := range mal.Studios {
|
|
res.Studios = append(res.Studios, AnimeStudio{
|
|
ID: s.ID,
|
|
Name: s.Name,
|
|
})
|
|
}
|
|
res.StartDate, _ = time.Parse("2006-01-02", mal.StartDate)
|
|
res.EndDate, _ = time.Parse("2006-01-02", mal.EndDate)
|
|
return res
|
|
}
|
|
|
|
func UserConvert(user *UserJikan) User {
|
|
return User{
|
|
MalID: user.Data.MalID,
|
|
Username: user.Data.Username,
|
|
URL: user.Data.URL,
|
|
ImageURL: user.Data.Images.Jpg.ImageURL,
|
|
LastOnline: user.Data.LastOnline,
|
|
Gender: user.Data.Gender,
|
|
Birthday: user.Data.Birthday,
|
|
Location: user.Data.Location,
|
|
Joined: user.Data.Joined,
|
|
}
|
|
}
|
|
|
|
func GetSeasonCache() ([]Anime, error) {
|
|
data, err := seasoncache.Get(seasonApiJikan)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var seasonData []Anime
|
|
err = json.Unmarshal(data, &seasonData)
|
|
return seasonData, err
|
|
}
|
|
|
|
func SearchSeason(animeId int64) (*Anime, error) {
|
|
season, err := GetSeasonCache()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(season) == 0 {
|
|
return nil, errors.New("no seasonal anime")
|
|
}
|
|
for _, a := range season {
|
|
if a.Anime == animeId {
|
|
return &a, err
|
|
}
|
|
}
|
|
return nil, errors.New("anime not found")
|
|
}
|
|
|
|
func FetchProgress(animeId, userId int64, username string, progress int) (int, time.Time, error) {
|
|
// check watching first
|
|
list, _, err := GetUserAnimeListData(username, malApiStatusW)
|
|
if err != nil {
|
|
return 0, time.Time{}, err
|
|
}
|
|
for _, a := range list.Data {
|
|
// check if found
|
|
if a.Node.ID == animeId {
|
|
// check if progress changed
|
|
if progress != a.ListStatus.NumEpisodesWatched {
|
|
return a.ListStatus.NumEpisodesWatched, a.ListStatus.UpdatedAt, nil
|
|
}
|
|
return progress, a.ListStatus.UpdatedAt, nil
|
|
}
|
|
}
|
|
// check completed
|
|
list, _, err = GetUserAnimeListData(username, malApiStatusC)
|
|
if err != nil {
|
|
return 0, time.Time{}, err
|
|
}
|
|
for _, a := range list.Data {
|
|
// check if found
|
|
if a.Node.ID == animeId {
|
|
// check if progress changed
|
|
if progress != a.ListStatus.NumEpisodesWatched {
|
|
return a.ListStatus.NumEpisodesWatched, a.ListStatus.UpdatedAt, nil
|
|
}
|
|
return progress, a.ListStatus.UpdatedAt, nil
|
|
}
|
|
}
|
|
// has no progress or dropped/hold
|
|
return 0, time.Now(), nil
|
|
}
|