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

@@ -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)