Commit 00c3caa649
Unsigned
Layout: unified · split
internal/jikan.go +35 −9
| @@ -17,21 +17,47 @@ func GetEpisodeData(animeID int, episodeNo int, anime *Anime) error { | |||
| 17 | return fmt.Errorf("error fetching data from Jikan (MyAnimeList) API: %w", err) | 17 | return fmt.Errorf("error fetching data from Jikan (MyAnimeList) API: %w", err) |
| 18 | } | 18 | } |
| 19 | 19 | ||
| 20 | Log(response, logFile) | ||
| 21 | |||
| 20 | // Check if the 'data' field exists and is valid | 22 | // Check if the 'data' field exists and is valid |
| 21 | data, ok := response["data"].(map[string]interface{}) | 23 | data, ok := response["data"].(map[string]interface{}) |
| 22 | if !ok { | 24 | if !ok { |
| 23 | return fmt.Errorf("invalid response structure: missing or invalid 'data' field") | 25 | return fmt.Errorf("invalid response structure: missing or invalid 'data' field") |
| 24 | } | 26 | } |
| 27 | // Helper function to safely get string value | ||
| 28 | getStringValue := func(field string) string { | ||
| 29 | if value, ok := data[field].(string); ok { | ||
| 30 | return value | ||
| 31 | } | ||
| 32 | return "" | ||
| 33 | } | ||
| 34 | |||
| 35 | // Helper function to safely get int value | ||
| 36 | getIntValue := func(field string) int { | ||
| 37 | if value, ok := data[field].(float64); ok { | ||
| 38 | return int(value) | ||
| 39 | } | ||
| 40 | return 0 | ||
| 41 | } | ||
| 42 | |||
| 43 | // Helper function to safely get bool value | ||
| 44 | getBoolValue := func(field string) bool { | ||
| 45 | if value, ok := data[field].(bool); ok { | ||
| 46 | return value | ||
| 47 | } | ||
| 48 | return false | ||
| 49 | } | ||
| 50 | |||
| 51 | // Safely assign values to the Anime struct | ||
| 52 | anime.Ep.Title.Romaji = getStringValue("title_romanji") | ||
| 53 | anime.Ep.Title.English = getStringValue("title") | ||
| 54 | anime.Ep.Title.Japanese = getStringValue("title_japanese") | ||
| 55 | anime.Ep.Aired = getStringValue("aired") | ||
| 56 | anime.Ep.Duration = getIntValue("duration") | ||
| 57 | anime.Ep.IsFiller = getBoolValue("filler") | ||
| 58 | anime.Ep.IsRecap = getBoolValue("recap") | ||
| 59 | anime.Ep.Synopsis = getStringValue("synopsis") | ||
| 25 | 60 | ||
| 26 | // Unmarshal the data into the Anime struct | ||
| 27 | anime.Ep.Title.Romaji = data["title_romanji"].(string) | ||
| 28 | anime.Ep.Title.English = data["title"].(string) | ||
| 29 | anime.Ep.Title.Japanese = data["title_japanese"].(string) | ||
| 30 | anime.Ep.Aired = data["aired"].(string) | ||
| 31 | anime.Ep.Duration = int(data["duration"].(float64)) | ||
| 32 | anime.Ep.IsFiller = data["filler"].(bool) | ||
| 33 | anime.Ep.IsRecap = data["recap"].(bool) | ||
| 34 | anime.Ep.Synopsis = data["synopsis"].(string) | ||
| 35 | return nil | 61 | return nil |
| 36 | } | 62 | } |
| 37 | 63 | ||