Skip to content

Commit

Permalink
add link type (#37)
Browse files Browse the repository at this point in the history
this should close #36
  • Loading branch information
laureanray authored Aug 17, 2023
2 parents 12a0572 + e3e9ea3 commit ee026f6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
21 changes: 13 additions & 8 deletions cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func getExtension(s string) string {
}

var (
selectedFilter string
selectedFilter string
linkType string
outputDirectory string
numberOfResults int

Expand All @@ -42,18 +43,18 @@ var (
return
}

m := mirror.NewLegacyMirror(libgen.IS)
m := mirror.NewLegacyMirror(libgen.IS)

// Set Defaults
m.SetNumberOfResults(numberOfResults)
// 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])
case libgen.ISBN:
books, _ = m.SearchByTitle(args[0])
default:
books, _ = m.SearchByTitle(args[0])
}
Expand All @@ -74,7 +75,7 @@ var (
prompt := promptui.Select{
Label: "Select Title",
Items: titles,
Size: 10,
Size: 10,
}

resultInt, _, err := prompt.Run()
Expand All @@ -84,7 +85,7 @@ var (
return
}

m.DownloadSelection(books[resultInt], outputDirectory)
m.DownloadSelection(books[resultInt], outputDirectory, linkType)
},
}
)
Expand All @@ -98,6 +99,10 @@ func init() {
PersistentFlags().
StringVarP(&outputDirectory, "output", "o", "./", `Output directory`)

searchCmd.
PersistentFlags().
StringVarP(&linkType, "l", "l", "default", `Standard or Faster Download link (default usually works for most of the files) [default, faster]`)

searchCmd.
PersistentFlags().
IntVarP(&numberOfResults, "number of results", "n", 10, `number of result(s) to be displayed maximum: 25`)
Expand Down
19 changes: 15 additions & 4 deletions internal/document_parser/legacy_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (ldp *LegacyDocumentParser) GetBookDataFromDocument() []book.Book {
}

func (ldp *LegacyDocumentParser) getDownloadLinkFromDocument() (string, bool) {
return ldp.doc.Find("#download > h2 > a").First().Attr("href")
}

func (ldp *LegacyDocumentParser) getFasterDownloadLinkFromDocument() (string, bool) {
return ldp.doc.Find("#download > ul > li > a").First().Attr("href")
}

Expand All @@ -87,7 +91,7 @@ func getBookTitleFromSelection(selection *goquery.Selection) string {
return title
}

func GetDirectDownloadLinkFromLegacy(link string) string {
func GetDirectDownloadLinkFromLegacy(link string, linkType string) string {
fmt.Println("Obtaining direct download link")
resp, err := http.Get(link)
defer func(Body io.ReadCloser) {
Expand All @@ -103,10 +107,17 @@ func GetDirectDownloadLinkFromLegacy(link string) string {
}

page := NewLegacyDocumentParserFromReader(resp.Body)
// TODO: I think this can be improved
directDownloadLink, exists := page.getDownloadLinkFromDocument()

fmt.Println("Direct download link:", directDownloadLink)
var directDownloadLink string
var exists bool

if (linkType == "faster") {
directDownloadLink, exists = page.getFasterDownloadLinkFromDocument()
} else {
directDownloadLink, exists = page.getDownloadLinkFromDocument()
}

fmt.Printf("[%s] Direct download link: [%s]\n", linkType, directDownloadLink)

if exists {
return directDownloadLink
Expand Down
7 changes: 4 additions & 3 deletions internal/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Downloader struct {
selectedBook book.Book
directLink string
outputFileDir string
linkType string;
}

func NewDownloader(selectedBook book.Book, directLink string, outputFileDir string) *Downloader {
Expand All @@ -31,7 +32,6 @@ func NewDownloader(selectedBook book.Book, directLink string, outputFileDir stri
func (d *Downloader) Download() error {
fmt.Println(console.Info("Initializing download "))

// TODO: implement retry
req, _ := http.NewRequest("GET", d.directLink, nil)
resp, error := http.DefaultClient.Do(req)

Expand All @@ -55,8 +55,9 @@ func (d *Downloader) Download() error {

bytes, err := io.Copy(io.MultiWriter(f, bar), resp.Body)

if bytes == 0 || err != nil {
fmt.Println(bytes, err)
// Check if byte size is unusually low
if bytes <= 200 || err != nil {
fmt.Println(console.Error("File downloaded with unusually low bytes size: %d bytes", bytes))
} else {
fmt.Println(console.Success("File successfully downloaded: %s", f.Name()))
}
Expand Down
4 changes: 2 additions & 2 deletions internal/mirror/legacy_mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ func (m *LegacyMirror) searchSite(query string, filter libgen.Filter) (*goquery.
return document, e
}

func (m *LegacyMirror) DownloadSelection(selectedBook book.Book, outputDirectory string) {
func (m *LegacyMirror) DownloadSelection(selectedBook book.Book, outputDirectory string, linkType string) {
fmt.Println(console.Info("Downloading book..."))
directLink := documentparser.GetDirectDownloadLinkFromLegacy(selectedBook.Mirrors[0])
directLink := documentparser.GetDirectDownloadLinkFromLegacy(selectedBook.Mirrors[0], linkType)

if outputDirectory == "" {
outputDirectory = "./"
Expand Down
2 changes: 1 addition & 1 deletion test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestSearch(t *testing.T) {
args []string
bytesToWrite []byte
}{
{"search test", []string{"search", "Eloquent JavaScript"}, []byte{14, 14, 10}},
{"search test", []string{"search", "Eloquent JavaScript", "-l", "faster"}, []byte{14, 14, 10}},
}

for _, tt := range tests {
Expand Down

0 comments on commit ee026f6

Please sign in to comment.