238 lines
6.9 KiB
Go
238 lines
6.9 KiB
Go
package cli
|
|
|
|
import (
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"code.fmartingr.dev/fmartingr/go-mangadex"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type CliOPtions struct {
|
|
MangaID string
|
|
}
|
|
|
|
const defaultLogLevel string = "INFO"
|
|
|
|
func fileExists(filename string) bool {
|
|
info, err := os.Stat(filename)
|
|
if os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return !info.IsDir()
|
|
}
|
|
|
|
func DownloadFile(url string, destinationPath string) error {
|
|
response, err := http.Get(url)
|
|
if err != nil {
|
|
logrus.Errorf("Error downloading page: %s", err)
|
|
return err
|
|
}
|
|
|
|
if response.StatusCode != 200 {
|
|
logrus.Errorf("Status code not OK: %d", response.StatusCode)
|
|
return errors.New("Status code not 200")
|
|
}
|
|
|
|
if response.Body != nil {
|
|
defer response.Body.Close()
|
|
}
|
|
|
|
file, errOpen := os.Create(destinationPath)
|
|
if err != nil {
|
|
logrus.Errorf("Error opening file: %s", errOpen)
|
|
return errOpen
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
body, errBody := ioutil.ReadAll(response.Body)
|
|
if errBody != nil {
|
|
logrus.Errorf("Failing read body from response: %s", errBody)
|
|
return err
|
|
}
|
|
|
|
file.Write(body)
|
|
|
|
return nil
|
|
}
|
|
|
|
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.MangaChapterList
|
|
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.MangaChapterList{}
|
|
var mangaVolumeChapterKeys []string
|
|
//testTest := map[string]map[string]mangadex.MangaChapter{}
|
|
|
|
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 {
|
|
//testTest[fmt.Sprintf("%04s", mangaChapters[chapter].Volume)][fmt.Sprintf("%08s", mangaChapters[chapter].Chapter)] = mangaChapters[chapter]
|
|
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)
|
|
|
|
mangaOutputPath := filepath.Join(outputPath, manga.Title)
|
|
for i := range mangaVolumeChapterKeys {
|
|
chapter := mangaVolumeChapter[mangaVolumeChapterKeys[i]]
|
|
if chapter.Volume == "1" {
|
|
volumeOutputPath := filepath.Join(mangaOutputPath, fmt.Sprintf("%s - Volume %s", manga.Title, chapter.Volume))
|
|
|
|
logrus.Infof("Processing Volume %s Chapter %s", chapter.Volume, chapter.Chapter)
|
|
logrus.Tracef("Processing %s", mangaVolumeChapterKeys[i])
|
|
|
|
errMkdir := os.MkdirAll(volumeOutputPath, 0766)
|
|
if errMkdir != nil {
|
|
logrus.Fatalf("Error creating output directory: %s", errMkdir)
|
|
}
|
|
|
|
chapterDetail, errChapterDetail := manga.GetChapter(strconv.Itoa(chapter.ID))
|
|
if errChapterDetail != nil {
|
|
logrus.Errorf("Failed getting chapter detail: %s", errChapterDetail)
|
|
}
|
|
|
|
for page := range chapterDetail.Pages {
|
|
logrus.Infof("Processing page %s", chapterDetail.Pages[page])
|
|
extension := path.Ext(chapterDetail.Pages[page])
|
|
pageDestinationPath := filepath.Join(volumeOutputPath, fmt.Sprintf("%s_%03d%s", mangaVolumeChapterKeys[i], page, extension))
|
|
if !fileExists(pageDestinationPath) {
|
|
logrus.Tracef("Downloading page from %s", chapterDetail.Server+path.Join(chapterDetail.Hash, chapterDetail.Pages[page]))
|
|
logrus.Tracef("Downlading page to %s", pageDestinationPath)
|
|
errDownload := DownloadFile(chapterDetail.Server+path.Join(chapterDetail.Hash, chapterDetail.Pages[page]), pageDestinationPath)
|
|
if errDownload != nil {
|
|
logrus.Errorf("Error downloading page: %s", errDownload)
|
|
}
|
|
}
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
covers, errCovers := manga.GetCovers()
|
|
if errCovers != nil {
|
|
logrus.Errorf("Error getting cover information: %s", errCovers)
|
|
}
|
|
|
|
for cover := range covers {
|
|
if covers[cover].Volume == "1" {
|
|
extension := path.Ext(covers[cover].URL)
|
|
coverOutputPath := filepath.Join(mangaOutputPath, fmt.Sprintf("%s - Volume %s", manga.Title, covers[cover].Volume), fmt.Sprintf("0000%s", extension))
|
|
if !fileExists(coverOutputPath) {
|
|
logrus.Tracef("Downloading cover from %s", covers[cover].URL)
|
|
logrus.Tracef("Writting cover to: %s", coverOutputPath)
|
|
errDownload := DownloadFile(covers[cover].URL, coverOutputPath)
|
|
if errDownload != nil {
|
|
logrus.Errorf("Error downloading cover: %s", errDownload)
|
|
}
|
|
}
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
}
|
|
// TODO: Zip folders into CBR files
|
|
}
|