Main Menu Dirty

This commit is contained in:
Pablu23
2024-02-21 23:10:06 +01:00
parent 8ecbc7e0aa
commit c8daa0d515
7 changed files with 103 additions and 34 deletions

View File

@@ -25,7 +25,7 @@ func (b *Bato) GetImageList(html string) ([]string, error) {
m := reg.FindStringSubmatch(html) m := reg.FindStringSubmatch(html)
if len(m) <= 0 { if len(m) <= 0 {
return nil, &NoMoreError{Err: errors.New("no more content")} return nil, errors.New("no more content")
} }
match := m[1] match := m[1]

5
go.mod
View File

@@ -2,4 +2,7 @@ module mangaGetter
go 1.22 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
)

2
go.sum
View File

@@ -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 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= 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=

59
main.go
View File

@@ -9,14 +9,6 @@ import (
"sync" "sync"
) )
type NoMoreError struct {
Err error
}
func (e *NoMoreError) Error() string {
return e.Err.Error()
}
type Image struct { type Image struct {
Path string Path string
Index int Index int
@@ -27,6 +19,17 @@ type ImageViewModel struct {
Images []Image Images []Image
} }
type MangaViewModel struct {
Title string
Number int
LastTime string
Url string
}
type MenuViewModel struct {
Mangas []MangaViewModel
}
func main() { func main() {
db := NewDatabase("db.sqlite", true) db := NewDatabase("db.sqlite", true)
err := db.Open() err := db.Open()
@@ -34,22 +37,21 @@ func main() {
return return
} }
var latestTimeStamp int64 = 0 //var latestTimeStamp int64 = 0
var latestUrl string //var latestUrl string
for _, m := range db.Mangas { //for _, m := range db.Mangas {
if latestTimeStamp < m.LatestChapter.TimeStampUnix { // if latestTimeStamp < m.LatestChapter.TimeStampUnix {
latestTimeStamp = m.LatestChapter.TimeStampUnix // latestTimeStamp = m.LatestChapter.TimeStampUnix
latestUrl = m.LatestChapter.Url // latestUrl = m.LatestChapter.Url
} // }
} //}
//
if latestUrl == "" { //if latestUrl == "" {
latestUrl = "/title/80381-i-stan-the-prince/1539086-ch_16" // latestUrl = "/title/80381-i-stan-the-prince/1539086-ch_16"
} //}
server := Server{ server := Server{
ImageBuffers: make(map[string]*bytes.Buffer), ImageBuffers: make(map[string]*bytes.Buffer),
CurrSubUrl: latestUrl,
NextReady: make(chan bool), NextReady: make(chan bool),
PrevReady: make(chan bool), PrevReady: make(chan bool),
Provider: &Bato{}, Provider: &Bato{},
@@ -66,18 +68,17 @@ func main() {
} }
}() }()
server.LoadCurr() //server.LoadCurr()
go server.LoadPrev() //go server.LoadPrev()
go server.LoadNext() //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("/img/{url}/", server.HandleImage)
http.HandleFunc("POST /next", server.HandleNext) http.HandleFunc("POST /next", server.HandleNext)
http.HandleFunc("POST /prev", server.HandlePrev) http.HandleFunc("POST /prev", server.HandlePrev)
http.HandleFunc("POST /exit", func(_ http.ResponseWriter, _ *http.Request) { http.HandleFunc("POST /exit", server.HandleExit)
Close(&db)
})
http.HandleFunc("/new/{title}/{chapter}", server.HandleNew)
fmt.Println("Server starting...") fmt.Println("Server starting...")
err = http.ListenAndServe(":8000", nil) err = http.ListenAndServe(":8000", nil)

21
menu.gohtml Normal file
View File

@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Main Menu</title>
</head>
<body>
<form method="post" action="/new/">
</form>
{{range .Mangas}}
<div>
<text>Title: {{.Title}}</text>
<text>Current Chapter: {{.Number}}</text>
<text>Last Accessed: {{.LastTime}}</text>
<a href="/new/{{.Url}}}" class="button">Go to last Chapter</a>
</div>
{{end}}
</body>
</html>

View File

@@ -3,6 +3,7 @@ package main
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"golang.org/x/text/language"
"html/template" "html/template"
"net/http" "net/http"
"path/filepath" "path/filepath"
@@ -10,6 +11,8 @@ import (
"strings" "strings"
"sync" "sync"
"time" "time"
"golang.org/x/text/cases"
) )
type Server struct { type Server struct {
@@ -78,7 +81,7 @@ func (s *Server) HandleNext(w http.ResponseWriter, r *http.Request) {
go s.LoadNext() go s.LoadNext()
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) http.Redirect(w, r, "/current/", http.StatusTemporaryRedirect)
} }
func (s *Server) LoadNext() { func (s *Server) LoadNext() {
@@ -181,7 +184,7 @@ func (s *Server) HandlePrev(w http.ResponseWriter, r *http.Request) {
go s.LoadPrev() 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) { 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.LoadNext()
go s.LoadPrev() go s.LoadPrev()
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) http.Redirect(w, r, "/current/", http.StatusTemporaryRedirect)
} }
func (s *Server) LoadCurr() { func (s *Server) LoadCurr() {
@@ -304,3 +307,42 @@ func (s *Server) AppendImagesToBuf(html string) ([]Image, error) {
wg.Wait() wg.Wait()
return images, nil 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)
}