From 0ff9bc9c8e75b2bc53a1ee41b7f4f538d0bf56ea Mon Sep 17 00:00:00 2001 From: laureanray Date: Tue, 27 Jun 2023 10:22:32 +0800 Subject: [PATCH] commit sa bqrq --- cmd/search.go | 10 ++++++++++ internal/mirror/current_mirror.go | 4 ++++ internal/mirror/legacy_mirror.go | 20 ++++++++++++++++++++ internal/mirror/mirror.go | 2 +- 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/cmd/search.go b/cmd/search.go index 7214bec..1a69e67 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -35,6 +35,7 @@ func getExtension(s string) string { var ( selectedSite string + selectedFilter string numberOfResults = 10 searchCmd = &cobra.Command{ @@ -100,6 +101,15 @@ func init() { "new" `) + searchCmd. + PersistentFlags(). + StringVarP(&selectedFilter, "filter", "f", "title", `select which filter to use + options: + "title" + "author" + "isbn" + `) + searchCmd. PersistentFlags(). IntVarP(&numberOfResults, "number of results", "n", 10, `number of result(s) to be displayed maximum: 25`) diff --git a/internal/mirror/current_mirror.go b/internal/mirror/current_mirror.go index 3c54b9c..c8ed4e2 100644 --- a/internal/mirror/current_mirror.go +++ b/internal/mirror/current_mirror.go @@ -55,6 +55,10 @@ func (m *CurrentMirror) SearchByTitle(query string) ([]book.Book, error) { return bookResults, err } +func (m *CurrentMirror) SearchByAuthor(query string) ([]book.Book, error) { + return nil, nil +} + // Search the libgen site returns the document // of the search results page diff --git a/internal/mirror/legacy_mirror.go b/internal/mirror/legacy_mirror.go index 852bd4f..c41d45a 100644 --- a/internal/mirror/legacy_mirror.go +++ b/internal/mirror/legacy_mirror.go @@ -55,6 +55,26 @@ func (m *LegacyMirror) SearchByTitle(query string) ([]book.Book, error) { return bookResults, err } +func (m *LegacyMirror) SearchByAuthor(query string) ([]book.Book, error) { + fmt.Println("Searching by author: %s", console.Higlight(query)) + + m.filter = libgen.AUTHOR + document, err := m.searchSite(query) + + if err != nil { + fmt.Println(console.Error("Error searching for book: %s", query)) + } + + bookResults := + documentparser.NewLegacyDocumentParser(document).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 *LegacyMirror) searchSite(query string) (*goquery.Document, error) { diff --git a/internal/mirror/mirror.go b/internal/mirror/mirror.go index 138d7d5..da2acf9 100644 --- a/internal/mirror/mirror.go +++ b/internal/mirror/mirror.go @@ -7,7 +7,7 @@ import ( type Mirror interface { SearchByTitle(query string) ([]book.Book, error) - // SearchByAuthor(author string) []book.Book + SearchByAuthor(author string) ([]book.Book, error) // SearchByISBN(isbn string) []book.Book // 1GetDownloadLink(book book.Book) string DownloadSelection(book book.Book)