Skip to content

Commit

Permalink
Merge pull request #51 from metafates/dev
Browse files Browse the repository at this point in the history
Mangal v2.1.1
  • Loading branch information
metafates authored Jun 30, 2022
2 parents ca888e5 + 49becff commit d3758c8
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 192 deletions.
136 changes: 7 additions & 129 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/skratchdot/open-golang/open"
"github.com/spf13/cobra"
"log"
"net/http"
"os"
"os/exec"
"path"
Expand All @@ -28,12 +27,6 @@ The ultimate CLI manga downloader`,
incognito, _ := cmd.Flags().GetBool("incognito")
initConfig(config, false)

if !incognito {
initAnilist()
} else {
UserConfig.Anilist.Enabled = false
}

if format, _ := cmd.Flags().GetString("format"); format != "" {
UserConfig.Formats.Default = FormatType(format)
}
Expand All @@ -43,6 +36,12 @@ The ultimate CLI manga downloader`,
log.Fatal(err)
}

if !incognito {
initAnilist()
} else {
UserConfig.Anilist.Enabled = false
}

var program *tea.Program

if UserConfig.UI.Fullscreen {
Expand Down Expand Up @@ -489,128 +488,7 @@ var doctorCmd = &cobra.Command{
Long: `Check if ` + Mangal + ` is properly configured.
It checks if config file is valid and used sources are available`,
Run: func(cmd *cobra.Command, args []string) {
var (
ok = func() {
fmt.Print(successStyle.Render("OK"))
fmt.Println()
}

fail = func() {
fmt.Print(failStyle.Render("Fail"))
fmt.Println()
}
)

fmt.Print("Checking if latest version is used... ")
latestVersion, err := FetchLatestVersion()
if err != nil {
fail()
fmt.Printf("Can't find latest version\nRun %s to get more information\n", accentStyle.Render("mangal latest"))
os.Exit(1)
} else if latestVersion > Version {
fail()
fmt.Printf("New version of %s is available: %s\n", Mangal, accentStyle.Render(latestVersion))
fmt.Printf("Run %s to get more information\n", accentStyle.Render("mangal latest"))
os.Exit(1)
} else {
ok()
}

fmt.Print("Checking config... ")
UserConfig = GetConfig("")

err = ValidateConfig(UserConfig)
if err != nil {
fail()
fmt.Printf("Config error: %s\n", err)
os.Exit(1)
}

ok()

var sourceNotAvailable = func(source *Source) {
fail()
fmt.Printf("Source %s is not available\n", source.Name)
fmt.Printf("Try to reinitialize your config with %s\n", accentStyle.Render("mangal config init --force"))
fmt.Println("Note, that this will overwrite your current config")
os.Exit(1)
}

scanner := bufio.NewScanner(os.Stdin)

// check if scraper sources are online
for _, scraper := range UserConfig.Scrapers {
source := scraper.Source

// read line from stdin
fmt.Printf("Please, enter a manga title to test %s: ", source.Name)
scanner.Scan()

if scanner.Err() != nil {
fail()
fmt.Printf("Error while reading from stdin: %s\n", err)
os.Exit(1)
}

fmt.Printf("Checking source %s... ", source.Name)
resp, err := http.Get(source.Base)
if err != nil {
sourceNotAvailable(source)
}

_ = resp.Body.Close()

// check if response is 200
if resp.StatusCode != 200 {
sourceNotAvailable(source)
}

// try to get any manga page using scraper
manga, err := scraper.SearchManga(scanner.Text())
if err != nil {
sourceNotAvailable(source)
}

// check if manga is not empty
if len(manga) == 0 {
sourceNotAvailable(source)
}

// get chapters for first manga
chapters, err := scraper.GetChapters(manga[0])
if err != nil {
sourceNotAvailable(source)
}

// check if chapters is not empty
if len(chapters) == 0 {
sourceNotAvailable(source)
}

// get pages for first chapter
pages, err := scraper.GetPages(chapters[0])
if err != nil {
sourceNotAvailable(source)
}

// check if pages is not empty
if len(pages) == 0 {
sourceNotAvailable(source)
}

// try to download first page
image, err := scraper.GetFile(pages[0])
if err != nil {
sourceNotAvailable(source)
}

// check if images is not empty
if image.Len() == 0 {
sourceNotAvailable(source)
}

ok()
}
RunDoctor()
},
}

Expand Down
42 changes: 21 additions & 21 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ type FormatsConfig struct {

type Config struct {
Scrapers []*Scraper
Formats FormatsConfig
UI UIConfig
Downloader DownloaderConfig
Formats *FormatsConfig
UI *UIConfig
Downloader *DownloaderConfig
Anilist struct {
Client *AnilistClient
Enabled bool
Expand Down Expand Up @@ -128,7 +128,7 @@ func ParseConfig(configString []byte) (*Config, error) {
UI UIConfig `toml:"ui"`
UseCustomReader bool `toml:"use_custom_reader"`
CustomReader string `toml:"custom_reader"`
Sources map[string]Source
Sources map[string]*Source
Downloader DownloaderConfig
Anilist struct {
Enabled bool `toml:"enabled"`
Expand All @@ -148,6 +148,19 @@ func ParseConfig(configString []byte) (*Config, error) {
return nil, err
}

conf.Downloader = &tempConf.Downloader
if conf.Downloader.ChapterNameTemplate == "" {
conf.Downloader.ChapterNameTemplate = "[%d] %s"
}

if strings.Contains(conf.Downloader.Path, "$HOME") {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
conf.Downloader.Path = strings.ReplaceAll(conf.Downloader.Path, "$HOME", home)
}

// Convert sources listed in tempConfig to Scrapers
for sourceName, source := range tempConf.Sources {
// If source is not listed in Use then skip it
Expand All @@ -156,8 +169,8 @@ func ParseConfig(configString []byte) (*Config, error) {
}

// Create scraper
source.Name = sourceName
scraper := MakeSourceScraper(&source)
scraper := MakeSourceScraper(source)
scraper.Source.Name = sourceName

if !conf.Downloader.CacheImages {
scraper.FilesCollector.CacheDir = ""
Expand All @@ -166,30 +179,17 @@ func ParseConfig(configString []byte) (*Config, error) {
conf.Scrapers = append(conf.Scrapers, scraper)
}

conf.UI = tempConf.UI
conf.UI = &tempConf.UI
if tempConf.UI.ChapterNameTemplate == "" {
tempConf.UI.ChapterNameTemplate = "[%d] %s"
}

// Default format is pdf
conf.Formats = tempConf.Formats
conf.Formats = &tempConf.Formats
if conf.Formats.Default == "" {
conf.Formats.Default = PDF
}

conf.Downloader = tempConf.Downloader
if conf.Downloader.ChapterNameTemplate == "" {
conf.Downloader.ChapterNameTemplate = "[%d] %s"
}

if strings.Contains(conf.Downloader.Path, "$HOME") {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
conf.Downloader.Path = strings.ReplaceAll(conf.Downloader.Path, "$HOME", home)
}

conf.UseCustomReader = tempConf.UseCustomReader
conf.CustomReader = tempConf.CustomReader

Expand Down
5 changes: 4 additions & 1 deletion constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import "time"

// Version is the current version of the program.
const Version = "2.1.0"
const Version = "2.1.1"

// Mangal is a name of application
// I keep it in a constant to avoid typos
Expand All @@ -25,6 +25,9 @@ const TestQuery = "Death Note"
// It approximates to 292 years
const Forever = time.Duration(1<<63 - 1)

// Referer is a default referer for requests
const Referer = "https://www.google.com"

var AvailableFormats = []FormatType{PDF, CBZ, Plain, Zip, Epub}
var FormatsInfo = map[FormatType]string{
PDF: "Chapters as PDF with images",
Expand Down
2 changes: 1 addition & 1 deletion cross-compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import shutil
import subprocess

VERSION = "2.1.0"
VERSION = "2.1.1"
TAG = f"v{VERSION}"
DESCRIPTION = "The ultimate CLI manga downloader"
GITHUB = "https://github.com/metafates/mangal"
Expand Down
Loading

0 comments on commit d3758c8

Please sign in to comment.