From c8daa0d5158935cea0070720427b264637cb2446 Mon Sep 17 00:00:00 2001 From: Pablu23 Date: Wed, 21 Feb 2024 23:10:06 +0100 Subject: [PATCH] Main Menu Dirty --- bato.go | 2 +- go.mod | 5 ++- go.sum | 2 ++ main.go | 59 ++++++++++++++++++------------------ menu.gohtml | 21 +++++++++++++ server.go | 48 +++++++++++++++++++++++++++-- test.gohtml => viewer.gohtml | 0 7 files changed, 103 insertions(+), 34 deletions(-) create mode 100644 menu.gohtml rename test.gohtml => viewer.gohtml (100%) diff --git a/bato.go b/bato.go index 0a182f2..63066cc 100644 --- a/bato.go +++ b/bato.go @@ -25,7 +25,7 @@ func (b *Bato) GetImageList(html string) ([]string, error) { m := reg.FindStringSubmatch(html) if len(m) <= 0 { - return nil, &NoMoreError{Err: errors.New("no more content")} + return nil, errors.New("no more content") } match := m[1] diff --git a/go.mod b/go.mod index 832296c..31abf3c 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module mangaGetter go 1.22 -require github.com/mattn/go-sqlite3 v1.14.22 +require ( + github.com/mattn/go-sqlite3 v1.14.22 + golang.org/x/text v0.14.0 +) diff --git a/go.sum b/go.sum index e8d092a..81788cd 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= diff --git a/main.go b/main.go index 78a6488..cb0b8a6 100644 --- a/main.go +++ b/main.go @@ -9,14 +9,6 @@ import ( "sync" ) -type NoMoreError struct { - Err error -} - -func (e *NoMoreError) Error() string { - return e.Err.Error() -} - type Image struct { Path string Index int @@ -27,6 +19,17 @@ type ImageViewModel struct { Images []Image } +type MangaViewModel struct { + Title string + Number int + LastTime string + Url string +} + +type MenuViewModel struct { + Mangas []MangaViewModel +} + func main() { db := NewDatabase("db.sqlite", true) err := db.Open() @@ -34,22 +37,21 @@ func main() { return } - var latestTimeStamp int64 = 0 - var latestUrl string - for _, m := range db.Mangas { - if latestTimeStamp < m.LatestChapter.TimeStampUnix { - latestTimeStamp = m.LatestChapter.TimeStampUnix - latestUrl = m.LatestChapter.Url - } - } - - if latestUrl == "" { - latestUrl = "/title/80381-i-stan-the-prince/1539086-ch_16" - } + //var latestTimeStamp int64 = 0 + //var latestUrl string + //for _, m := range db.Mangas { + // if latestTimeStamp < m.LatestChapter.TimeStampUnix { + // latestTimeStamp = m.LatestChapter.TimeStampUnix + // latestUrl = m.LatestChapter.Url + // } + //} + // + //if latestUrl == "" { + // latestUrl = "/title/80381-i-stan-the-prince/1539086-ch_16" + //} server := Server{ ImageBuffers: make(map[string]*bytes.Buffer), - CurrSubUrl: latestUrl, NextReady: make(chan bool), PrevReady: make(chan bool), Provider: &Bato{}, @@ -66,18 +68,17 @@ func main() { } }() - server.LoadCurr() - go server.LoadPrev() - go server.LoadNext() + //server.LoadCurr() + //go server.LoadPrev() + //go server.LoadNext() - http.HandleFunc("/", server.HandleCurrent) + http.HandleFunc("/", server.HandleMenu) + http.HandleFunc("/new/title/{title}/{chapter}", server.HandleNew) + http.HandleFunc("/current/", server.HandleCurrent) http.HandleFunc("/img/{url}/", server.HandleImage) http.HandleFunc("POST /next", server.HandleNext) http.HandleFunc("POST /prev", server.HandlePrev) - http.HandleFunc("POST /exit", func(_ http.ResponseWriter, _ *http.Request) { - Close(&db) - }) - http.HandleFunc("/new/{title}/{chapter}", server.HandleNew) + http.HandleFunc("POST /exit", server.HandleExit) fmt.Println("Server starting...") err = http.ListenAndServe(":8000", nil) diff --git a/menu.gohtml b/menu.gohtml new file mode 100644 index 0000000..bb7fe7a --- /dev/null +++ b/menu.gohtml @@ -0,0 +1,21 @@ + + + + + Main Menu + + +
+ +
+ + {{range .Mangas}} +
+ Title: {{.Title}} + Current Chapter: {{.Number}} + Last Accessed: {{.LastTime}} + Go to last Chapter +
+ {{end}} + + \ No newline at end of file diff --git a/server.go b/server.go index 2fb7ae0..56c92d0 100644 --- a/server.go +++ b/server.go @@ -3,6 +3,7 @@ package main import ( "bytes" "fmt" + "golang.org/x/text/language" "html/template" "net/http" "path/filepath" @@ -10,6 +11,8 @@ import ( "strings" "sync" "time" + + "golang.org/x/text/cases" ) type Server struct { @@ -78,7 +81,7 @@ func (s *Server) HandleNext(w http.ResponseWriter, r *http.Request) { go s.LoadNext() - http.Redirect(w, r, "/", http.StatusTemporaryRedirect) + http.Redirect(w, r, "/current/", http.StatusTemporaryRedirect) } func (s *Server) LoadNext() { @@ -181,7 +184,7 @@ func (s *Server) HandlePrev(w http.ResponseWriter, r *http.Request) { go s.LoadPrev() - http.Redirect(w, r, "/", http.StatusTemporaryRedirect) + http.Redirect(w, r, "/current/", http.StatusTemporaryRedirect) } func (s *Server) HandleCurrent(w http.ResponseWriter, _ *http.Request) { @@ -253,7 +256,7 @@ func (s *Server) HandleNew(w http.ResponseWriter, r *http.Request) { go s.LoadNext() go s.LoadPrev() - http.Redirect(w, r, "/", http.StatusTemporaryRedirect) + http.Redirect(w, r, "/current/", http.StatusTemporaryRedirect) } func (s *Server) LoadCurr() { @@ -304,3 +307,42 @@ func (s *Server) AppendImagesToBuf(html string) ([]Image, error) { wg.Wait() return images, nil } + +func (s *Server) HandleMenu(w http.ResponseWriter, r *http.Request) { + tmpl := template.Must(template.ParseFiles("menu.gohtml")) + + all := s.DbMgr.Mangas.All() + l := len(all) + mangaViewModels := make([]MangaViewModel, l) + + for i, manga := range all { + title := cases.Title(language.English, cases.Compact).String(strings.Replace(manga.Title, "-", " ", -1)) + + mangaViewModels[i] = MangaViewModel{ + Title: title, + Number: manga.LatestChapter.Value.Number, + // I Hate this time Format... 15 = hh, 04 = mm, 02 = DD, 01 = MM, 06 == YY + LastTime: time.Unix(manga.TimeStampUnix, 0).Format("15:04 (02-01-06)"), + Url: manga.LatestChapter.Value.Url, + } + } + + menuViewModel := MenuViewModel{ + Mangas: mangaViewModels, + } + + err := tmpl.Execute(w, menuViewModel) + if err != nil { + fmt.Println(err) + } +} + +func (s *Server) HandleExit(w http.ResponseWriter, r *http.Request) { + err := s.DbMgr.Save() + if err != nil { + fmt.Println(err) + return + } + + http.Redirect(w, r, "/", http.StatusTemporaryRedirect) +} diff --git a/test.gohtml b/viewer.gohtml similarity index 100% rename from test.gohtml rename to viewer.gohtml