gamboupload/main.go

100 lines
1.7 KiB
Go
Raw Normal View History

2025-03-10 08:21:59 -07:00
package main
import (
"bytes"
"encoding/json"
2025-03-10 08:54:10 -07:00
"errors"
2025-03-10 08:21:59 -07:00
"fmt"
2025-03-10 08:54:10 -07:00
"github.com/BurntSushi/toml"
"github.com/sqweek/dialog"
2025-03-10 08:21:59 -07:00
"log"
2025-03-10 08:54:10 -07:00
"net/http"
2025-03-10 08:21:59 -07:00
"os"
"strings"
)
2025-03-10 08:54:10 -07:00
type Config struct {
Log string
Apikey string
}
var config Config
2025-03-10 08:21:59 -07:00
func main() {
2025-03-10 08:54:10 -07:00
if _, statErr := os.Stat("config.toml"); os.IsNotExist(statErr) {
path, err := dialog.File().Title("Select your WoWChatLog.txt").Load()
if err != nil {
if errors.Is(err, dialog.ErrCancelled) {
log.Fatalf("Cancelled dialog box, exiting")
}
}
newConfig := &Config{
Log: path,
Apikey: "12345",
}
file, err := os.Create("config.toml")
if err != nil {
log.Fatal(err)
}
defer file.Close()
encoder := toml.NewEncoder(file)
if err := encoder.Encode(newConfig); err != nil {
log.Fatal(err)
}
}
_, err := toml.DecodeFile("config.toml", &config)
b, err := os.ReadFile(config.Log)
2025-03-10 08:21:59 -07:00
if err != nil {
log.Fatal(err)
}
// Strip carriage returns because Windows is retarded
b = bytes.ReplaceAll(b, []byte("\r"), []byte(""))
lines := strings.Split(string(b), "\n")
games, err := parseGames(lines)
if err != nil {
log.Fatal(err)
}
for _, game := range games {
2025-03-10 08:54:10 -07:00
err = upload(game, "https://forcek.in/game")
2025-03-10 08:21:59 -07:00
if err != nil {
log.Fatal(err)
}
}
}
2025-03-10 08:54:10 -07:00
func upload(game Game, endpoint string) error {
marshalled, err := json.Marshal(game)
if err != nil {
return err
}
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(marshalled))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-API-KEY", config.Apikey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
fmt.Println(resp.Status)
return nil
}
func uploadTest(game Game) error {
return upload(game, "http://localhost:3000/game")
}