Added saving of LatestAvailableChapter and updating it in goroutine every 5 Minutes, also Added filtering to menu

This commit is contained in:
Pablu23
2024-03-06 23:27:14 +01:00
parent ad1fcbc68a
commit 1377fd420e
7 changed files with 113 additions and 59 deletions

View File

@@ -33,7 +33,7 @@ func (s *Server) HandleNew(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/current/", http.StatusTemporaryRedirect)
}
func (s *Server) HandleMenu(w http.ResponseWriter, _ *http.Request) {
func (s *Server) HandleMenu(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(view.GetViewTemplate(view.Menu))
fmt.Println("Locking Rw in handler.go:43")
@@ -75,25 +75,10 @@ func (s *Server) HandleMenu(w http.ResponseWriter, _ *http.Request) {
// This is very slow
// TODO: put this into own Method
if manga.LastChapterNum == 0 {
l, err := s.Provider.GetChapterList("/title/" + strconv.Itoa(manga.Id))
err := s.UpdateLatestAvailableChapter(manga)
if err != nil {
continue
fmt.Println(err)
}
le := len(l)
_, c, err := s.Provider.GetTitleAndChapter(l[le-1])
if err != nil {
continue
}
chapterNumberStr := strings.Replace(c, "ch_", "", 1)
i, err := strconv.Atoi(chapterNumberStr)
if err != nil {
continue
}
manga.LastChapterNum = i
}
t2 = time.Now().UnixNano()
@@ -121,9 +106,29 @@ func (s *Server) HandleMenu(w http.ResponseWriter, _ *http.Request) {
n = time.Now().UnixNano()
slices.SortStableFunc(mangaViewModels, func(a, b view.MangaViewModel) int {
return cmp.Compare(a.Title, b.Title)
})
sort := r.URL.Query().Get("sort")
if sort == "" || sort == "title" {
slices.SortStableFunc(mangaViewModels, func(a, b view.MangaViewModel) int {
return cmp.Compare(a.Title, b.Title)
})
} else if sort == "chapter" {
slices.SortStableFunc(mangaViewModels, func(a, b view.MangaViewModel) int {
return cmp.Compare(b.Number, a.Number)
})
} else if sort == "last" {
slices.SortStableFunc(mangaViewModels, func(a, b view.MangaViewModel) int {
aT, err := time.Parse("15:04 (02-01-06)", a.LastTime)
if err != nil {
return cmp.Compare(a.Title, b.Title)
}
bT, err := time.Parse("15:04 (02-01-06)", b.LastTime)
if err != nil {
return cmp.Compare(a.Title, b.Title)
}
return bT.Compare(aT)
})
}
nex = time.Now().UnixNano()
fmt.Printf("Sorting took %d ms\n", (nex-n)/1000000)

View File

@@ -13,6 +13,7 @@ import (
"strconv"
"strings"
"sync"
"time"
)
type Server struct {
@@ -46,6 +47,40 @@ func New(provider provider.Provider, db *database.Manager) *Server {
return &s
}
func (s *Server) Start() error {
http.HandleFunc("/", s.HandleMenu)
http.HandleFunc("/new/", s.HandleNewQuery)
http.HandleFunc("/new/title/{title}/{chapter}", s.HandleNew)
http.HandleFunc("/current/", s.HandleCurrent)
http.HandleFunc("/img/{url}/", s.HandleImage)
http.HandleFunc("POST /next", s.HandleNext)
http.HandleFunc("POST /prev", s.HandlePrev)
http.HandleFunc("POST /exit", s.HandleExit)
http.HandleFunc("POST /delete", s.HandleDelete)
http.HandleFunc("/favicon.ico", s.HandleFavicon)
// Update Latest Chapter every 5 Minutes
go func(s *Server) {
for {
select {
case <-time.After(time.Minute * 5):
s.DbMgr.Rw.Lock()
for _, m := range s.DbMgr.Mangas {
err := s.UpdateLatestAvailableChapter(m)
if err != nil {
fmt.Println(err)
}
}
s.DbMgr.Rw.Unlock()
}
}
}(s)
fmt.Println("Server starting...")
err := http.ListenAndServe(":8000", nil)
return err
}
func (s *Server) LoadNext() {
c, err := s.Provider.GetHtml(s.CurrSubUrl)
if err != nil {
@@ -157,6 +192,31 @@ func (s *Server) LoadCurr() {
fmt.Println("Loaded current")
}
func (s *Server) UpdateLatestAvailableChapter(manga *database.Manga) error {
fmt.Printf("Updating Manga: %s\n", manga.Title)
l, err := s.Provider.GetChapterList("/title/" + strconv.Itoa(manga.Id))
if err != nil {
return err
}
le := len(l)
_, c, err := s.Provider.GetTitleAndChapter(l[le-1])
if err != nil {
return err
}
chapterNumberStr := strings.Replace(c, "ch_", "", 1)
i, err := strconv.Atoi(chapterNumberStr)
if err != nil {
return err
}
manga.LastChapterNum = i
return nil
}
func (s *Server) LoadThumbnail(manga *database.Manga) (path string, err error) {
strId := strconv.Itoa(manga.Id)