diff --git a/cmd/search.go b/cmd/search.go index 5bc7f85..dde7eab 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -71,8 +71,12 @@ var ( 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) diff --git a/internal/mirror/current_mirror.go b/internal/mirror/current_mirror.go index fa109b3..50d83fa 100644 --- a/internal/mirror/current_mirror.go +++ b/internal/mirror/current_mirror.go @@ -1,6 +1,7 @@ package mirror import ( + "errors" "fmt" "io" "net/http" @@ -37,12 +38,11 @@ func (m *CurrentMirror) SearchByTitle(query string) ([]book.Book, error) { document, err := m.searchSite(query) - if err != nil { - fmt.Println(console.Error("Error searching for book: %s", query)) - // TODO: Implement retrying - // fmt.Println(infoColor("Retrying with other site")) - // document, e = searchLibgen(query, siteToUse) // If this also fails then we have a problem - } + 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) @@ -62,13 +62,10 @@ func (m *CurrentMirror) SearchByAuthor(query string) ([]book.Book, error) { m.filter = libgen.AUTHOR document, err := m.searchSite(query) - if err != nil { - fmt.Println(console.Error("Error searching for book: %s", query)) - // TODO: Implement retrying - // fmt.Println(infoColor("Retrying with other site")) - // document, e = searchLibgen(query, siteToUse) // If this also fails then we have a problem - } - fmt.Println(console.Success("Search complete, parsing the document...")) + 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() @@ -84,7 +81,6 @@ func (m *CurrentMirror) SearchByAuthor(query string) ([]book.Book, error) { // 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( @@ -101,6 +97,11 @@ func (m *CurrentMirror) searchSite(query string) (*goquery.Document, error) { 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 } diff --git a/internal/mirror/legacy_mirror.go b/internal/mirror/legacy_mirror.go index 04a148b..3bb8676 100644 --- a/internal/mirror/legacy_mirror.go +++ b/internal/mirror/legacy_mirror.go @@ -1,6 +1,7 @@ package mirror import ( + "errors" "fmt" "io" "net/http" @@ -37,11 +38,12 @@ func (m *LegacyMirror) SearchByTitle(query string) ([]book.Book, error) { document, err := m.searchSite(query) - if err != nil { + if err != nil || document == nil { fmt.Println(console.Error("Error searching for book: %s", query)) // 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...")) @@ -61,8 +63,9 @@ func (m *LegacyMirror) SearchByAuthor(query string) ([]book.Book, error) { m.filter = libgen.AUTHOR document, err := m.searchSite(query) - if err != nil { + if err != nil || document == nil { fmt.Println(console.Error("Error searching for book: %s", query)) + return nil, errors.New("Error searching for book") } bookResults := @@ -92,6 +95,11 @@ func (m *LegacyMirror) searchSite(query string) (*goquery.Document, error) { resp, e := http.Get(queryString) + if (resp.StatusCode > 400) { + fmt.Println("Library Genesis is down. ¯\\_(ツ)_/¯") + return nil, errors.New("Library Genesis is down") + } + if e != nil { return nil, e }