add option to only parse last raid or full log
This commit is contained in:
parent
fd18343b84
commit
dd00e06a27
46
gambo.go
46
gambo.go
@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func gambo() {
|
func gambo() {
|
||||||
@ -21,7 +22,29 @@ func gambo() {
|
|||||||
|
|
||||||
lines := strings.Split(string(b), "\n")
|
lines := strings.Split(string(b), "\n")
|
||||||
|
|
||||||
games, err := parseGames(lines)
|
fmt.Println("Parse entire log or just the last raid's entries?")
|
||||||
|
fmt.Println("1) Last raid")
|
||||||
|
fmt.Println("2) Full log")
|
||||||
|
|
||||||
|
var (
|
||||||
|
start int
|
||||||
|
choice int
|
||||||
|
)
|
||||||
|
_, err = fmt.Scanf("%d", &choice)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch choice {
|
||||||
|
case 1:
|
||||||
|
start = findStart(lines)
|
||||||
|
case 2:
|
||||||
|
start = 0
|
||||||
|
default:
|
||||||
|
log.Fatalf("%d is not a valid choice, retard\n", choice)
|
||||||
|
}
|
||||||
|
|
||||||
|
games, err := parseGames(lines, start)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -35,6 +58,27 @@ func gambo() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func findStart(lines []string) int {
|
||||||
|
now := time.Now()
|
||||||
|
t := time.Date(now.Year(), now.Month(), now.Day(), 15, 45, 0, 0, time.UTC)
|
||||||
|
for t.Weekday() != time.Sunday {
|
||||||
|
t = t.AddDate(0, 0, -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// skip ending newline
|
||||||
|
var i = len(lines) - 2
|
||||||
|
for ; i >= 0; i-- {
|
||||||
|
ts, err := timestamp(lines[i])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to rewind log: %v\n", err)
|
||||||
|
}
|
||||||
|
if ts.Before(t) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i + 1
|
||||||
|
}
|
||||||
|
|
||||||
func uploadGambo(game Game, endpoint string) error {
|
func uploadGambo(game Game, endpoint string) error {
|
||||||
marshalled, err := json.Marshal(game)
|
marshalled, err := json.Marshal(game)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
24
parser.go
24
parser.go
@ -34,11 +34,11 @@ var (
|
|||||||
reEnd = regexp.MustCompile(`([\p{L}']+) owes ([\p{L}']+) ([\d,]+) gold!`)
|
reEnd = regexp.MustCompile(`([\p{L}']+) owes ([\p{L}']+) ([\d,]+) gold!`)
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseGames(lines []string) ([]Game, error) {
|
func parseGames(lines []string, start int) ([]Game, error) {
|
||||||
games := make([]Game, 0)
|
games := make([]Game, 0)
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
i := 0
|
i := start
|
||||||
for i < len(lines) {
|
for i < len(lines) {
|
||||||
if reGameStart.MatchString(lines[i]) {
|
if reGameStart.MatchString(lines[i]) {
|
||||||
var game Game
|
var game Game
|
||||||
@ -54,19 +54,25 @@ func parseGames(lines []string) ([]Game, error) {
|
|||||||
return games, nil
|
return games, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func timestamp(line string) (time.Time, error) {
|
||||||
|
ts := reTimeStamp.FindString(line)
|
||||||
|
if ts == "" {
|
||||||
|
return time.Now(), fmt.Errorf("failed to extract timestamp from %s", line)
|
||||||
|
}
|
||||||
|
stamp, err := time.Parse("1/2 15:04:05.000", ts)
|
||||||
|
if err != nil {
|
||||||
|
return time.Now(), fmt.Errorf("failed to extract timestamp from %s", line)
|
||||||
|
}
|
||||||
|
return time.Date(time.Now().Year(), stamp.Month(), stamp.Day(), stamp.Hour(), stamp.Minute(), stamp.Second(), 0, time.UTC), nil
|
||||||
|
}
|
||||||
|
|
||||||
func parse(lines []string, i int) (Game, int, error) {
|
func parse(lines []string, i int) (Game, int, error) {
|
||||||
var (
|
var (
|
||||||
g Game
|
g Game
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
// Timestamp
|
g.Timestamp, err = timestamp(lines[i])
|
||||||
ts := reTimeStamp.FindString(lines[i])
|
|
||||||
if ts == "" {
|
|
||||||
return g, i, fmt.Errorf("failed to extract timestamp from %s", lines[i])
|
|
||||||
}
|
|
||||||
g.Timestamp, err = time.Parse("1/2 15:04:05.000", ts)
|
|
||||||
g.Timestamp = time.Date(time.Now().Year(), g.Timestamp.Month(), g.Timestamp.Day(), g.Timestamp.Hour(), g.Timestamp.Minute(), g.Timestamp.Second(), 0, time.UTC)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return g, i, err
|
return g, i, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user