🐛 hyaku

This commit is contained in:
源文雨 2022-10-04 16:39:18 +08:00
parent a2ada0b67e
commit 4b288a59b9
2 changed files with 17 additions and 6 deletions

2
data

@ -1 +1 @@
Subproject commit 5ce4b48c12ea42fc999823ea119b69a9d5dfa451 Subproject commit aa8f7eb30a360babcbd7d658d4bebc4643d478df

View File

@ -4,6 +4,7 @@ package hyaku
import ( import (
"encoding/csv" "encoding/csv"
"fmt" "fmt"
"io"
"math/rand" "math/rand"
"os" "os"
"reflect" "reflect"
@ -12,6 +13,7 @@ import (
"github.com/FloatTech/floatbox/binary" "github.com/FloatTech/floatbox/binary"
"github.com/FloatTech/floatbox/file" "github.com/FloatTech/floatbox/file"
"github.com/FloatTech/floatbox/web"
ctrl "github.com/FloatTech/zbpctrl" ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext" "github.com/FloatTech/zbputils/ctxext"
@ -61,17 +63,26 @@ func init() {
}) })
csvfile := engine.DataFolder() + "hyaku.csv" csvfile := engine.DataFolder() + "hyaku.csv"
go func() { go func() {
var f *os.File
if file.IsNotExist(csvfile) { if file.IsNotExist(csvfile) {
err := file.DownloadTo(bed+"小倉百人一首.csv", csvfile, true) data, err := web.RequestDataWith(web.NewTLS12Client(), bed+"小倉百人一首.csv", "GET", "gitcode.net", web.RandUA())
if err != nil { if err != nil {
_ = os.Remove(csvfile) _ = os.Remove(csvfile)
panic(err) panic(err)
} }
} f, err = os.Create(csvfile)
f, err := os.Open(csvfile)
if err != nil { if err != nil {
panic(err) panic(err)
} }
_, _ = f.Write(data)
_, _ = f.Seek(0, io.SeekStart)
} else {
var err error
f, err = os.Open(csvfile)
if err != nil {
panic(err)
}
}
records, err := csv.NewReader(f).ReadAll() records, err := csv.NewReader(f).ReadAll()
if err != nil { if err != nil {
panic(err) panic(err)