aboutsummaryrefslogtreecommitdiff
path: root/lib/recommendations.go
blob: 6875a1fbf0896d410d99141420c9b99dfd7eae06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package lib

import (
	"encoding/json"
	"fmt"
	"net/http"
	"yato/config"
)

type Recommendation struct {
	MALId string `json:"mal_id"`
	URL   string `json:"url"`
	Entry []struct {
		MALId  int    `json:"mal_id"`
		URL    string `json:"url"`
		Images struct {
			JPG struct {
				ImageURL      string `json:"image_url"`
				SmallImageURL string `json:"small_image_url"`
				LargeImageURL string `json:"large_image_url"`
			} `json:"jpg"`
			WebP struct {
				ImageURL      string `json:"image_url"`
				SmallImageURL string `json:"small_image_url"`
				LargeImageURL string `json:"large_image_url"`
			} `json:"webp"`
		} `json:"images"`
		Title string `json:"title"`
	} `json:"entry"`
	Content string `json:"content"`
	Date    string `json:"date"`
	User    struct {
		URL      string `json:"url"`
		Username string `json:"username"`
	} `json:"user"`
}

type recommendationsResponse struct {
	Pagination struct {
		LastVisiblePage int  `json:"last_visible_page"`
		HasNextPage     bool `json:"has_next_page"`
	} `json:"pagination"`
	Data []Recommendation `json:"data"`
}

func getRecentRecommendations(mediaType string) ([]Recommendation, error) {
	url := fmt.Sprintf("%s/recommendations/%s", config.JikanAPIBaseURL, mediaType)

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to create request: %w", err)
	}

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return nil, fmt.Errorf("failed to send request: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
	}

	var response recommendationsResponse
	if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
		return nil, fmt.Errorf("failed to decode response: %w", err)
	}

	return response.Data, nil
}

func GetRecentAnimeRecommendations() ([]Recommendation, error) {
	return getRecentRecommendations("anime")
}

func GetRecentMangaRecommendations() ([]Recommendation, error) {
	return getRecentRecommendations("manga")
}