Skip to content

Commit

Permalink
Feature/dynamic output dir (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
laureanray authored Jul 13, 2023
2 parents 4eb4d88 + 795eace commit 5f2ea0d
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 71 deletions.
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,19 @@ Search for a book using the old website (default)
clibgen search "Eloquent JavaScript"
```

Search for a book using the newer website (this is useful if for some reason the old website is down or the mirrors are not working)
`-s or -site` flag
#### Search

```shell
clibgen search -s "new" "Eloquent JavaScript"
```
Usage:
` clibgen search [flags]`

```shell
clibgen search -s "legacy" "Eloquent JavaScript"
```
Flags:
- -f, --filter string search by [title, author, isbn] (default "title")
- -h, --help help for search
- -n, --number of results int number of result(s) to be displayed maximum: 25 (default 10)
- -o, --output string Output directory (default "./")
- -s, --site string which website to use [legacy, new] (default "legacy")

Limit search results (default: 10)

```shell
clibgen search -n 5 "Eloquent JavaScript"
```

### Found an issue?

Expand Down
92 changes: 39 additions & 53 deletions cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@ package cmd

import (
"fmt"
"strings"

"github.com/fatih/color"
"github.com/laureanray/clibgen/internal/book"
"github.com/laureanray/clibgen/internal/libgen"
"github.com/laureanray/clibgen/internal/mirror"
"github.com/laureanray/clibgen/internal/utils"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)

func truncateText(s string, max int) string {
if max > len(s) {
return s
}
return s[:strings.LastIndex(s[:max], " ")] + " ..."
}

func getExtension(s string) string {
cyan := color.New(color.FgHiCyan).SprintFunc()
magenta := color.New(color.FgHiMagenta).SprintFunc()
Expand All @@ -36,7 +28,8 @@ func getExtension(s string) string {

var (
selectedSite string
selectedFilter string
selectedFilter string
outputDirectory string
numberOfResults = 10

searchCmd = &cobra.Command{
Expand All @@ -50,76 +43,69 @@ var (
return
}

var m mirror.Mirror
var m mirror.Mirror

if selectedSite == "legacy" {
m = mirror.NewLegacyMirror(libgen.IS)
m = mirror.NewLegacyMirror(libgen.IS)
} else if selectedSite == "new" {
m = mirror.NewCurrentMirror(libgen.LC)
} else{
// TODO: Improve this.
fmt.Print("Not an option");
return
}

var books []book.Book

switch (selectedFilter) {
case libgen.AUTHOR:
books, _ = m.SearchByAuthor(args[0])
default:
books, _ = m.SearchByTitle(args[0])
}

if len(books) == 0 {
return
}
m = mirror.NewCurrentMirror(libgen.LC)
} else {
// TODO: Improve this.
fmt.Print("Not an option")
return
}

var books []book.Book

switch selectedFilter {
case libgen.AUTHOR:
books, _ = m.SearchByAuthor(args[0])
default:
books, _ = m.SearchByTitle(args[0])
}

if len(books) == 0 {
return
}

var titles []string

for _, book := range books {
parsedTitle := truncateText(book.Title, 42)
parsedAuthor := truncateText(book.Author, 24)
parsedTitle := utils.TruncateText(book.Title, 42)
parsedAuthor := utils.TruncateText(book.Author, 24)
parsedExt := getExtension(fmt.Sprintf("%-4s", book.Extension))
titles = append(titles, fmt.Sprintf("%s %-6s | %-45s %s", parsedExt, book.FileSize, parsedTitle, parsedAuthor))
}

prompt := promptui.Select{
Label: "Select Title",
Items: titles,
}

resultInt, _, err := prompt.Run()

if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}

println(resultInt)

m.DownloadSelection(books[resultInt])
m.DownloadSelection(books[resultInt], outputDirectory)
},
}
)

func init() {
searchCmd.
PersistentFlags().
StringVarP(&selectedSite, "site", "s", "legacy", `select which site to use
options:
"legacy"
"new"
`)

searchCmd.
PersistentFlags().
StringVarP(&selectedFilter, "filter", "f", "title", `select which filter to use
options:
"title"
"author"
"isbn"
`)
StringVarP(&selectedSite, "site", "s", "legacy", `which website to use [legacy, new]`)

searchCmd.
PersistentFlags().
StringVarP(&selectedFilter, "filter", "f", "title", `search by [title, author, isbn]`)

searchCmd.
PersistentFlags().
StringVarP(&outputDirectory, "output", "o", "./", `Output directory`)

searchCmd.
PersistentFlags().
Expand Down
8 changes: 7 additions & 1 deletion internal/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/kennygrant/sanitize"
Expand All @@ -16,12 +17,14 @@ import (
type Downloader struct {
selectedBook book.Book
directLink string
outputFileDir string
}

func NewDownloader(selectedBook book.Book, directLink string) *Downloader {
func NewDownloader(selectedBook book.Book, directLink string, outputFileDir string) *Downloader {
return &Downloader{
selectedBook: selectedBook,
directLink: directLink,
outputFileDir: outputFileDir,
}
}

Expand All @@ -38,6 +41,9 @@ func (d *Downloader) Download() error {

defer resp.Body.Close()
filename := sanitize.Path(strings.Trim(d.selectedBook.Title, " ") + "." + d.selectedBook.Extension)
filename = filepath.Clean(d.outputFileDir + "/" + filename)

fmt.Println("Downloading to: ", filename)

f, _ := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0666)
defer f.Close()
Expand Down
8 changes: 6 additions & 2 deletions internal/mirror/current_mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ func (m *CurrentMirror) searchSite(query string) (*goquery.Document, error) {
return document, e
}

func (m *CurrentMirror) DownloadSelection(selectedBook book.Book) {
func (m *CurrentMirror) DownloadSelection(selectedBook book.Book, outputDirectory string) {
fmt.Println(console.Info("Downloading book..."))

directLink := documentparser.GetDirectDownloadLinkFromCurrent(selectedBook.Mirrors[0])
downloader.NewDownloader(selectedBook, directLink).Download()
if outputDirectory == "" {
outputDirectory = "./"
}
downloader.NewDownloader(selectedBook, directLink, outputDirectory).Download()
}
9 changes: 7 additions & 2 deletions internal/mirror/legacy_mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,13 @@ func (m *LegacyMirror) searchSite(query string) (*goquery.Document, error) {
return document, e
}

func (m *LegacyMirror) DownloadSelection(selectedBook book.Book) {
func (m *LegacyMirror) DownloadSelection(selectedBook book.Book, outputDirectory string) {
fmt.Println(console.Info("Downloading book..."))
directLink := documentparser.GetDirectDownloadLinkFromLegacy(selectedBook.Mirrors[0])
downloader.NewDownloader(selectedBook, directLink).Download()

if outputDirectory == "" {
outputDirectory = "./"
}

downloader.NewDownloader(selectedBook, directLink, outputDirectory).Download()
}
2 changes: 1 addition & 1 deletion internal/mirror/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Mirror interface {
SearchByAuthor(author string) ([]book.Book, error)
// SearchByISBN(isbn string) []book.Book
// 1GetDownloadLink(book book.Book) string
DownloadSelection(book book.Book)
DownloadSelection(book book.Book, outputDirectory string)
}

// TODO: Make this persistent
Expand Down
10 changes: 10 additions & 0 deletions internal/utils/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package utils

import "strings"

func TruncateText(s string, max int) string {
if max > len(s) {
return s
}
return s[:strings.LastIndex(s[:max], " ")] + " ..."
}

0 comments on commit 5f2ea0d

Please sign in to comment.