forked from metafates/mangal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
167 lines (138 loc) · 3.7 KB
/
search.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package anilist
import (
"bytes"
"encoding/json"
"fmt"
"github.com/metafates/mangal/log"
"github.com/metafates/mangal/network"
"github.com/metafates/mangal/query"
"github.com/samber/lo"
"net/http"
"strconv"
)
type searchByNameResponse struct {
Data struct {
Page struct {
Media []*Manga `json:"media"`
} `json:"page"`
} `json:"data"`
}
type searchByIDResponse struct {
Data struct {
Media *Manga `json:"media"`
} `json:"data"`
}
// GetByID returns the manga with the given id.
// If the manga is not found, it returns nil.
func GetByID(id int) (*Manga, error) {
if manga := idCacher.Get(id); manga.IsPresent() {
return manga.MustGet(), nil
}
// prepare body
log.Infof("Searching anilist for manga with id: %d", id)
body := map[string]interface{}{
"query": searchByIDQuery,
"variables": map[string]interface{}{
"id": id,
},
}
// parse body to json
jsonBody, err := json.Marshal(body)
if err != nil {
log.Error(err)
return nil, err
}
// send request
log.Info("Sending request to Anilist")
req, err := http.NewRequest(http.MethodPost, "https://graphql.anilist.co", bytes.NewBuffer(jsonBody))
if err != nil {
log.Error(err)
return nil, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := network.Client.Do(req)
if err != nil {
log.Error(err)
return nil, err
}
if resp.StatusCode != http.StatusOK {
log.Error("Anilist returned status code " + strconv.Itoa(resp.StatusCode))
return nil, fmt.Errorf("invalid response code %d", resp.StatusCode)
}
// decode response
var response searchByIDResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
log.Error(err)
return nil, err
}
manga := response.Data.Media
log.Infof("Got response from Anilist, found manga with id %d", manga.ID)
_ = idCacher.Set(id, manga)
return manga, nil
}
// SearchByName returns a list of mangas that match the given name.
func SearchByName(name string) ([]*Manga, error) {
name = normalizedName(name)
_ = query.Remember(name, 1)
if _, failed := failCacher.Get(name).Get(); failed {
return nil, fmt.Errorf("failed to search for %s", name)
}
if ids, ok := searchCacher.Get(name).Get(); ok {
mangas := lo.FilterMap(ids, func(item, _ int) (*Manga, bool) {
return idCacher.Get(item).Get()
})
if len(mangas) == 0 {
_ = searchCacher.Delete(name)
return SearchByName(name)
}
return mangas, nil
}
// prepare body
log.Infof("Searching anilist for manga %s", name)
body := map[string]any{
"query": searchByNameQuery,
"variables": map[string]any{
"query": name,
},
}
// parse body to json
jsonBody, err := json.Marshal(body)
if err != nil {
log.Error(err)
return nil, err
}
// send request
log.Info("Sending request to Anilist")
req, err := http.NewRequest(http.MethodPost, "https://graphql.anilist.co", bytes.NewBuffer(jsonBody))
if err != nil {
log.Error(err)
return nil, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := network.Client.Do(req)
if err != nil {
log.Error(err)
_ = failCacher.Set(name, true)
return nil, err
}
if resp.StatusCode != http.StatusOK {
log.Error("Anilist returned status code " + strconv.Itoa(resp.StatusCode))
_ = failCacher.Set(name, true)
return nil, fmt.Errorf("invalid response code %d", resp.StatusCode)
}
// decode response
var response searchByNameResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
log.Error(err)
return nil, err
}
mangas := response.Data.Page.Media
log.Infof("Got response from Anilist, found %d results", len(mangas))
ids := make([]int, len(mangas))
for i, manga := range mangas {
ids[i] = manga.ID
_ = idCacher.Set(manga.ID, manga)
}
_ = searchCacher.Set(name, ids)
return mangas, nil
}