100 lines
1.7 KiB
Go
100 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/BurntSushi/toml"
|
|
"github.com/sqweek/dialog"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
type Config struct {
|
|
Log string
|
|
Apikey string
|
|
}
|
|
|
|
var config Config
|
|
|
|
func main() {
|
|
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)
|
|
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 {
|
|
err = upload(game, "https://forcek.in/game")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|