Fixed various Bugs

This commit is contained in:
Pablu23
2024-04-03 16:12:44 +02:00
parent cb592c7109
commit 58344d4def
9 changed files with 62 additions and 57 deletions

4
.gitignore vendored
View File

@@ -2,4 +2,6 @@
findings.txt findings.txt
test.txt test.txt
h.html h.html
db.sqlite *.sqlite
/bin
*.exe

View File

@@ -2,10 +2,10 @@ run: develop
bin/develop bin/develop
develop: develop:
go build -tags Develop -o bin/develop cmd/mangaGetter/main.go go build -tags Develop -o bin/develop ./cmd/mangaGetter/
release: release:
go build -o bin/MangaGetter_unix cmd/mangaGetter/main.go go build -o bin/MangaGetter_unix ./cmd/mangaGetter/
win-amd64: win-amd64:
GOOS=windows GOARCH=amd64 go build -o bin/MangaGetter-amd64_windows.exe cmd/mangaGetter/main.go GOOS=windows GOARCH=amd64 go build -o bin/MangaGetter-amd64_windows.exe ./cmd/mangaGetter/

View File

@@ -9,11 +9,11 @@ type Chapter struct {
MangaId int MangaId int
Url string Url string
Name string Name string
Number int Number string
TimeStampUnix int64 TimeStampUnix int64
} }
func NewChapter(id int, mangaId int, url string, name string, number int, timeStampUnix int64) Chapter { func NewChapter(id int, mangaId int, url string, name string, number string, timeStampUnix int64) Chapter {
return Chapter{ return Chapter{
Id: id, Id: id,
MangaId: mangaId, MangaId: mangaId,
@@ -52,6 +52,6 @@ func loadChapters(db *sql.DB) (map[int]Chapter, error) {
} }
func deleteChapter(db *sql.DB, key int) error { func deleteChapter(db *sql.DB, key int) error {
_, err := db.Exec("DELETE from Manga where ID = ?", key) _, err := db.Exec("DELETE from Chapter where ID = ?", key)
return err return err
} }

View File

@@ -2,8 +2,8 @@ create table if not exists Manga (
ID integer not null primary key, ID integer not null primary key,
Title text, Title text,
TimeStampUnixEpoch integer not null, TimeStampUnixEpoch integer not null,
Thumbnail blob, Thumbnail blob null,
LatestAvailableChapter integer not null LatestAvailableChapter text
); );
create table if not exists Chapter ( create table if not exists Chapter (
@@ -11,7 +11,7 @@ create table if not exists Chapter (
MangaID integer not null, MangaID integer not null,
Url text not null, Url text not null,
Name text null, Name text null,
Number integer not null, Number text null,
TimeStampUnixEpoch integer not null, TimeStampUnixEpoch integer not null,
foreign key(MangaID) references Manga(ID) foreign key(MangaID) references Manga(ID)
); );

View File

@@ -10,7 +10,7 @@ type Manga struct {
Title string Title string
TimeStampUnix int64 TimeStampUnix int64
Thumbnail *bytes.Buffer Thumbnail *bytes.Buffer
LastChapterNum int LastChapterNum string
} }
func NewManga(id int, title string, timeStampUnix int64) Manga { func NewManga(id int, title string, timeStampUnix int64) Manga {
@@ -18,6 +18,7 @@ func NewManga(id int, title string, timeStampUnix int64) Manga {
Id: id, Id: id,
Title: title, Title: title,
TimeStampUnix: timeStampUnix, TimeStampUnix: timeStampUnix,
LastChapterNum: "",
} }
} }
@@ -43,13 +44,25 @@ func (m *Manga) GetLatestChapter(chapters *DbTable[int, Chapter]) (*Chapter, boo
func updateManga(db *sql.DB, m *Manga) error { func updateManga(db *sql.DB, m *Manga) error {
const cmd = "UPDATE Manga set Title = ?, TimeStampUnixEpoch = ?, Thumbnail = ?, LatestAvailableChapter = ? WHERE ID = ?" const cmd = "UPDATE Manga set Title = ?, TimeStampUnixEpoch = ?, Thumbnail = ?, LatestAvailableChapter = ? WHERE ID = ?"
_, err := db.Exec(cmd, m.Title, m.TimeStampUnix, m.Thumbnail.Bytes(), m.LastChapterNum, m.Id) var err error
if m.Thumbnail == nil {
_, err = db.Exec(cmd, m.Title, m.TimeStampUnix, nil, m.LastChapterNum, m.Id)
} else {
_, err = db.Exec(cmd, m.Title, m.TimeStampUnix, m.Thumbnail.Bytes(), m.LastChapterNum, m.Id)
}
return err return err
} }
func insertManga(db *sql.DB, manga *Manga) error { func insertManga(db *sql.DB, manga *Manga) error {
const cmd = "INSERT INTO Manga(ID, Title, TimeStampUnixEpoch, Thumbnail, LatestAvailableChapter) values(?, ?, ?, ?, ?)" const cmd = "INSERT INTO Manga(ID, Title, TimeStampUnixEpoch, Thumbnail, LatestAvailableChapter) values(?, ?, ?, ?, ?)"
_, err := db.Exec(cmd, manga.Id, manga.Title, manga.TimeStampUnix, manga.Thumbnail.Bytes(), manga.LastChapterNum) var err error
if manga.Thumbnail == nil {
_, err = db.Exec(cmd, manga.Id, manga.Title, manga.TimeStampUnix, nil, manga.LastChapterNum)
} else {
_, err = db.Exec(cmd, manga.Id, manga.Title, manga.TimeStampUnix, manga.Thumbnail.Bytes(), manga.LastChapterNum)
}
return err return err
} }
@@ -78,6 +91,6 @@ func loadMangas(db *sql.DB) (map[int]Manga, error) {
} }
func deleteManga(db *sql.DB, key int) error { func deleteManga(db *sql.DB, key int) error {
_, err := db.Exec("DELETE from Chapter where ID = ?", key) _, err := db.Exec("DELETE from Manga where ID = ?", key)
return err return err
} }

View File

@@ -110,7 +110,7 @@ func (b *Bato) GetTitleIdAndChapterId(url string) (titleId int, chapterId int, e
} }
func (b *Bato) GetChapterList(subUrl string) (subUrls []string, err error) { func (b *Bato) GetChapterList(subUrl string) (subUrls []string, err error) {
reg, err := regexp.Compile(`<div class="space-x-1">.*?<a href="(.*?)" .*?>Chapter (\d*)</a>`) reg, err := regexp.Compile(`<div class="space-x-1">.*?<a href="(.*?)" .*?>.*?</a>`)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -68,7 +68,7 @@ func (s *Server) HandleMenu(w http.ResponseWriter, _ *http.Request) {
// This is very slow // This is very slow
// TODO: put this into own Method // TODO: put this into own Method
if manga.LastChapterNum == 0 { if manga.LastChapterNum == "" {
err, updated := s.UpdateLatestAvailableChapter(&manga) err, updated := s.UpdateLatestAvailableChapter(&manga)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@@ -173,6 +173,7 @@ func (s *Server) HandleExit(w http.ResponseWriter, r *http.Request) {
err := s.DbMgr.Save() err := s.DbMgr.Save()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
http.Redirect(w, r, "/curr", http.StatusTemporaryRedirect)
return return
} }
@@ -207,11 +208,13 @@ func (s *Server) HandleCurrent(w http.ResponseWriter, _ *http.Request) {
mangaId, chapterId, err := s.Provider.GetTitleIdAndChapterId(s.CurrSubUrl) mangaId, chapterId, err := s.Provider.GetTitleIdAndChapterId(s.CurrSubUrl)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} else { }
title, chapterName, err := s.Provider.GetTitleAndChapter(s.CurrSubUrl) title, chapterName, err := s.Provider.GetTitleAndChapter(s.CurrSubUrl)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} else { }
manga, ok := s.DbMgr.Mangas.Get(mangaId) manga, ok := s.DbMgr.Mangas.Get(mangaId)
if !ok { if !ok {
manga = database.NewManga(mangaId, title, time.Now().Unix()) manga = database.NewManga(mangaId, title, time.Now().Unix())
@@ -222,21 +225,13 @@ func (s *Server) HandleCurrent(w http.ResponseWriter, _ *http.Request) {
chapter, ok := s.DbMgr.Chapters.Get(chapterId) chapter, ok := s.DbMgr.Chapters.Get(chapterId)
if !ok { if !ok {
chapterNumberStr := strings.Replace(chapterName, "ch_", "", 1) chapterNumberStr := strings.Replace(chapterName, "ch_", "", 1)
number, err := strconv.Atoi(chapterNumberStr) chapter = database.NewChapter(chapterId, manga.Id, s.CurrSubUrl, chapterName, chapterNumberStr, time.Now().Unix())
if err != nil {
fmt.Println(err)
number = 0
}
chapter = database.NewChapter(chapterId, manga.Id, s.CurrSubUrl, chapterName, number, time.Now().Unix())
} else { } else {
chapter.TimeStampUnix = time.Now().Unix() chapter.TimeStampUnix = time.Now().Unix()
} }
s.DbMgr.Chapters.Set(chapterId, chapter) s.DbMgr.Chapters.Set(chapterId, chapter)
s.DbMgr.Mangas.Set(mangaId, manga) s.DbMgr.Mangas.Set(mangaId, manga)
}
}
err = tmpl.Execute(w, s.CurrViewModel) err = tmpl.Execute(w, s.CurrViewModel)
if err != nil { if err != nil {

View File

@@ -223,15 +223,10 @@ func (s *Server) UpdateLatestAvailableChapter(manga *database.Manga) (error, boo
chapterNumberStr := strings.Replace(c, "ch_", "", 1) chapterNumberStr := strings.Replace(c, "ch_", "", 1)
i, err := strconv.Atoi(chapterNumberStr) if manga.LastChapterNum == chapterNumberStr {
if err != nil {
return err, false
}
if manga.LastChapterNum == i {
return nil, false return nil, false
} else { } else {
manga.LastChapterNum = i manga.LastChapterNum = chapterNumberStr
return nil, true return nil, true
} }
} }

View File

@@ -15,8 +15,8 @@ type ImageViewModel struct {
type MangaViewModel struct { type MangaViewModel struct {
ID int ID int
Title string Title string
Number int Number string
LastNumber int LastNumber string
LastTime string LastTime string
Url string Url string
ThumbnailUrl string ThumbnailUrl string