diff --git a/cmd/search.go b/cmd/search.go index ede22fa..a2478a2 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -27,7 +27,8 @@ func getExtension(s string) string { } var ( - selectedFilter string + selectedFilter string + linkType string outputDirectory string numberOfResults int @@ -42,18 +43,18 @@ var ( return } - m := mirror.NewLegacyMirror(libgen.IS) + m := mirror.NewLegacyMirror(libgen.IS) - // Set Defaults - m.SetNumberOfResults(numberOfResults) + // Set Defaults + m.SetNumberOfResults(numberOfResults) var books []book.Book switch selectedFilter { case libgen.AUTHOR: books, _ = m.SearchByAuthor(args[0]) - case libgen.ISBN: - books, _ = m.SearchByTitle(args[0]) + case libgen.ISBN: + books, _ = m.SearchByTitle(args[0]) default: books, _ = m.SearchByTitle(args[0]) } @@ -74,7 +75,7 @@ var ( prompt := promptui.Select{ Label: "Select Title", Items: titles, - Size: 10, + Size: 10, } resultInt, _, err := prompt.Run() @@ -84,7 +85,7 @@ var ( return } - m.DownloadSelection(books[resultInt], outputDirectory) + m.DownloadSelection(books[resultInt], outputDirectory, linkType) }, } ) @@ -98,6 +99,10 @@ func init() { PersistentFlags(). StringVarP(&outputDirectory, "output", "o", "./", `Output directory`) + searchCmd. + PersistentFlags(). + StringVarP(&linkType, "l", "l", "default", `Standard or Faster Download link (default usually works for most of the files) [default, faster]`) + searchCmd. PersistentFlags(). IntVarP(&numberOfResults, "number of results", "n", 10, `number of result(s) to be displayed maximum: 25`) diff --git a/internal/document_parser/legacy_document.go b/internal/document_parser/legacy_document.go index c7b5d6e..068e934 100644 --- a/internal/document_parser/legacy_document.go +++ b/internal/document_parser/legacy_document.go @@ -69,6 +69,10 @@ func (ldp *LegacyDocumentParser) GetBookDataFromDocument() []book.Book { } func (ldp *LegacyDocumentParser) getDownloadLinkFromDocument() (string, bool) { + return ldp.doc.Find("#download > h2 > a").First().Attr("href") +} + +func (ldp *LegacyDocumentParser) getFasterDownloadLinkFromDocument() (string, bool) { return ldp.doc.Find("#download > ul > li > a").First().Attr("href") } @@ -87,7 +91,7 @@ func getBookTitleFromSelection(selection *goquery.Selection) string { return title } -func GetDirectDownloadLinkFromLegacy(link string) string { +func GetDirectDownloadLinkFromLegacy(link string, linkType string) string { fmt.Println("Obtaining direct download link") resp, err := http.Get(link) defer func(Body io.ReadCloser) { @@ -103,10 +107,17 @@ func GetDirectDownloadLinkFromLegacy(link string) string { } page := NewLegacyDocumentParserFromReader(resp.Body) - // TODO: I think this can be improved - directDownloadLink, exists := page.getDownloadLinkFromDocument() - fmt.Println("Direct download link:", directDownloadLink) + var directDownloadLink string + var exists bool + + if (linkType == "faster") { + directDownloadLink, exists = page.getFasterDownloadLinkFromDocument() + } else { + directDownloadLink, exists = page.getDownloadLinkFromDocument() + } + + fmt.Printf("[%s] Direct download link: [%s]\n", linkType, directDownloadLink) if exists { return directDownloadLink diff --git a/internal/downloader/downloader.go b/internal/downloader/downloader.go index ce028ae..717ce7a 100644 --- a/internal/downloader/downloader.go +++ b/internal/downloader/downloader.go @@ -18,6 +18,7 @@ type Downloader struct { selectedBook book.Book directLink string outputFileDir string + linkType string; } func NewDownloader(selectedBook book.Book, directLink string, outputFileDir string) *Downloader { @@ -31,7 +32,6 @@ func NewDownloader(selectedBook book.Book, directLink string, outputFileDir stri func (d *Downloader) Download() error { fmt.Println(console.Info("Initializing download ")) - // TODO: implement retry req, _ := http.NewRequest("GET", d.directLink, nil) resp, error := http.DefaultClient.Do(req) @@ -55,8 +55,9 @@ func (d *Downloader) Download() error { bytes, err := io.Copy(io.MultiWriter(f, bar), resp.Body) - if bytes == 0 || err != nil { - fmt.Println(bytes, err) + // Check if byte size is unusually low + if bytes <= 200 || err != nil { + fmt.Println(console.Error("File downloaded with unusually low bytes size: %d bytes", bytes)) } else { fmt.Println(console.Success("File successfully downloaded: %s", f.Name())) } diff --git a/internal/mirror/legacy_mirror.go b/internal/mirror/legacy_mirror.go index a0f25d2..f161efc 100644 --- a/internal/mirror/legacy_mirror.go +++ b/internal/mirror/legacy_mirror.go @@ -144,9 +144,9 @@ func (m *LegacyMirror) searchSite(query string, filter libgen.Filter) (*goquery. return document, e } -func (m *LegacyMirror) DownloadSelection(selectedBook book.Book, outputDirectory string) { +func (m *LegacyMirror) DownloadSelection(selectedBook book.Book, outputDirectory string, linkType string) { fmt.Println(console.Info("Downloading book...")) - directLink := documentparser.GetDirectDownloadLinkFromLegacy(selectedBook.Mirrors[0]) + directLink := documentparser.GetDirectDownloadLinkFromLegacy(selectedBook.Mirrors[0], linkType) if outputDirectory == "" { outputDirectory = "./"