Skip to content

Commit ee026f6

Browse files
authored
add link type (#37)
this should close #36
2 parents 12a0572 + e3e9ea3 commit ee026f6

File tree

5 files changed

+35
-18
lines changed

5 files changed

+35
-18
lines changed

cmd/search.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func getExtension(s string) string {
2727
}
2828

2929
var (
30-
selectedFilter string
30+
selectedFilter string
31+
linkType string
3132
outputDirectory string
3233
numberOfResults int
3334

@@ -42,18 +43,18 @@ var (
4243
return
4344
}
4445

45-
m := mirror.NewLegacyMirror(libgen.IS)
46+
m := mirror.NewLegacyMirror(libgen.IS)
4647

47-
// Set Defaults
48-
m.SetNumberOfResults(numberOfResults)
48+
// Set Defaults
49+
m.SetNumberOfResults(numberOfResults)
4950

5051
var books []book.Book
5152

5253
switch selectedFilter {
5354
case libgen.AUTHOR:
5455
books, _ = m.SearchByAuthor(args[0])
55-
case libgen.ISBN:
56-
books, _ = m.SearchByTitle(args[0])
56+
case libgen.ISBN:
57+
books, _ = m.SearchByTitle(args[0])
5758
default:
5859
books, _ = m.SearchByTitle(args[0])
5960
}
@@ -74,7 +75,7 @@ var (
7475
prompt := promptui.Select{
7576
Label: "Select Title",
7677
Items: titles,
77-
Size: 10,
78+
Size: 10,
7879
}
7980

8081
resultInt, _, err := prompt.Run()
@@ -84,7 +85,7 @@ var (
8485
return
8586
}
8687

87-
m.DownloadSelection(books[resultInt], outputDirectory)
88+
m.DownloadSelection(books[resultInt], outputDirectory, linkType)
8889
},
8990
}
9091
)
@@ -98,6 +99,10 @@ func init() {
9899
PersistentFlags().
99100
StringVarP(&outputDirectory, "output", "o", "./", `Output directory`)
100101

102+
searchCmd.
103+
PersistentFlags().
104+
StringVarP(&linkType, "l", "l", "default", `Standard or Faster Download link (default usually works for most of the files) [default, faster]`)
105+
101106
searchCmd.
102107
PersistentFlags().
103108
IntVarP(&numberOfResults, "number of results", "n", 10, `number of result(s) to be displayed maximum: 25`)

internal/document_parser/legacy_document.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ func (ldp *LegacyDocumentParser) GetBookDataFromDocument() []book.Book {
6969
}
7070

7171
func (ldp *LegacyDocumentParser) getDownloadLinkFromDocument() (string, bool) {
72+
return ldp.doc.Find("#download > h2 > a").First().Attr("href")
73+
}
74+
75+
func (ldp *LegacyDocumentParser) getFasterDownloadLinkFromDocument() (string, bool) {
7276
return ldp.doc.Find("#download > ul > li > a").First().Attr("href")
7377
}
7478

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

90-
func GetDirectDownloadLinkFromLegacy(link string) string {
94+
func GetDirectDownloadLinkFromLegacy(link string, linkType string) string {
9195
fmt.Println("Obtaining direct download link")
9296
resp, err := http.Get(link)
9397
defer func(Body io.ReadCloser) {
@@ -103,10 +107,17 @@ func GetDirectDownloadLinkFromLegacy(link string) string {
103107
}
104108

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

109-
fmt.Println("Direct download link:", directDownloadLink)
111+
var directDownloadLink string
112+
var exists bool
113+
114+
if (linkType == "faster") {
115+
directDownloadLink, exists = page.getFasterDownloadLinkFromDocument()
116+
} else {
117+
directDownloadLink, exists = page.getDownloadLinkFromDocument()
118+
}
119+
120+
fmt.Printf("[%s] Direct download link: [%s]\n", linkType, directDownloadLink)
110121

111122
if exists {
112123
return directDownloadLink

internal/downloader/downloader.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Downloader struct {
1818
selectedBook book.Book
1919
directLink string
2020
outputFileDir string
21+
linkType string;
2122
}
2223

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

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

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

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

58-
if bytes == 0 || err != nil {
59-
fmt.Println(bytes, err)
58+
// Check if byte size is unusually low
59+
if bytes <= 200 || err != nil {
60+
fmt.Println(console.Error("File downloaded with unusually low bytes size: %d bytes", bytes))
6061
} else {
6162
fmt.Println(console.Success("File successfully downloaded: %s", f.Name()))
6263
}

internal/mirror/legacy_mirror.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ func (m *LegacyMirror) searchSite(query string, filter libgen.Filter) (*goquery.
144144
return document, e
145145
}
146146

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

151151
if outputDirectory == "" {
152152
outputDirectory = "./"

test/integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestSearch(t *testing.T) {
9494
args []string
9595
bytesToWrite []byte
9696
}{
97-
{"search test", []string{"search", "Eloquent JavaScript"}, []byte{14, 14, 10}},
97+
{"search test", []string{"search", "Eloquent JavaScript", "-l", "faster"}, []byte{14, 14, 10}},
9898
}
9999

100100
for _, tt := range tests {

0 commit comments

Comments
 (0)