Skip to content

Commit

Permalink
add 502 handling
Browse files Browse the repository at this point in the history
  • Loading branch information
laureanray committed Jun 29, 2023
1 parent 39e40fa commit f30ad1e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
6 changes: 5 additions & 1 deletion cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 15 additions & 14 deletions internal/mirror/current_mirror.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mirror

import (
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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(
Expand All @@ -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
}
Expand Down
12 changes: 10 additions & 2 deletions internal/mirror/legacy_mirror.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mirror

import (
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -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..."))

Expand All @@ -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 :=
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit f30ad1e

Please sign in to comment.