From c2e39f3ccba5b9454cae0a9e9e3a4fc6ecd3ea76 Mon Sep 17 00:00:00 2001 From: laureanray Date: Thu, 17 Aug 2023 03:34:02 +0800 Subject: [PATCH 1/2] remove current mirror --- README.md | 2 - cmd/search.go | 21 +---- internal/libgen/libgen.go | 6 -- internal/mirror/current_mirror.go | 133 ------------------------------ internal/mirror/legacy_mirror.go | 39 ++++++--- internal/mirror/mirror.go | 5 +- 6 files changed, 36 insertions(+), 170 deletions(-) delete mode 100644 internal/mirror/current_mirror.go 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..987317d 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -27,8 +27,7 @@ func getExtension(s string) string { } var ( - selectedSite string - selectedFilter string + selectedFilter string outputDirectory string numberOfResults = 10 @@ -43,23 +42,15 @@ var ( return } - var m mirror.Mirror - - 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 - } + m := mirror.NewLegacyMirror(libgen.IS) 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]) } @@ -95,10 +86,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..40410a0 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,7 +24,6 @@ func NewLegacyMirror(domain libgen.Domain) *LegacyMirror { return &LegacyMirror{ domain: domain, // TODO: Make this configurable - filter: libgen.TITLE, config: Configuration{ numberOfResults: 5, }, @@ -35,8 +33,7 @@ func NewLegacyMirror(domain libgen.Domain) *LegacyMirror { 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 +54,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 +100,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..16facdd 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) } @@ -21,3 +20,5 @@ type Configuration struct { type NewMirror struct { domain libgen.Domain } + + From 3203278fa677c781b30cab123ee1e861a94bbeca Mon Sep 17 00:00:00 2001 From: laureanray Date: Thu, 17 Aug 2023 03:40:20 +0800 Subject: [PATCH 2/2] fix max num of results --- cmd/search.go | 6 +++++- internal/mirror/legacy_mirror.go | 6 +++++- internal/mirror/mirror.go | 2 -- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/cmd/search.go b/cmd/search.go index 987317d..ede22fa 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -29,7 +29,7 @@ func getExtension(s string) string { var ( selectedFilter string outputDirectory string - numberOfResults = 10 + numberOfResults int searchCmd = &cobra.Command{ Use: "search", @@ -44,6 +44,9 @@ var ( m := mirror.NewLegacyMirror(libgen.IS) + // Set Defaults + m.SetNumberOfResults(numberOfResults) + var books []book.Book switch selectedFilter { @@ -71,6 +74,7 @@ var ( prompt := promptui.Select{ Label: "Select Title", Items: titles, + Size: 10, } resultInt, _, err := prompt.Run() diff --git a/internal/mirror/legacy_mirror.go b/internal/mirror/legacy_mirror.go index 40410a0..a0f25d2 100644 --- a/internal/mirror/legacy_mirror.go +++ b/internal/mirror/legacy_mirror.go @@ -25,11 +25,15 @@ func NewLegacyMirror(domain libgen.Domain) *LegacyMirror { domain: domain, // TODO: Make this configurable 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 diff --git a/internal/mirror/mirror.go b/internal/mirror/mirror.go index 16facdd..941c93b 100644 --- a/internal/mirror/mirror.go +++ b/internal/mirror/mirror.go @@ -20,5 +20,3 @@ type Configuration struct { type NewMirror struct { domain libgen.Domain } - -