mangadex2cbz/pkg/cli/cli.go

137 lines
3.8 KiB
Go

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
}