Skip to content

Commit

Permalink
refactor naming and flags to prevent confusion
Browse files Browse the repository at this point in the history
  • Loading branch information
laureanray committed Jul 15, 2022
1 parent 05ec0dc commit 02bb4fa
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions .wakatime-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
clibgen
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ clibgen search "Eloquent JavaScript"
```

Search for a book using the newer website (useful if for some reason the old website is down or the mirrors are not working)
`-m or -mirror` flag
`-s or -site` flag
```shell
clibgen search -m "new" "Eloquent JavaScript"
```
Expand Down
8 changes: 4 additions & 4 deletions cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var (
selectedMirror string
selectedSite string
numberOfResults = 10

searchCmd = &cobra.Command{
Expand All @@ -26,9 +26,9 @@ var (

var libgenType = api.LibgenNew

if selectedMirror == "old" {
if selectedSite == "old" {
libgenType = api.LibgenOld
} else if selectedMirror == "new" {
} else if selectedSite == "new" {
libgenType = api.LibgenNew
}

Expand Down Expand Up @@ -68,7 +68,7 @@ var (
func init() {
searchCmd.
PersistentFlags().
StringVarP(&selectedMirror, "mirror", "m", "old", `select which mirror to use
StringVarP(&selectedSite, "site", "s", "old", `select which site to use
options:
"old" -> libgen.is
"new" -> liggen.li
Expand Down
22 changes: 11 additions & 11 deletions pkg/api/libgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ Currently there are three libgen domains:
These domains might change in the future
*/

type MirrorType int32
type Site int32

const (
LibgenOld MirrorType = 0
LibgenNew MirrorType = 1
LibgenOld Site = 0
LibgenNew Site = 1
)

// Note: Applicable only for libgen.is! (We might need to implemented something like this for libgen.li)
Expand Down Expand Up @@ -136,8 +136,8 @@ func getBookDataFromDocumentNew(document *goquery.Document) []Book {
}

// Parse HTML and get the data from the table by parsing and iterating through them.
func getBookDataFromDocument(document *goquery.Document, libgenMirrorType MirrorType) []Book {
switch libgenMirrorType {
func getBookDataFromDocument(document *goquery.Document, libgenSite Site) []Book {
switch libgenSite {
case LibgenOld:
return getBookDataFromDocumentOld(document)
case LibgenNew:
Expand All @@ -154,7 +154,7 @@ func getLinkFromDocumentNew(document *goquery.Document) (string, bool) {
return document.Find("#main a").First().Attr("href")
}

func getDirectDownloadLink(link string, libgenType MirrorType) string {
func getDirectDownloadLink(link string, libgenType Site) string {
log.Println("Obtaining direct download link")

resp, err := http.Get(link)
Expand Down Expand Up @@ -203,11 +203,11 @@ func getDirectDownloadLink(link string, libgenType MirrorType) string {
return ""
}

func SearchBookByTitle(query string, limit int, libgenMirrorType MirrorType) ([]Book, error) {
func SearchBookByTitle(query string, limit int, libgenSite Site) ([]Book, error) {
log.Println("Searching for:", query)
var e error
var baseUrl string
switch libgenMirrorType {
switch libgenSite {
case LibgenOld:
baseUrl = "https://libgen.is/search.php"
case LibgenNew:
Expand Down Expand Up @@ -237,7 +237,7 @@ func SearchBookByTitle(query string, limit int, libgenMirrorType MirrorType) ([]
e = err
}

books := getBookDataFromDocument(document, libgenMirrorType)
books := getBookDataFromDocument(document, libgenSite)

if len(books) >= limit {
books = books[:limit]
Expand All @@ -247,14 +247,14 @@ func SearchBookByTitle(query string, limit int, libgenMirrorType MirrorType) ([]
}

// DownloadSelection Downloads the file to current working directory
func DownloadSelection(selectedBook Book, libgenType MirrorType) {
func DownloadSelection(selectedBook Book, libgenType Site) {
log.Println("Initializing download")
link := getDirectDownloadLink(selectedBook.Mirrors[0], libgenType)
req, _ := http.NewRequest("GET", link, nil)
resp, error := http.DefaultClient.Do(req)

if error != nil {
log.Fatal("Failed to download! Please try other mirror")
log.Fatal("Failed to download! Please try the other site")
}

defer resp.Body.Close()
Expand Down

0 comments on commit 02bb4fa

Please sign in to comment.