CLI Proof of Concept

This commit is contained in:
Felipe M 2021-02-06 23:50:43 +01:00
parent 157358def9
commit 07152f8f02
Signed by: fmartingr
GPG Key ID: 716BC147715E716F
5 changed files with 168 additions and 1 deletions

View File

@ -1,2 +1,3 @@
# mangadex2cbr
# MangaDex to CBR downloader
Generate CBR comics from MangaDex releases

8
go.mod Normal file
View File

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

12
go.sum Normal file
View File

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

10
main.go Normal file
View File

@ -0,0 +1,10 @@
package main
import (
"code.fmartingr.dev/fmartingr/mangadex2cbr/pkg/cli"
)
func main() {
//logrus.SetFormatter(&logrus.JSONFormatter{})
cli.Start()
}

136
pkg/cli/cli.go Normal file
View File

@ -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/<Manga.Name>/<Manga.Volume>/<Manga.Chapter>_%4d.jpg
// TODO: Download chapters
// TODO: Store download status
// TODO: Download covers: Output/<Manga.Name>/<Manga.Name> - Volume <Manga.Volume>/0000.jpg
// TODO: Zip folders into CBR files
}