internal/jikan.go

106 lines · 2962 bytes

  1package internal
  2
  3import (
  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
 11func 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		Log(fmt.Sprintf("Warning: Jikan API error: %v - continuing without filler data", err))
 18		// Set default values when API fails
 19		anime.Ep.IsFiller = false
 20		anime.Ep.IsRecap = false
 21		return nil // Return nil to allow the application to continue
 22	}
 23
 24	Log(response)
 25
 26	// Check if the 'data' field exists and is valid
 27	data, ok := response["data"].(map[string]interface{})
 28	if !ok {
 29		Log("Warning: Invalid Jikan API response - continuing without filler data")
 30		// Set default values when response is invalid
 31		anime.Ep.IsFiller = false
 32		anime.Ep.IsRecap = false
 33		return nil // Return nil to allow the application to continue
 34	}
 35	// Helper function to safely get string value
 36	getStringValue := func(field string) string {
 37		if value, ok := data[field].(string); ok {
 38			return value
 39		}
 40		return ""
 41	}
 42
 43	// Helper function to safely get int value
 44	getIntValue := func(field string) int {
 45		if value, ok := data[field].(float64); ok {
 46			return int(value)
 47		}
 48		return 0
 49	}
 50
 51	// Helper function to safely get bool value
 52	getBoolValue := func(field string) bool {
 53		if value, ok := data[field].(bool); ok {
 54			return value
 55		}
 56		return false
 57	}
 58
 59	// Safely assign values to the Anime struct
 60	anime.Ep.Title.Romaji = getStringValue("title_romanji")
 61	anime.Ep.Title.English = getStringValue("title")
 62	anime.Ep.Title.Japanese = getStringValue("title_japanese")
 63	anime.Ep.Aired = getStringValue("aired")
 64	anime.Ep.Duration = getIntValue("duration")
 65	anime.Ep.IsFiller = getBoolValue("filler")
 66	anime.Ep.IsRecap = getBoolValue("recap")
 67	anime.Ep.Synopsis = getStringValue("synopsis")
 68
 69	return nil
 70}
 71
 72// Helper function to make GET requests
 73func makeGetRequest(url string, headers map[string]string) (map[string]interface{}, error) {
 74	req, err := http.NewRequest("GET", url, nil)
 75	if err != nil {
 76		return nil, fmt.Errorf("failed to create GET request: %w", err)
 77	}
 78
 79	for key, value := range headers {
 80		req.Header.Set(key, value)
 81	}
 82
 83	client := &http.Client{}
 84	resp, err := client.Do(req)
 85	if err != nil {
 86		return nil, fmt.Errorf("failed to send GET request: %w", err)
 87	}
 88	defer resp.Body.Close()
 89
 90	body, err := io.ReadAll(resp.Body)
 91	if err != nil {
 92		return nil, fmt.Errorf("failed to read response body: %w", err)
 93	}
 94
 95	if resp.StatusCode != http.StatusOK {
 96		return nil, fmt.Errorf("failed with status %d: %s", resp.StatusCode, body)
 97	}
 98
 99	var responseData map[string]interface{}
100	err = json.Unmarshal(body, &responseData)
101	if err != nil {
102		return nil, fmt.Errorf("failed to unmarshal response: %w", err)
103	}
104
105	return responseData, nil
106}