diff --git a/.wakatime-project b/.wakatime-project new file mode 100644 index 0000000..9af17ef --- /dev/null +++ b/.wakatime-project @@ -0,0 +1 @@ +clibgen diff --git a/README.md b/README.md index e1be8dc..248f097 100644 --- a/README.md +++ b/README.md @@ -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" ``` diff --git a/cmd/search.go b/cmd/search.go index 76c2b3d..3556b5b 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -10,7 +10,7 @@ import ( ) var ( - selectedMirror string + selectedSite string numberOfResults = 10 searchCmd = &cobra.Command{ @@ -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 } @@ -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 diff --git a/pkg/api/libgen.go b/pkg/api/libgen.go index 533f6bb..0824b41 100644 --- a/pkg/api/libgen.go +++ b/pkg/api/libgen.go @@ -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) @@ -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: @@ -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) @@ -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: @@ -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] @@ -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()