From 9183914420faf2c179a6193f35095c3d83a371f9 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Wed, 14 Oct 2020 14:46:20 -0500 Subject: [PATCH] make website display actual beatmap names --- go.mod | 1 + go.sum | 7 +++++ main.go | 2 +- web/templates/index.html | 4 +-- web/web.go | 55 +++++++++++++++++++++++----------------- 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 16d6804..72d891c 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/gin-gonic/gin v1.6.3 github.com/go-git/go-git/v5 v5.2.0 github.com/gorilla/sessions v1.2.1 // indirect + github.com/kofalt/go-memoize v0.0.0-20200917044458-9b55a8d73e1c go.etcd.io/bbolt v1.3.5 golang.org/x/sync v0.0.0-20201008141435-b3e1573b7520 ) diff --git a/go.sum b/go.sum index 235f9e5..19cd531 100644 --- a/go.sum +++ b/go.sum @@ -78,6 +78,8 @@ github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v2wtGp9Gmz1Ze3eVRAWJMLokvN3QjdzCHLY= github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= +github.com/kofalt/go-memoize v0.0.0-20200917044458-9b55a8d73e1c h1:iQm590VHHIeY1j9OFi2qBPgIeDqxz+PmHdDK/fUcN7s= +github.com/kofalt/go-memoize v0.0.0-20200917044458-9b55a8d73e1c/go.mod h1:IvB2BCBCdgZFN9ZSgInoUlL1sAu0Xbvqfd7D+qqzTeo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= @@ -102,10 +104,14 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E= +github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= +github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= +github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= +github.com/smartystreets/gunit v1.4.2/go.mod h1:ZjM1ozSIMJlAz/ay4SG8PeKF00ckUp+zMHZXV9/bvak= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= @@ -134,6 +140,7 @@ golang.org/x/net v0.0.0-20190607181551-461777fb6f67/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0= golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201008141435-b3e1573b7520 h1:Bx6FllMpG4NWDOfhMBz1VR2QYNp/SAOHPIAsaVmxfPo= golang.org/x/sync v0.0.0-20201008141435-b3e1573b7520/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/main.go b/main.go index a003edc..095a392 100644 --- a/main.go +++ b/main.go @@ -40,7 +40,7 @@ func main() { } go scrape.RunScraper(&config, bot, db, api) - go web.RunWeb(&config) + go web.RunWeb(&config, api) signal_chan := make(chan os.Signal, 1) signal.Notify(signal_chan, diff --git a/web/templates/index.html b/web/templates/index.html index a8dc65c..ea5f1d9 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -15,8 +15,8 @@

Maps:

diff --git a/web/web.go b/web/web.go index 5eae125..6e8c5ad 100644 --- a/web/web.go +++ b/web/web.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "path" + "strconv" "strings" "time" @@ -16,15 +17,21 @@ import ( "github.com/gin-contrib/static" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" + "github.com/kofalt/go-memoize" "subscribe-bot/config" + "subscribe-bot/osuapi" ) const ( USER_KEY = "user" ) -func RunWeb(config *config.Config) { +var ( + cache = memoize.NewMemoizer(90*time.Second, 10*time.Minute) +) + +func RunWeb(config *config.Config, api *osuapi.Osuapi) { hc := http.Client{ Timeout: 10 * time.Second, } @@ -101,12 +108,12 @@ func RunWeb(config *config.Config) { } } - repos, _ := getRepos(config) + beatmapSets := getRepos(config, api) // render with master c.HTML(http.StatusOK, "index.html", gin.H{ - "LoggedIn": loggedIn, - "Repos": repos, + "LoggedIn": loggedIn, + "Beatmapsets": beatmapSets, }) }) @@ -114,28 +121,30 @@ func RunWeb(config *config.Config) { r.Run(addr) } -func getRepos(config *config.Config) (repos []string, err error) { - repos = make([]string, 0) - reposDir := config.Repos - users, err := ioutil.ReadDir(reposDir) - if err != nil { - return - } +func getRepos(config *config.Config, api *osuapi.Osuapi) []osuapi.Beatmapset { + expensive := func() (interface{}, error) { + repos := make([]osuapi.Beatmapset, 0) + reposDir := config.Repos + users, _ := ioutil.ReadDir(reposDir) - for _, user := range users { - userDir := path.Join(reposDir, user.Name()) - var maps []os.FileInfo - maps, err = ioutil.ReadDir(userDir) - if err != nil { - return + for _, user := range users { + userDir := path.Join(reposDir, user.Name()) + var maps []os.FileInfo + maps, _ = ioutil.ReadDir(userDir) + + for _, mapId := range maps { + mapDir := path.Join(userDir, mapId.Name()) + fmt.Println(mapDir) + + id, _ := strconv.Atoi(mapId.Name()) + bs, _ := api.GetBeatmapSet(id) + repos = append(repos, bs) + } } - for _, mapId := range maps { - mapDir := path.Join(userDir, mapId.Name()) - fmt.Println(mapDir) - repos = append(repos, user.Name()+"/"+mapId.Name()) - } + return repos, nil } - return + result, _, _ := cache.Memoize("key1", expensive) + return result.([]osuapi.Beatmapset) }