From 07152f8f02095d92dff3efc1e4c520553e54df36 Mon Sep 17 00:00:00 2001 From: Felipe M Date: Sat, 6 Feb 2021 23:50:43 +0100 Subject: [PATCH] CLI Proof of Concept --- README.md | 3 +- go.mod | 8 +++ go.sum | 12 +++++ main.go | 10 ++++ pkg/cli/cli.go | 136 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 pkg/cli/cli.go diff --git a/README.md b/README.md index dbbe376..a04952d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ -# mangadex2cbr +# MangaDex to CBR downloader +Generate CBR comics from MangaDex releases diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..11f1804 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module code.fmartingr.dev/fmartingr/mangadex2cbr + +go 1.15 + +require ( + code.fmartingr.dev/fmartingr/go-mangadex v0.0.0-20210206105358-8cfcb7a013b8 + github.com/sirupsen/logrus v1.7.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..03e6074 --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +code.fmartingr.dev/fmartingr/go-mangadex v0.0.0-20210206105358-8cfcb7a013b8 h1:JS8C40VcGUO1WdaAQfofvu4ROzDSJ1YCG0ycswOPfyY= +code.fmartingr.dev/fmartingr/go-mangadex v0.0.0-20210206105358-8cfcb7a013b8/go.mod h1:C4HCcFpl4VDk4O5Huau2cdTAOuwqIP9NBXItWhaMZVo= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= +github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/main.go b/main.go new file mode 100644 index 0000000..1f88de8 --- /dev/null +++ b/main.go @@ -0,0 +1,10 @@ +package main + +import ( + "code.fmartingr.dev/fmartingr/mangadex2cbr/pkg/cli" +) + +func main() { + //logrus.SetFormatter(&logrus.JSONFormatter{}) + cli.Start() +} diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go new file mode 100644 index 0000000..3bb0c35 --- /dev/null +++ b/pkg/cli/cli.go @@ -0,0 +1,136 @@ +package cli + +import ( + "flag" + "fmt" + "os" + "path/filepath" + "sort" + "strings" + + "code.fmartingr.dev/fmartingr/go-mangadex" + "github.com/sirupsen/logrus" +) + +type CliOPtions struct { + MangaID string +} + +const defaultLogLevel string = "INFO" + +func Start() { + logLevelFlag := flag.String("log-level", defaultLogLevel, "Log level") + mangaIDFlag := flag.Int("manga-id", 0, "Manga ID to convert") + noCacheFlag := flag.Bool("no-cache", false, "Cache requests to mangadex") + outputPath := "Output" + flag.Parse() + + cwd, errCwd := os.Getwd() + if errCwd != nil { + logrus.Fatalf("Error retrieving current working directory: %s", errCwd) + } + outputPath = filepath.Join(cwd, outputPath) + + if !*noCacheFlag { + mangadex.EnableCache() + } + + logLevel, errLogLevel := logrus.ParseLevel(*logLevelFlag) + if errLogLevel != nil { + logrus.Warnf("Incorrect loglevel %s, using default %s", *logLevelFlag, defaultLogLevel) + } else { + logrus.SetLevel(logLevel) + } + + if *mangaIDFlag == 0 { + logrus.Error("You should set -manga-id") + logrus.Exit(1) + } + + logrus.Infof("Getting information for Manga ID: %d", *mangaIDFlag) + manga, err := mangadex.GetManga(*mangaIDFlag) + if err != nil { + panic(err) + } + + logrus.Infof("Found! %s", manga.Title) + logrus.Infof("Getting chapter information...") + + var mangaChapters []mangadex.MangaChapter + mangaGroups := map[int]mangadex.MangaGroup{} + chapterParams := mangadex.NewGetChaptersParams() + currentPage := 1 + + // TODO: Select language + selectedLanguage := "gb" + + for currentPage != chapterParams.Page { + chapterParams.Page = currentPage + logrus.Infof("Downloading chapters page %d", currentPage) + chapters, groups, errChapters := manga.GetChapters(chapterParams) + if errChapters != nil { + logrus.Errorf("Error retrieving manga chapters: %s", errChapters) + } + for chapter := range chapters { + if chapters[chapter].Language == selectedLanguage { + mangaChapters = append(mangaChapters, chapters[chapter]) + } + } + + for group := range groups { + _, exists := mangaGroups[groups[group].ID] + if !exists { + mangaGroups[groups[group].ID] = groups[group] + } + } + + // If we have the total number of items we try the next page + if len(chapters) == chapterParams.Limit { + currentPage++ + } + } + + logrus.Printf("Found following groups: ") + for group := range mangaGroups { + logrus.Printf(" %6d: %s", mangaGroups[group].ID, mangaGroups[group].Name) + } + + // TODO: Select groups for digitalization + // TODO: Using all for testing + selectedGroups := make([]int, 0, len(mangaGroups)) + for k := range mangaGroups { + selectedGroups = append(selectedGroups, k) + } + + logrus.Infof("Selected groups: %d", selectedGroups) + + logrus.Infof("Calculating Volumes...") + + mangaVolumeChapter := map[string]mangadex.MangaChapter{} + var mangaVolumeChapterKeys []string + + for chapter := range mangaChapters { + if !strings.Contains(mangaChapters[chapter].Chapter, ".") { + mangaChapters[chapter].Chapter += ".0" + } + volumeChapterKey := fmt.Sprintf("%04s_%08s", mangaChapters[chapter].Volume, mangaChapters[chapter].Chapter) + _, exists := mangaVolumeChapter[volumeChapterKey] + if !exists { + logrus.Debugf("Collecting volume %4s chapter %4s from group %7d", mangaChapters[chapter].Volume, mangaChapters[chapter].Chapter, mangaChapters[chapter].Groups) + mangaVolumeChapter[volumeChapterKey] = mangaChapters[chapter] + mangaVolumeChapterKeys = append(mangaVolumeChapterKeys, volumeChapterKey) + } + } + + logrus.Debugf("Sorting by volume and chapter") + sort.Strings(mangaVolumeChapterKeys) + + for i := range mangaVolumeChapterKeys { + logrus.Info(mangaVolumeChapterKeys[i]) + } + // TODO: Build output directory structure: Output///_%4d.jpg + // TODO: Download chapters + // TODO: Store download status + // TODO: Download covers: Output// - Volume /0000.jpg + // TODO: Zip folders into CBR files +}