Files
BedrockFetch/bds/fetch.go
Matheus Sampaio Queiroga 0f2865d993 Migrate to golang
Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
2024-04-04 00:23:10 -03:00

64 lines
1.6 KiB
Go

package bds
import (
"regexp"
"sirherobrine23.org/minecraft-server/bedrockfetch/internal/request"
)
var fileMatch = regexp.MustCompile(`(?m)bin-(?P<Platform>win|linux)-?(?P<isPreview>preview)?\/bedrock-server-(?P<Version>[0-9\.]+)\.zip$`)
// FindAllGroups returns a map with each match group. The map key corresponds to the match group name.
// A nil return value indicates no matches.
func FindAllGroups(re *regexp.Regexp, s string) map[string]string {
matches := re.FindStringSubmatch(s)
subnames := re.SubexpNames()
if matches == nil || len(matches) != len(subnames) {
return nil
}
matchMap := map[string]string{}
for i := 1; i < len(matches); i++ {
matchMap[subnames[i]] = matches[i]
}
return matchMap
}
type FileInfo struct {
FileUrl string // Zip file url
Platform string // Target
IsPreview bool // Is preview server release
}
func MinecraftFetch() (map[string][]FileInfo, error) {
links, _, _, err := request.RequestHtmlLinks(request.RequestOptions{
Url: "https://www.minecraft.net/en-us/download/server/bedrock",
HttpError: true,
})
if err != nil {
return nil, err
}
// Filter files
zipFiles := map[string][]FileInfo{}
for _, file := range links {
if fileMatch.MatchString(file) {
info := FindAllGroups(fileMatch, file)
if _, existVer := zipFiles[info["Version"]]; !existVer {
zipFiles[info["Version"]] = []FileInfo{}
}
zipFiles[info["Version"]] = append(zipFiles[info["Version"]], FileInfo{
FileUrl: file,
Platform: info["Platform"],
IsPreview: info["isPreview"] != "",
})
}
}
return zipFiles, nil
}