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 +}