2021-01-24 18:12:33 -04:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-01-31 07:56:52 -04:00
|
|
|
"fmt"
|
2021-01-24 18:12:33 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2021-01-29 16:28:21 -04:00
|
|
|
"r2go/utils"
|
2021-01-24 18:12:33 -04:00
|
|
|
"time"
|
2021-01-29 16:28:21 -04:00
|
|
|
|
|
|
|
"github.com/lithammer/fuzzysearch/fuzzy"
|
2021-01-24 18:12:33 -04:00
|
|
|
)
|
|
|
|
|
2021-01-29 16:28:21 -04:00
|
|
|
// PkgList contains all mods in current package cache
|
|
|
|
var PkgList []Mod
|
|
|
|
|
2021-01-24 18:12:33 -04:00
|
|
|
// Mod contains info about a mod from thunderstore API
|
|
|
|
type Mod struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
FullName string `json:"full_name"`
|
|
|
|
PackageURL string `json:"package_url"`
|
|
|
|
DateCreated string `json:"date_created"`
|
|
|
|
DateUpdated string `json:"date_updated"`
|
|
|
|
UUID4 string `json:"uuid4"`
|
|
|
|
RatingScore int `json:"rating_score"`
|
|
|
|
IsPinned bool `json:"is_pinned"`
|
|
|
|
IsDeprecated bool `json:"is_deprecated"`
|
|
|
|
HasNSFWContent bool `json:"has_nsfw_content"`
|
|
|
|
Categories []string `json:"categories"`
|
|
|
|
Versions []Version `json:"versions"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version contains info about a specific version of a mod
|
|
|
|
type Version struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
FullName string `json:"full_name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Icon string `json:"icon"`
|
|
|
|
VersionNumber string `json:"version_number"`
|
|
|
|
Dependencies []string `json:"dependencies"`
|
|
|
|
DownloadURL string `json:"download_url"`
|
|
|
|
Downloads int `json:"downloads"`
|
|
|
|
DateCreated string `json:"date_created"`
|
|
|
|
WebsiteURL string `json:"website_url"`
|
|
|
|
IsActive bool `json:"is_active"`
|
|
|
|
UUID4 string `json:"uuid4"`
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:28:21 -04:00
|
|
|
// InitAPI stores the API in an object when needed.
|
|
|
|
func InitAPI() {
|
2021-01-29 16:42:45 -04:00
|
|
|
if CheckAPICache() != 0 {
|
|
|
|
UpdateAPICache()
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:28:21 -04:00
|
|
|
PkgList = UnpackAPI()
|
|
|
|
}
|
|
|
|
|
2021-01-24 18:12:33 -04:00
|
|
|
// CheckAPICache checks if the cached json is expired
|
2021-01-29 13:56:49 -04:00
|
|
|
func CheckAPICache() int {
|
2021-01-29 16:42:45 -04:00
|
|
|
if !utils.PathExists(utils.SystemInfo.ProgDir + "/pkg.json") {
|
2021-01-29 13:56:49 -04:00
|
|
|
return 1
|
|
|
|
}
|
2021-01-24 18:12:33 -04:00
|
|
|
|
2021-01-29 16:42:45 -04:00
|
|
|
info, err := os.Stat(utils.SystemInfo.ProgDir + "/pkg.json")
|
2021-01-29 13:56:49 -04:00
|
|
|
utils.CheckErr(err)
|
2021-01-24 18:12:33 -04:00
|
|
|
|
2021-01-29 13:56:49 -04:00
|
|
|
lastModTime := info.ModTime()
|
|
|
|
currentTime := time.Now()
|
|
|
|
|
|
|
|
if currentTime.Sub(lastModTime).Seconds() > 7200 {
|
|
|
|
return 1
|
2021-01-24 18:12:33 -04:00
|
|
|
}
|
2021-01-29 13:56:49 -04:00
|
|
|
|
|
|
|
return 0
|
2021-01-24 18:12:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAPICache gets a new copy of the thunderstore pkgfile
|
|
|
|
func UpdateAPICache() {
|
|
|
|
apiURL := "https://thunderstore.io/api/v1/package/"
|
|
|
|
|
2021-01-29 16:42:45 -04:00
|
|
|
err := utils.DownloadFile(utils.SystemInfo.ProgDir+"/pkg.json", apiURL)
|
2021-01-24 18:12:33 -04:00
|
|
|
utils.CheckErr(err)
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:28:21 -04:00
|
|
|
// UnpackAPI stores the API cache as an object for later use.
|
|
|
|
func UnpackAPI() []Mod {
|
2021-01-29 16:42:45 -04:00
|
|
|
pkgFile, err := os.Open(utils.SystemInfo.ProgDir + "/pkg.json")
|
2021-01-24 18:12:33 -04:00
|
|
|
utils.CheckErr(err)
|
|
|
|
|
2021-01-29 13:56:49 -04:00
|
|
|
defer pkgFile.Close()
|
|
|
|
|
2021-01-24 18:12:33 -04:00
|
|
|
byteValue, err := ioutil.ReadAll(pkgFile)
|
|
|
|
utils.CheckErr(err)
|
|
|
|
|
|
|
|
var mods []Mod
|
|
|
|
|
|
|
|
err = json.Unmarshal(byteValue, &mods)
|
|
|
|
utils.CheckErr(err)
|
|
|
|
|
2021-01-29 13:56:49 -04:00
|
|
|
return mods
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:28:21 -04:00
|
|
|
// SearchMods finds mods that match a fuzzy search
|
|
|
|
func SearchMods(inp string) fuzzy.Ranks {
|
|
|
|
var pkgNames []string
|
|
|
|
|
|
|
|
for _, mod := range PkgList {
|
|
|
|
pkgNames = append(pkgNames, mod.FullName)
|
|
|
|
}
|
|
|
|
|
|
|
|
results := fuzzy.RankFindFold(inp, pkgNames)
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
2021-01-29 13:56:49 -04:00
|
|
|
|
|
|
|
// GetModData gets object of mod by depString
|
2021-01-30 16:02:49 -04:00
|
|
|
func GetModData(depString string) (int, Mod) {
|
2021-01-29 13:56:49 -04:00
|
|
|
if depString == "R2API" {
|
|
|
|
depString = "tristanmcpherson-R2API"
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(PkgList); i++ {
|
|
|
|
if PkgList[i].FullName == depString {
|
2021-01-30 16:02:49 -04:00
|
|
|
return 0, PkgList[i]
|
2021-01-24 18:12:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 07:56:52 -04:00
|
|
|
fmt.Println("Couldn't get mod data :(")
|
2021-01-30 16:02:49 -04:00
|
|
|
return 1, Mod{}
|
2021-01-24 18:12:33 -04:00
|
|
|
}
|