From 8ecbc7e0aa349b9260c3e5798a0130d55460bb6b Mon Sep 17 00:00:00 2001 From: Pablu23 Date: Wed, 21 Feb 2024 19:19:54 +0100 Subject: [PATCH] Split up main.go into multiple files. Added Db support to save last viewed Manga and chapter, and auto load it next time you start the server. Added Providers to add more Manga Platforms than just Bato in the future --- .gitignore | 3 +- bato.go | 83 +++++++++++ createDb.sql | 15 ++ database.go | 165 ++++++++++++++++++++++ go.mod | 2 + go.sum | 2 + main.go | 390 +++++++-------------------------------------------- server.go | 306 ++++++++++++++++++++++++++++++++++++++++ test.gohtml | 4 +- util.go | 64 +++++++++ 10 files changed, 690 insertions(+), 344 deletions(-) create mode 100644 bato.go create mode 100644 createDb.sql create mode 100644 database.go create mode 100644 server.go create mode 100644 util.go diff --git a/.gitignore b/.gitignore index faf8eb0..ccf5f7b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /.idea findings.txt test.txt -h.html \ No newline at end of file +h.html +db.sqlite diff --git a/bato.go b/bato.go new file mode 100644 index 0000000..0a182f2 --- /dev/null +++ b/bato.go @@ -0,0 +1,83 @@ +package main + +import ( + "errors" + "fmt" + "io" + "net/http" + "regexp" +) + +type Provider interface { + GetImageList(html string) (imageUrls []string, err error) + GetHtml(url string) (html string, err error) + GetNext(html string) (url string, err error) + GetPrev(html string) (url string, err error) +} + +type Bato struct{} + +func (b *Bato) GetImageList(html string) ([]string, error) { + reg, err := regexp.Compile(` 300 { + return "", errors.New("could not get html") + } + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + fmt.Printf("Could not close body because: %v\n", err) + } + }(resp.Body) + + all, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + h := string(all) + + return h, nil +} + +func (b *Bato) GetNext(html string) (subUrl string, err error) { + reg, err := regexp.Compile(` 300 { - return "", errors.New("could not get html") - } - defer func(Body io.ReadCloser) { - err := Body.Close() - if err != nil { - fmt.Printf("Could not close body because: %v\n", err) - } - }(resp.Body) - - all, err := io.ReadAll(resp.Body) - if err != nil { - return "", err - } - h := string(all) - - return h, nil -} - -func getNext(html string) (subUrl string, err error) { - reg, err := regexp.Compile(`
+
@@ -95,6 +94,7 @@
+
diff --git a/util.go b/util.go new file mode 100644 index 0000000..165e20e --- /dev/null +++ b/util.go @@ -0,0 +1,64 @@ +package main + +import ( + "bytes" + "errors" + "fmt" + "io" + "net/http" + "regexp" + "strconv" +) + +func getTitleAndChapter(url string) (title string, chapter string, err error) { + reg, err := regexp.Compile(`/title/\d*-(.*?)/\d*-(.*)`) + if err != nil { + return "", "", err + } + + matches := reg.FindAllStringSubmatch(url, -1) + if len(matches) <= 0 { + return "", "", errors.New("no title or chapter found") + } + + return matches[0][1], matches[0][2], nil +} + +func getMangaIdAndChapterId(url string) (titleId int, chapterId int, err error) { + reg, err := regexp.Compile(`/title/(\d*)-.*?/(\d*)-.*`) + if err != nil { + return 0, 0, err + } + + matches := reg.FindAllStringSubmatch(url, -1) + if len(matches) <= 0 { + return 0, 0, errors.New("no title or chapter found") + } + t, err := strconv.Atoi(matches[0][1]) + if err != nil { + return 0, 0, err + } + c, err := strconv.Atoi(matches[0][2]) + + return t, c, err +} + +func addFileToRam(url string) (*bytes.Buffer, error) { + // Get the data + resp, err := http.Get(url) + if err != nil { + return nil, err + } + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + fmt.Println(err) + } + }(resp.Body) + + buf := new(bytes.Buffer) + + // Write the body to file + _, err = io.Copy(buf, resp.Body) + return buf, err +}