diff --git a/README.md b/README.md index 5c107a0..632f833 100644 --- a/README.md +++ b/README.md @@ -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? diff --git a/cmd/search.go b/cmd/search.go index dde7eab..02d6aaa 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -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() @@ -36,7 +28,8 @@ func getExtension(s string) string { var ( selectedSite string - selectedFilter string + selectedFilter string + outputDirectory string numberOfResults = 10 searchCmd = &cobra.Command{ @@ -50,55 +43,53 @@ 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) }, } ) @@ -106,20 +97,15 @@ var ( 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(). diff --git a/internal/downloader/downloader.go b/internal/downloader/downloader.go index dab4bcd..d88a8e3 100644 --- a/internal/downloader/downloader.go +++ b/internal/downloader/downloader.go @@ -5,6 +5,7 @@ import ( "io" "net/http" "os" + "path/filepath" "strings" "github.com/kennygrant/sanitize" @@ -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, } } @@ -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() diff --git a/internal/mirror/current_mirror.go b/internal/mirror/current_mirror.go index 50d83fa..7482aeb 100644 --- a/internal/mirror/current_mirror.go +++ b/internal/mirror/current_mirror.go @@ -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() } diff --git a/internal/mirror/legacy_mirror.go b/internal/mirror/legacy_mirror.go index 3bb8676..eb78edd 100644 --- a/internal/mirror/legacy_mirror.go +++ b/internal/mirror/legacy_mirror.go @@ -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() } diff --git a/internal/mirror/mirror.go b/internal/mirror/mirror.go index da2acf9..9328570 100644 --- a/internal/mirror/mirror.go +++ b/internal/mirror/mirror.go @@ -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 diff --git a/internal/utils/string.go b/internal/utils/string.go new file mode 100644 index 0000000..fe7dc5a --- /dev/null +++ b/internal/utils/string.go @@ -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], " ")] + " ..." +}