Commit 6f19377ffb
Unsigned
Layout: unified · split
internal/jikan.go added +72
| @@ -0,0 +1,72 @@ | |||
| 1 | package internal | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "encoding/json" | ||
| 5 | "fmt" | ||
| 6 | "net/http" | ||
| 7 | "io" | ||
| 8 | ) | ||
| 9 | |||
| 10 | // GetEpisodeData fetches episode data for a given anime ID and episode number | ||
| 11 | func GetEpisodeData(animeID int, episodeNo int, anime *Anime) error { | ||
| 12 | url := fmt.Sprintf("https://api.jikan.moe/v4/anime/%d/episodes/%d", animeID, episodeNo) | ||
| 13 | |||
| 14 | // Use the helper function for making the GET request | ||
| 15 | response, err := makeGetRequest(url, nil) | ||
| 16 | if err != nil { | ||
| 17 | return fmt.Errorf("error fetching data from Jikan (MyAnimeList) API: %w", err) | ||
| 18 | } | ||
| 19 | |||
| 20 | // Check if the 'data' field exists and is valid | ||
| 21 | data, ok := response["data"].(map[string]interface{}) | ||
| 22 | if !ok { | ||
| 23 | return fmt.Errorf("invalid response structure: missing or invalid 'data' field") | ||
| 24 | } | ||
| 25 | |||
| 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 | ||
| 36 | } | ||
| 37 | |||
| 38 | // Helper function to make GET requests | ||
| 39 | func makeGetRequest(url string, headers map[string]string) (map[string]interface{}, error) { | ||
| 40 | req, err := http.NewRequest("GET", url, nil) | ||
| 41 | if err != nil { | ||
| 42 | return nil, fmt.Errorf("failed to create GET request: %w", err) | ||
| 43 | } | ||
| 44 | |||
| 45 | for key, value := range headers { | ||
| 46 | req.Header.Set(key, value) | ||
| 47 | } | ||
| 48 | |||
| 49 | client := &http.Client{} | ||
| 50 | resp, err := client.Do(req) | ||
| 51 | if err != nil { | ||
| 52 | return nil, fmt.Errorf("failed to send GET request: %w", err) | ||
| 53 | } | ||
| 54 | defer resp.Body.Close() | ||
| 55 | |||
| 56 | body, err := io.ReadAll(resp.Body) | ||
| 57 | if err != nil { | ||
| 58 | return nil, fmt.Errorf("failed to read response body: %w", err) | ||
| 59 | } | ||
| 60 | |||
| 61 | if resp.StatusCode != http.StatusOK { | ||
| 62 | return nil, fmt.Errorf("failed with status %d: %s", resp.StatusCode, body) | ||
| 63 | } | ||
| 64 | |||
| 65 | var responseData map[string]interface{} | ||
| 66 | err = json.Unmarshal(body, &responseData) | ||
| 67 | if err != nil { | ||
| 68 | return nil, fmt.Errorf("failed to unmarshal response: %w", err) | ||
| 69 | } | ||
| 70 | |||
| 71 | return responseData, nil | ||
| 72 | }\ No newline at end of file | ||