Skip to content

Commit

Permalink
Mangal v1.5.1 (#23)
Browse files Browse the repository at this point in the history
* fix(tui): empty list select bug

* fix(packer): fix plain & cbz formats conflict

* refactor: inline cmd moved to separate function

* feat: add scoop package manifest generation

* feat: add package generation for cross-compile script

* fix: scoop generation

* docs: add more comments

* docs: add more comments

* fix: cross-compile script sha256 function

* fix: sha256sums.txt file format

* refactor: change how checksums are generated

* docs(README): add scoop
  • Loading branch information
metafates authored Jun 22, 2022
1 parent 43d7742 commit 59c70a4
Show file tree
Hide file tree
Showing 16 changed files with 742 additions and 219 deletions.
6 changes: 6 additions & 0 deletions .idea/mangal.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 42 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ Use "mangal [command] --help" for more information about a command.
You will need [Go installed](https://go.dev/doc/install)

```bash
go install github.com/metafates/mangal@latest
go install -ldflags="-s -w" github.com/metafates/mangal@latest
```

#### Update
Expand All @@ -226,21 +226,55 @@ go install github.com/metafates/mangal@latest
```bash
git clone https://github.com/metafates/mangal.git
cd mangal
go install
go install -ldflags="-s -w"
```

### MacOS

Install using [Homebrew](https://brew.sh/)

```bash
brew tap metafates/tap
brew install metafates/tap/mangal
brew tap metafates/mangal
brew install mangal
```

<details>
<summary>Update & Uninstall</summary>

#### Update

```bash
brew upgrade mangal
```

#### Uninstall

```bash
brew uninstall mangal
```

</details>

### Windows

> Soon... 😴
```powershell
scoop install https://raw.githubusercontent.com/metafates/scoop-mangal/main/mangal.json
```

<details>
<summary>Update & Uninstall</summary>

#### Update

```powershell
scoop update mangal
```

#### Uninstall

```powershell
scoop uninstall mangal
```

### Linux

Expand All @@ -254,10 +288,12 @@ cd mangal
go build
```


> You can also cross build for windows, linux & macos
> by running `cross-compile.py` (you will need Python 3)
>
> Built binaries will be stored in the `bin` folder
> Built binaries and generated packages
> will be stored in the `bin` folder
## Limitations

Expand Down
9 changes: 6 additions & 3 deletions cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
)

// RemoveCache removes cache files
func RemoveCache() (int, int64) {
var (
// counter of removed files
Expand All @@ -17,6 +18,7 @@ func RemoveCache() (int, int64) {
// Cleanup cache files
cacheDir, err := os.UserCacheDir()
if err == nil {
// Check if cache dir exists
scraperCacheDir := filepath.Join(cacheDir, CachePrefix)
if exists, err := Afero.Exists(scraperCacheDir); err == nil && exists {
files, err := Afero.ReadDir(scraperCacheDir)
Expand All @@ -34,6 +36,7 @@ func RemoveCache() (int, int64) {
return counter, bytes
}

// RemoveTemp removes temp files
func RemoveTemp() (int, int64) {
var (
// counter of removed files
Expand All @@ -42,14 +45,14 @@ func RemoveTemp() (int, int64) {
bytes int64
)

// Cleanup temp files
tempDir := os.TempDir()
tempFiles, err := Afero.ReadDir(tempDir)
if err == nil {
lowerAppName := strings.ToLower(AppName)
for _, tempFile := range tempFiles {
name := tempFile.Name()
if strings.HasPrefix(name, AppName) || strings.HasPrefix(name, lowerAppName) {

// Check if file is temp file
if strings.HasPrefix(name, TempPrefix) {

p := filepath.Join(tempDir, name)

Expand Down
158 changes: 31 additions & 127 deletions cmd.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package main

import (
"encoding/json"
"errors"
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/skratchdot/open-golang/open"
"github.com/spf13/cobra"
"log"
"os"
"path"
"path/filepath"
"strings"
"sync"
)

var rootCmd = &cobra.Command{
Use: strings.ToLower(AppName),
Short: AppName + " - A Manga Downloader",
Long: `The ultimate manga downloader multitool`,
Long: `The ultimate CLI manga downloader`,
Run: func(cmd *cobra.Command, args []string) {
config, _ := cmd.Flags().GetString("config")
initConfig(config)
Expand Down Expand Up @@ -93,142 +92,45 @@ var inlineCmd = &cobra.Command{
Long: `Search & Download manga in inline mode
Useful for scripting`,
Run: func(cmd *cobra.Command, args []string) {
asTemp, _ := cmd.Flags().GetBool("temp")
defer func() {
if !asTemp {
RemoveTemp()
}
}()

config, _ := cmd.Flags().GetString("config")
initConfig(config)
if format, _ := cmd.Flags().GetString("format"); format != "" {
UserConfig.Format = FormatType(format)
}

if err := ValidateConfig(UserConfig); err != nil {
log.Fatal(err)
}

var (
manga []*URL
wg sync.WaitGroup
)

query, _ := cmd.Flags().GetString("query")
if query == "" {
log.Fatal("Query expected")
}

wg.Add(len(UserConfig.Scrapers))

for _, scraper := range UserConfig.Scrapers {
go func(s *Scraper) {
defer wg.Done()

m, err := s.SearchManga(query)

if err == nil {
manga = append(manga, m...)
}
}(scraper)
}

wg.Wait()

showUrls, _ := cmd.Flags().GetBool("urls")
mangaIdx, _ := cmd.Flags().GetInt("manga")
chapterIdx, _ := cmd.Flags().GetInt("chapter")
asJson, _ := cmd.Flags().GetBool("json")
if mangaIdx, _ := cmd.Flags().GetInt("manga"); mangaIdx != -1 {
if mangaIdx > len(manga) || mangaIdx <= 0 {
log.Fatal("Index out of range")
}

selectedManga := manga[mangaIdx-1]
format, _ := cmd.Flags().GetString("format")
showUrls, _ := cmd.Flags().GetBool("urls")
asTemp, _ := cmd.Flags().GetBool("temp")
doRead, _ := cmd.Flags().GetBool("read")
doOpen, _ := cmd.Flags().GetBool("open")
config, _ := cmd.Flags().GetString("config")

chapters, err := selectedManga.Scraper.GetChapters(selectedManga)
if err != nil {
log.Fatal("Error while getting chapters")
}

if chapterIdx, _ := cmd.Flags().GetInt("chapter"); chapterIdx != -1 {

selectedChapter, ok := Find(chapters, func(c *URL) bool {
return c.Index == chapterIdx
})

if !ok {
log.Fatal("Index out of range")
}

read, _ := cmd.Flags().GetBool("read")

if read {
asTemp = true
}

chapterPath, err := DownloadChapter(selectedChapter, nil, asTemp)
if err != nil {
log.Fatal("Error while downloading chapter")
}

if EpubFile != nil {
EpubFile.SetAuthor(selectedManga.Scraper.Source.Base)
if err := EpubFile.Write(chapterPath); err != nil {
log.Fatal("Error while making epub file")
}
}

if read {
if UserConfig.UseCustomReader {
_ = open.StartWith(chapterPath, UserConfig.CustomReader)
} else {
_ = open.Start(chapterPath)
}
return
}

fmt.Println(chapterPath)
return
}
res, err := InlineMode(query, InlineOptions{
config: config,
mangaIdx: mangaIdx,
chapterIdx: chapterIdx,
asJson: asJson,
format: FormatType(format),
showUrls: showUrls,
asTemp: asTemp,
doRead: doRead,
doOpen: doOpen,
})

if err != nil {
if asJson {
data, err := json.Marshal(chapters)
if err != nil {
log.Fatal("Could not get data as json")
}

fmt.Println(string(data))
fmt.Printf(`{error: "%s"}\n`, err)
} else {
for _, c := range chapters {
if showUrls {
fmt.Printf("[%d] %s %s\n", c.Index, c.Info, c.Address)
} else {
fmt.Printf("[%d] %s\n", c.Index, c.Info)
}
}
fmt.Println(err)
}

} else {
if asJson {
data, err := json.Marshal(manga)
if err != nil {
log.Fatal("Could not get data as json")
}

fmt.Println(string(data))
} else {
for i, m := range manga {
if showUrls {
fmt.Printf("[%d] %s %s\n", i+1, m.Info, m.Address)
} else {
fmt.Printf("[%d] %s\n", i+1, m.Info)
}
}
}
os.Exit(1)
}

fmt.Print(res)
},
}

// initConfig initializes the config file
// If the given string is empty, it will use the default config file
func initConfig(config string) {
exists, err := Afero.Exists(config)

Expand Down Expand Up @@ -397,6 +299,7 @@ var formatsCmd = &cobra.Command{
},
}

// CmdExecute adds all subcommands to the root command and executes it
func CmdExecute() {
rootCmd.AddCommand(versionCmd)

Expand All @@ -422,6 +325,7 @@ func CmdExecute() {
inlineCmd.Flags().BoolP("urls", "u", false, "show urls")
inlineCmd.Flags().BoolP("temp", "t", false, "download as temp")
inlineCmd.Flags().BoolP("read", "r", false, "read chapter")
inlineCmd.Flags().BoolP("open", "o", false, "open url")
inlineCmd.Flags().StringP("config", "c", "", "use config from path")
inlineCmd.Flags().SortFlags = false
rootCmd.AddCommand(inlineCmd)
Expand Down
Loading

0 comments on commit 59c70a4

Please sign in to comment.