Skip to content

Commit

Permalink
Merge branch 'main' into feature/setup-versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
laureanray authored Aug 16, 2023
2 parents 972c71c + 52389ac commit 2deca15
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 171 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
25 changes: 8 additions & 17 deletions cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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])
}
Expand All @@ -80,6 +74,7 @@ var (
prompt := promptui.Select{
Label: "Select Title",
Items: titles,
Size: 10,
}

resultInt, _, err := prompt.Run()
Expand All @@ -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]`)
Expand Down
6 changes: 0 additions & 6 deletions internal/libgen/libgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ const (
RS = "rs"
IS = "is"
ST = "st"

LI = "li"
LC = "lc"
GS = "gs"
TOP = "top"
CLICK = "click"
)

type Filter string
Expand Down
133 changes: 0 additions & 133 deletions internal/mirror/current_mirror.go

This file was deleted.

45 changes: 34 additions & 11 deletions internal/mirror/legacy_mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,27 @@ import (

type LegacyMirror struct {
domain libgen.Domain
filter libgen.Filter
config Configuration
}

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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions internal/mirror/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 2deca15

Please sign in to comment.