71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type Forge struct{}
|
|
|
|
type ForgeVersion struct {
|
|
Version string
|
|
Time string
|
|
Url string
|
|
}
|
|
|
|
func parseForgeVersions(html string) []ForgeVersion {
|
|
lines := strings.Split(html, "\n")
|
|
parsing := false
|
|
foundTR := false
|
|
buff := ""
|
|
versions := []ForgeVersion{}
|
|
for _, line := range lines {
|
|
if strings.Contains(line, "<tbody>") {
|
|
parsing = true
|
|
} else if strings.Contains(line, "</tbody>") {
|
|
parsing = false
|
|
} else if parsing {
|
|
if strings.Contains(line, "<tr>") {
|
|
buff = ""
|
|
foundTR = true
|
|
} else if strings.Contains(line, "</tr>") {
|
|
foundTR = false
|
|
versions = append(versions, parseForgeVersion(buff))
|
|
} else if foundTR {
|
|
buff += line + "\n"
|
|
}
|
|
}
|
|
}
|
|
return versions
|
|
}
|
|
|
|
func parseForgeVersion(html string) ForgeVersion {
|
|
lines := strings.Split(html, "\n")
|
|
version := ForgeVersion{}
|
|
for ind, line := range lines {
|
|
if strings.Contains(line, "<td class=\"download-version\">") {
|
|
version.Version = strings.TrimSpace(lines[ind+1])
|
|
} else if strings.Contains(line, "<td class=\"download-time\"") {
|
|
version.Time = strings.Split(strings.Split(line, "<td class=\"download-time\" title=\"")[1], "\">")[0]
|
|
} else if strings.Contains(line, "https://adfoc.us") && strings.Contains(line, "installer.jar") {
|
|
version.Url = strings.Split(strings.Split(line, "&url=")[1], "\">")[0]
|
|
}
|
|
}
|
|
return version
|
|
}
|
|
|
|
func (Forge) GetForgeVersions(mcVersion string) ([]ForgeVersion, error) {
|
|
resp, err := http.Get(fmt.Sprintf("https://files.minecraftforge.net/net/minecraftforge/forge/index_%s.html", mcVersion))
|
|
if err != nil {
|
|
return []ForgeVersion{}, fmt.Errorf("unable to access minecraft forge index: %e", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != 200 {
|
|
return []ForgeVersion{}, fmt.Errorf("unable to access minecraft forge index: %s", err)
|
|
}
|
|
data, _ := io.ReadAll(resp.Body)
|
|
return parseForgeVersions(string(data)), nil
|
|
}
|