From da474ecedef81f035d9655bf112b9a9deeef671c Mon Sep 17 00:00:00 2001 From: daru Date: Wed, 29 Jun 2022 22:40:46 +0200 Subject: [PATCH] Charts V1 --- go.mod | 2 +- go.sum | 4 +- klotz.go | 7 +++ ober.go | 11 +++- praktikant.go | 21 +++++++ schaffer.go | 38 ++++++++++++ season.qtpl | 37 +++++++++-- season.qtpl.go | 164 +++++++++++++++++++++++++++++-------------------- zecke.go | 26 ++++++++ 9 files changed, 234 insertions(+), 76 deletions(-) diff --git a/go.mod b/go.mod index e0a9048..1411059 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( require ( github.com/andybalholm/brotli v1.0.4 // indirect github.com/bwmarrin/snowflake v0.3.0 // indirect - github.com/klauspost/compress v1.15.6 // indirect + github.com/klauspost/compress v1.15.7 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect diff --git a/go.sum b/go.sum index 4f14043..d222c3b 100644 --- a/go.sum +++ b/go.sum @@ -20,8 +20,8 @@ github.com/gookit/color v1.5.1/go.mod h1:wZFzea4X8qN6vHOSP2apMb4/+w/orMznEzYsIHP github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= github.com/klauspost/compress v1.13.5/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= -github.com/klauspost/compress v1.15.6 h1:6D9PcO8QWu0JyaQ2zUMmu16T1T+zjjEpP91guRsvDfY= -github.com/klauspost/compress v1.15.6/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= +github.com/klauspost/compress v1.15.7 h1:7cgTQxJCU/vy+oP/E3B9RGbQTgbiVzIJWIKOLoAsPok= +github.com/klauspost/compress v1.15.7/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/klotz.go b/klotz.go index 272dcb6..c726630 100644 --- a/klotz.go +++ b/klotz.go @@ -317,3 +317,10 @@ type MmOracle struct { Anime int64 `json:"anime"` AvgScore int `json:"avgScore"` } + +type MovieChart struct { + MmId int `json:"mmId"` + AvgScore float64 `json:"avgScore"` + UserCount int `json:"userCount"` + Data Anime `json:"data"` +} diff --git a/ober.go b/ober.go index 39679d9..d79134a 100644 --- a/ober.go +++ b/ober.go @@ -9,6 +9,7 @@ import ( "time" "github.com/fasthttp/router" + "github.com/gookit/color" "github.com/valyala/fasthttp" "github.com/xujiajun/nutsdb" ) @@ -46,11 +47,15 @@ func Start(ctx *fasthttp.RequestCtx) { oracles, err := MmReadOracle() if err != nil { - addErrorToCtx(ctx, err) - return + color.Errorln(err.Error()) } - WriteIndex(ctx, season, oracles, logOut.String()) + charts, err := BuildMovieCharts() + if err != nil { + color.Errorln(err.Error()) + } + + WriteIndex(ctx, season, oracles, charts, logOut.String()) ctx.SetContentType("text/html; charset=utf-8") ctx.SetStatusCode(fasthttp.StatusOK) diff --git a/praktikant.go b/praktikant.go index b473bf9..fedcca4 100644 --- a/praktikant.go +++ b/praktikant.go @@ -194,6 +194,27 @@ func LangeArbeit() { } } + charts, err := BuildMovieCharts() + if err != nil { + color.Errorln(err.Error()) + logOut.WriteError(err) + } else { + for _, c := range charts { + // search season first + _, err = SearchSeason(c.Data.Anime) + if err == nil { + continue + } + err = refreshAnime(c.Data.Anime) + if err != nil { + color.Errorln(err.Error()) + logOut.WriteError(err) + continue + } + count++ + } + } + color.Infof("%d Anime aktualisiert\n", count) logOut.WriteLine(fmt.Sprintf("🔃 %d Anime aktualisiert", count)) } diff --git a/schaffer.go b/schaffer.go index 2e39550..a451146 100644 --- a/schaffer.go +++ b/schaffer.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "github.com/gookit/color" "github.com/xujiajun/nutsdb" ) @@ -270,3 +271,40 @@ func CheckAnimeExistInDbAndUserWatches(animeId, userId int64) (bool, error) { } return false, err } + +func BuildMovieCharts() ([]MovieChart, error) { + key := "charts" + var charts []MovieChart + data, err := mmCache.Get(key) + if err == nil { + err = json.Unmarshal(data, &charts) + if err == nil { + return charts, err + } + } + + movieList, err := MmReadCharts() + if err != nil { + return nil, err + } + charts = make([]MovieChart, 0) + for _, m := range movieList { + c := MovieChart{ + MmId: m.Id, + } + anime, err := SearchAnime(m.Anime) + if err != nil { + color.Errorln(err.Error()) + } else { + c.Data = *anime + charts = append(charts, c) + } + } + + bytes, err := json.Marshal(charts) + if err == nil { + mmCache.Set(key, bytes) + } + + return charts, nil +} diff --git a/season.qtpl b/season.qtpl index e649b8b..ceefbec 100644 --- a/season.qtpl +++ b/season.qtpl @@ -1,5 +1,5 @@ {% package main %} -{% func Index(animes []Anime, oracles []MmOracle, log string) %} +{% func Index(animes []Anime, oracles []MmOracle, charts []MovieChart, log string) %} {% collapsespace %} @@ -30,23 +30,52 @@ body { background-color: #1a1a1a; color: #fff; }
+ + + + + {% for _, anime := range animes %} - - + + + {% endfor %} +
MALIDSCORETITLE
{%dl anime.Anime %}{%s anime.Title %}{%d anime.Episodes %} {%f anime.Score %}{%s anime.Title %}
+

MovieManager Charts

+ + + + + + + + + {% for _, chart := range charts %} + + + + + + {% endfor %}
MMIDMALIDUSERSCORETITLE
{%d chart.MmId %}{%dl chart.Data.Anime %}{%d chart.UserCount %}{%f chart.AvgScore %}{%s chart.Data.Title %}

MovieManager Oracle

+ + + + + + {% for _, oracle := range oracles %} + - {% endfor %}
MMIDMALIDSCORETITLE
{%d oracle.Id %}{%dl oracle.Anime %} {%d oracle.AvgScore %} {%s oracle.Title %}{%dl oracle.Anime %}
diff --git a/season.qtpl.go b/season.qtpl.go index 50600b4..c3bd21d 100644 --- a/season.qtpl.go +++ b/season.qtpl.go @@ -18,7 +18,7 @@ var ( ) //line season.qtpl:2 -func StreamIndex(qw422016 *qt422016.Writer, animes []Anime, oracles []MmOracle, log string) { +func StreamIndex(qw422016 *qt422016.Writer, animes []Anime, oracles []MmOracle, charts []MovieChart, log string) { //line season.qtpl:2 qw422016.N().S(` `) @@ -31,91 +31,123 @@ func StreamIndex(qw422016 *qt422016.Writer, animes []Anime, oracles []MmOracle, //line season.qtpl:28 qw422016.N().D(len(animes)) //line season.qtpl:28 - qw422016.N().S(`
`) -//line season.qtpl:33 + qw422016.N().S(`

`) +//line season.qtpl:38 for _, anime := range animes { -//line season.qtpl:33 +//line season.qtpl:38 qw422016.N().S(` `) //line season.qtpl:40 - } -//line season.qtpl:40 - qw422016.N().S(`
MALID SCORE TITLE
`) -//line season.qtpl:35 +//line season.qtpl:40 qw422016.N().DL(anime.Anime) -//line season.qtpl:35 - qw422016.N().S(` `) -//line season.qtpl:36 - qw422016.E().S(anime.Title) -//line season.qtpl:36 - qw422016.N().S(` `) -//line season.qtpl:37 - qw422016.N().D(anime.Episodes) -//line season.qtpl:37 - qw422016.N().S(` `) -//line season.qtpl:38 - qw422016.N().F(anime.Score) -//line season.qtpl:38 - qw422016.N().S(`

MovieManager Oracle

`) -//line season.qtpl:44 - for _, oracle := range oracles { -//line season.qtpl:44 - qw422016.N().S(` `) -//line season.qtpl:51 +//line season.qtpl:42 + qw422016.E().S(anime.Title) +//line season.qtpl:42 + qw422016.N().S(` `) +//line season.qtpl:44 } -//line season.qtpl:51 - qw422016.N().S(`
`) -//line season.qtpl:46 - qw422016.N().D(oracle.Id) -//line season.qtpl:46 qw422016.N().S(` `) -//line season.qtpl:47 - qw422016.N().D(oracle.AvgScore) -//line season.qtpl:47 +//line season.qtpl:41 + qw422016.N().F(anime.Score) +//line season.qtpl:41 qw422016.N().S(` `) -//line season.qtpl:48 - qw422016.E().S(oracle.Title) -//line season.qtpl:48 - qw422016.N().S(` `) -//line season.qtpl:49 - qw422016.N().DL(oracle.Anime) -//line season.qtpl:49 - qw422016.N().S(`
`) +//line season.qtpl:44 + qw422016.N().S(`

MovieManager Charts

`) //line season.qtpl:55 + for _, chart := range charts { +//line season.qtpl:55 + qw422016.N().S(` `) +//line season.qtpl:63 + } +//line season.qtpl:63 + qw422016.N().S(`
MMID MALID USER SCORE TITLE
`) +//line season.qtpl:57 + qw422016.N().D(chart.MmId) +//line season.qtpl:57 + qw422016.N().S(` `) +//line season.qtpl:58 + qw422016.N().DL(chart.Data.Anime) +//line season.qtpl:58 + qw422016.N().S(` `) +//line season.qtpl:59 + qw422016.N().D(chart.UserCount) +//line season.qtpl:59 + qw422016.N().S(` `) +//line season.qtpl:60 + qw422016.N().F(chart.AvgScore) +//line season.qtpl:60 + qw422016.N().S(` `) +//line season.qtpl:61 + qw422016.E().S(chart.Data.Title) +//line season.qtpl:61 + qw422016.N().S(`

MovieManager Oracle

`) +//line season.qtpl:73 + for _, oracle := range oracles { +//line season.qtpl:73 + qw422016.N().S(` `) +//line season.qtpl:80 + } +//line season.qtpl:80 + qw422016.N().S(`
MMID MALID SCORE TITLE
`) +//line season.qtpl:75 + qw422016.N().D(oracle.Id) +//line season.qtpl:75 + qw422016.N().S(` `) +//line season.qtpl:76 + qw422016.N().DL(oracle.Anime) +//line season.qtpl:76 + qw422016.N().S(` `) +//line season.qtpl:77 + qw422016.N().D(oracle.AvgScore) +//line season.qtpl:77 + qw422016.N().S(` `) +//line season.qtpl:78 + qw422016.E().S(oracle.Title) +//line season.qtpl:78 + qw422016.N().S(`
`) +//line season.qtpl:84 qw422016.N().S(` `) -//line season.qtpl:56 +//line season.qtpl:85 } -//line season.qtpl:56 -func WriteIndex(qq422016 qtio422016.Writer, animes []Anime, oracles []MmOracle, log string) { -//line season.qtpl:56 +//line season.qtpl:85 +func WriteIndex(qq422016 qtio422016.Writer, animes []Anime, oracles []MmOracle, charts []MovieChart, log string) { +//line season.qtpl:85 qw422016 := qt422016.AcquireWriter(qq422016) -//line season.qtpl:56 - StreamIndex(qw422016, animes, oracles, log) -//line season.qtpl:56 +//line season.qtpl:85 + StreamIndex(qw422016, animes, oracles, charts, log) +//line season.qtpl:85 qt422016.ReleaseWriter(qw422016) -//line season.qtpl:56 +//line season.qtpl:85 } -//line season.qtpl:56 -func Index(animes []Anime, oracles []MmOracle, log string) string { -//line season.qtpl:56 +//line season.qtpl:85 +func Index(animes []Anime, oracles []MmOracle, charts []MovieChart, log string) string { +//line season.qtpl:85 qb422016 := qt422016.AcquireByteBuffer() -//line season.qtpl:56 - WriteIndex(qb422016, animes, oracles, log) -//line season.qtpl:56 +//line season.qtpl:85 + WriteIndex(qb422016, animes, oracles, charts, log) +//line season.qtpl:85 qs422016 := string(qb422016.B) -//line season.qtpl:56 +//line season.qtpl:85 qt422016.ReleaseByteBuffer(qb422016) -//line season.qtpl:56 +//line season.qtpl:85 return qs422016 -//line season.qtpl:56 +//line season.qtpl:85 } diff --git a/zecke.go b/zecke.go index 746cf3c..7f8af20 100644 --- a/zecke.go +++ b/zecke.go @@ -85,3 +85,29 @@ func MmReadOracle() ([]MmOracle, error) { return oracles, err } + +func MmReadCharts() ([]MmOracle, error) { + chartsQuery := `SELECT m.id, m.title, m.mal_id FROM movie m + JOIN evening_movie em ON em.movie_id = m.id + JOIN evening e ON em.evening_id = e.id + WHERE e.date <= CURDATE() AND m.mal_id IS NOT NULL;` + rows, err := mmDb.Query(chartsQuery) + if err != nil { + return nil, err + } + defer rows.Close() + + charts := make([]MmOracle, 0) + for rows.Next() { + var chart MmOracle + err = rows.Scan(&chart.Id, &chart.Title, &chart.Anime) + if err != nil { + return charts, err + } + charts = append(charts, chart) + } + + err = rows.Err() + + return charts, err +}