diff options
Diffstat (limited to 'internal/modrinth/modrinth.go')
| -rw-r--r-- | internal/modrinth/modrinth.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/internal/modrinth/modrinth.go b/internal/modrinth/modrinth.go index 8060f3b..a6ed9eb 100644 --- a/internal/modrinth/modrinth.go +++ b/internal/modrinth/modrinth.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net/http" + "net/url" "os" "time" ) @@ -37,6 +38,43 @@ func NewClient() *Client { return &Client{http: &http.Client{Timeout: 30 * time.Second}} } +type SearchHit struct { + Slug string `json:"slug"` + Title string `json:"title"` + Description string `json:"description"` + Downloads int `json:"downloads"` +} + +func (c *Client) Search(query, mcVersion, loader string) ([]SearchHit, error) { + facets := fmt.Sprintf(`[["project_type:mod"],["categories:%s"],["versions:%s"]]`, loader, mcVersion) + endpoint := fmt.Sprintf("%s/search?limit=20&index=relevance&query=%s&facets=%s", + apiBase, url.QueryEscape(query), url.QueryEscape(facets)) + + req, err := http.NewRequest(http.MethodGet, endpoint, nil) + if err != nil { + return nil, err + } + req.Header.Set("User-Agent", userAgent) + + resp, err := c.http.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("modrinth: search returned %s", resp.Status) + } + + var result struct { + Hits []SearchHit `json:"hits"` + } + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + return nil, err + } + return result.Hits, nil +} + func (c *Client) GetVersions(slug string) ([]Version, error) { url := fmt.Sprintf("%s/project/%s/version", apiBase, slug) @@ -77,6 +115,15 @@ func LatestVersion(versions []Version) *Version { return &versions[best] } +func FindVersion(versions []Version, versionNumber string) *Version { + for i := range versions { + if versions[i].VersionNumber == versionNumber { + return &versions[i] + } + } + return nil +} + func CompatibleVersion(versions []Version, mcVersion, loader string) *Version { var compatible []Version for _, v := range versions { |
