diff --git a/README.md b/README.md index 632f833..225985a 100644 --- a/README.md +++ b/README.md @@ -42,8 +42,6 @@ Flags: - -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") - ### Found an issue? diff --git a/cmd/search.go b/cmd/search.go index 02d6aaa..ede22fa 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -27,10 +27,9 @@ func getExtension(s string) string { } var ( - selectedSite string - selectedFilter string + selectedFilter string outputDirectory string - numberOfResults = 10 + numberOfResults int searchCmd = &cobra.Command{ Use: "search", @@ -43,23 +42,18 @@ var ( return } - var m mirror.Mirror + m := mirror.NewLegacyMirror(libgen.IS) - if selectedSite == "legacy" { - m = mirror.NewLegacyMirror(libgen.IS) - } else if selectedSite == "new" { - m = mirror.NewCurrentMirror(libgen.LC) - } else { - // TODO: Improve this. - fmt.Print("Not an option") - return - } + // 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]) default: books, _ = m.SearchByTitle(args[0]) } @@ -80,6 +74,7 @@ var ( prompt := promptui.Select{ Label: "Select Title", Items: titles, + Size: 10, } resultInt, _, err := prompt.Run() @@ -95,10 +90,6 @@ var ( ) func init() { - searchCmd. - PersistentFlags(). - StringVarP(&selectedSite, "site", "s", "legacy", `which website to use [legacy, new]`) - searchCmd. PersistentFlags(). StringVarP(&selectedFilter, "filter", "f", "title", `search by [title, author, isbn]`) diff --git a/internal/libgen/libgen.go b/internal/libgen/libgen.go index f9f2fff..08bad72 100644 --- a/internal/libgen/libgen.go +++ b/internal/libgen/libgen.go @@ -6,12 +6,6 @@ const ( RS = "rs" IS = "is" ST = "st" - - LI = "li" - LC = "lc" - GS = "gs" - TOP = "top" - CLICK = "click" ) type Filter string diff --git a/internal/mirror/current_mirror.go b/internal/mirror/current_mirror.go deleted file mode 100644 index 8cab6d1..0000000 --- a/internal/mirror/current_mirror.go +++ /dev/null @@ -1,133 +0,0 @@ -package mirror - -import ( - "errors" - "fmt" - "io" - "net/http" - "net/url" - - "github.com/PuerkitoBio/goquery" - "github.com/laureanray/clibgen/internal/book" - "github.com/laureanray/clibgen/internal/console" - "github.com/laureanray/clibgen/internal/document_parser" - "github.com/laureanray/clibgen/internal/downloader" - "github.com/laureanray/clibgen/internal/libgen" -) - -type CurrentMirror struct { - domain libgen.Domain - filter libgen.Filter - config Configuration -} - -func NewCurrentMirror(domain libgen.Domain) *CurrentMirror { - return &CurrentMirror{ - domain: domain, - // TODO: Make this configurable - filter: libgen.TITLE, - config: Configuration{ - numberOfResults: 5, - }, - } -} - -func (m *CurrentMirror) SearchByTitle(query string) ([]book.Book, error) { - fmt.Println("Searching for: ", console.Higlight(query)) - var document *goquery.Document - - document, err := m.searchSite(query) - - if err != nil || document == nil { - fmt.Println(console.Error("Error searching for book: %s", query)) - return nil, errors.New("Error searching for book") - } - - fmt.Println(console.Success("Search complete, parsing the document...")) - - page := documentparser.NewCurrentDocumentParser(document) - bookResults := page.GetBookDataFromDocument() - - if len(bookResults) >= m.config.numberOfResults { - bookResults = bookResults[:m.config.numberOfResults] - } - - return bookResults, err -} - -func (m *CurrentMirror) SearchByAuthor(query string) ([]book.Book, error) { - fmt.Println("Searching by author: ", console.Higlight(query)) - var document *goquery.Document - - m.filter = libgen.AUTHOR - document, err := m.searchSite(query) - - if err != nil || document == nil { - fmt.Println(console.Error("Error searching for book: %s", query)) - return nil, errors.New("Error searching for book") - } - - page := documentparser.NewCurrentDocumentParser(document) - bookResults := page.GetBookDataFromDocument() - - if len(bookResults) >= m.config.numberOfResults { - bookResults = bookResults[:m.config.numberOfResults] - } - - return bookResults, err -} - -// Search the libgen site returns the document -// of the search results page -func (m *CurrentMirror) searchSite(query string) (*goquery.Document, error) { - baseUrl := fmt.Sprintf("https://libgen.%s/index.php", m.domain) - - queryString := fmt.Sprintf( - "%s?req=\"%s\"", - baseUrl, - url.QueryEscape(query), - ) - - filter := string(string(m.filter)[0]) - - reqString := queryString + "&columns%5B%5D=" + filter + "&objects%5B%5D=f&objects%5B%5D=e&objects%5B%5D=s&objects%5B%5D=a&objects%5B%5D=p&objects%5B%5D=w&topics%5B%5D=l&topics%5B%5D=c&topics%5B%5D=f&res=25&gmode=on&filesuns=all" - - fmt.Println(reqString) - - resp, e := http.Get(reqString) - - if resp.StatusCode > 400 { - fmt.Println("Library Genesis is down. ¯\\_(ツ)_/¯") - return nil, errors.New("Library Genesis is down") - } - - if e != nil { - return nil, e - } - - defer func(Body io.ReadCloser) { - err := Body.Close() - if err != nil { - e = err - } - }(resp.Body) - - document, e := goquery.NewDocumentFromReader(resp.Body) - - if e != nil { - fmt.Println(e) - return nil, e - } - - return document, e -} - -func (m *CurrentMirror) DownloadSelection(selectedBook book.Book, outputDirectory string) { - fmt.Println(console.Info("Downloading book...")) - - directLink := documentparser.GetDirectDownloadLinkFromCurrent(selectedBook.Mirrors[0]) - if outputDirectory == "" { - outputDirectory = "./" - } - downloader.NewDownloader(selectedBook, directLink, outputDirectory).Download() -} diff --git a/internal/mirror/legacy_mirror.go b/internal/mirror/legacy_mirror.go index ca8ede1..a0f25d2 100644 --- a/internal/mirror/legacy_mirror.go +++ b/internal/mirror/legacy_mirror.go @@ -17,7 +17,6 @@ import ( type LegacyMirror struct { domain libgen.Domain - filter libgen.Filter config Configuration } @@ -25,18 +24,20 @@ func NewLegacyMirror(domain libgen.Domain) *LegacyMirror { return &LegacyMirror{ domain: domain, // TODO: Make this configurable - filter: libgen.TITLE, config: Configuration{ - numberOfResults: 5, + numberOfResults: 10, }, } } +func (m *LegacyMirror) SetNumberOfResults(numberOfResults int) { + m.config.numberOfResults = numberOfResults +} + func (m *LegacyMirror) SearchByTitle(query string) ([]book.Book, error) { fmt.Println("Searching for: ", console.Higlight(query)) var document *goquery.Document - - document, err := m.searchSite(query) + document, err := m.searchSite(query, libgen.TITLE) if err != nil || document == nil { fmt.Println(console.Error("Error searching for book: %s", query)) @@ -57,11 +58,34 @@ func (m *LegacyMirror) SearchByTitle(query string) ([]book.Book, error) { return bookResults, err } +func (m *LegacyMirror) SearchByISBN(isbn string) ([]book.Book, error) { + fmt.Println("Searching using ISBN: ", console.Higlight(isbn)) + var document *goquery.Document + document, err := m.searchSite(isbn, libgen.ISBN) + + if err != nil || document == nil { + fmt.Println(console.Error("Error searching for book: %s", isbn)) + // TODO: Implement retrying + // fmt.Println(infoColor("Retrying with other site")) + // document, e = searchLibgen(query, siteToUse) // If this also fails then we have a problem + return nil, errors.New("Error searching for book") + } + fmt.Println(console.Success("Search complete, parsing the document...")) + + bookResults := + documentparser.NewLegacyDocumentParser(document).GetBookDataFromDocument() + + if len(bookResults) >= m.config.numberOfResults { + bookResults = bookResults[:m.config.numberOfResults] + } + + return bookResults, err +} + + func (m *LegacyMirror) SearchByAuthor(query string) ([]book.Book, error) { fmt.Println("Searching by author: ", console.Higlight(query)) - - m.filter = libgen.AUTHOR - document, err := m.searchSite(query) + document, err := m.searchSite(query, libgen.AUTHOR) if err != nil || document == nil { fmt.Println(console.Error("Error searching for book: %s", query)) @@ -80,15 +104,14 @@ func (m *LegacyMirror) SearchByAuthor(query string) ([]book.Book, error) { // Search the libgen site returns the document // of the search results page -func (m *LegacyMirror) searchSite(query string) (*goquery.Document, error) { - +func (m *LegacyMirror) searchSite(query string, filter libgen.Filter) (*goquery.Document, error) { baseUrl := fmt.Sprintf("https://libgen.%s/search.php", m.domain) queryString := fmt.Sprintf( "%s?req=%s&res=25&view=simple&phrase=1&column=%s", baseUrl, url.QueryEscape(query), - m.filter, + filter, ) fmt.Println(console.Info(queryString)) diff --git a/internal/mirror/mirror.go b/internal/mirror/mirror.go index 0f8af05..941c93b 100644 --- a/internal/mirror/mirror.go +++ b/internal/mirror/mirror.go @@ -8,8 +8,7 @@ import ( type Mirror interface { SearchByTitle(query string) ([]book.Book, error) SearchByAuthor(author string) ([]book.Book, error) - // SearchByISBN(isbn string) []book.Book - // 1GetDownloadLink(book book.Book) string + SearchByISBN(isbn string) ([]book.Book, error) DownloadSelection(book book.Book, outputDirectory string) }