Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Downloading of Scientific Articles #4 #5

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 24 additions & 35 deletions pkg/api/libgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"strings"

"github.com/PuerkitoBio/goquery"
"github.com/fatih/color"
"github.com/kennygrant/sanitize"
"github.com/laureanray/clibgen/pkg/utils"
"github.com/schollz/progressbar/v3"
)

/*

Currently there are three libgen domains:
Currently there are multiple libgen domains for now
we support the ff:

- libgen.is -> primary domain (old interface)
- libgen.li -> secondary (fallback) newer interface
Expand All @@ -25,12 +26,20 @@ These domains might change in the future
*/

type Site int32
type Type int

const (
LibgenOld Site = 0
LibgenNew Site = 1
LibgenIs Site = 0
LibgenLi Site = 1
)

const ()

type Clibgen struct {
selected int
site Site
}

// Note: Applicable only for libgen.is! (We might need to implemented something like this for libgen.li)
// Get book title from the selection, in most cases the title is hidden through nested anchor tags.
// In order to produce a clean output extra texts are also removed.
Expand Down Expand Up @@ -140,9 +149,9 @@ 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, libgenSite Site) []Book {
switch libgenSite {
case LibgenOld:
case LibgenIs:
return getBookDataFromDocumentOld(document)
case LibgenNew:
case LibgenLi:
return getBookDataFromDocumentNew(document)
}
return []Book{}
Expand Down Expand Up @@ -197,34 +206,14 @@ func getDirectDownloadLink(link string, libgenType Site) string {
}

if exists {
fmt.Println(successColor("Direct download link found"))
fmt.Println(text.Success("Direct download link found"))
return directDownloadLink
}

fmt.Println(errorColor("Direct download link not found"))
fmt.Println(text.Error("Direct download link not found"))
return ""
}

func highlight(s string) string {
magenta := color.New(color.FgHiWhite).Add(color.BgBlack).SprintFunc()
return magenta(s)
}

func errorColor(s string) string {
red := color.New(color.FgRed).SprintFunc()
return red(s)
}

func infoColor(s string) string {
yellow := color.New(color.FgYellow).SprintFunc()
return yellow(s)
}

func successColor(s string) string {
green := color.New(color.FgHiGreen).SprintFunc()
return green(s)
}

func searchLibgen(query string, libgenSite Site) (document *goquery.Document, e error) {
var baseUrl string

Expand Down Expand Up @@ -260,23 +249,23 @@ func searchLibgen(query string, libgenSite Site) (document *goquery.Document, e
}

func SearchBookByTitle(query string, limit int, libgenSite Site) (bookResults []Book, siteToUse Site, e error) {
fmt.Println("Searching for:", highlight(query))
fmt.Println("Searching for:", text.Highlight(query))
var document *goquery.Document
document, e = searchLibgen(query, siteToUse)

if e != nil {
fmt.Println(errorColor("Error searching for book: " + query))
fmt.Println(text.Error("Error searching for book: " + query))
failedSite := libgenSite
if failedSite == LibgenOld {
siteToUse = LibgenNew
} else {
siteToUse = LibgenOld
}

fmt.Println(infoColor("Retrying with other site"))
fmt.Println(text.Info("Retrying with other site"))
document, e = searchLibgen(query, siteToUse) // If this also fails then we have a problem
}
fmt.Println(successColor("Search complete, parsing the document..."))
fmt.Println(text.Success("Search complete, parsing the document..."))

bookResults = getBookDataFromDocument(document, siteToUse)

Expand All @@ -290,12 +279,12 @@ func SearchBookByTitle(query string, limit int, libgenSite Site) (bookResults []
// DownloadSelection Downloads the file to current working directory
func DownloadSelection(selectedBook Book, libgenType Site) {
link := getDirectDownloadLink(selectedBook.Mirrors[0], libgenType)
fmt.Println(infoColor("Initializing download "))
fmt.Println(text.Info("Initializing download "))
req, _ := http.NewRequest("GET", link, nil)
resp, error := http.DefaultClient.Do(req)

if error != nil {
fmt.Println(errorColor("Error downloading file: " + error.Error()))
fmt.Println(text.Error("Error downloading file: " + error.Error()))
}

defer resp.Body.Close()
Expand All @@ -314,6 +303,6 @@ func DownloadSelection(selectedBook Book, libgenType Site) {
if bytes == 0 || err != nil {
fmt.Println(bytes, err)
} else {
fmt.Println(successColor("File successfully downloaded:"), f.Name())
fmt.Println(text.Success("File successfully downloaded:"), f.Name())
}
}
13 changes: 13 additions & 0 deletions pkg/api/page.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package api

import "github.com/PuerkitoBio/goquery"

type Page struct {
doc *goquery.Document
}

func New(document *goquery.Document) *Page {
p := &Page{doc: document}

return p
}
12 changes: 12 additions & 0 deletions pkg/api/scimag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package api

type SciMag struct {
ID string
Title string
Author string
Year string
Journal string
Extension string
Mirrors []string
FileSize string
}
23 changes: 23 additions & 0 deletions pkg/utils/text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package text

import "github.com/fatih/color"

func Highlight(s string) string {
magenta := color.New(color.FgHiWhite).Add(color.BgBlack).SprintFunc()
return magenta(s)
}

func Error(s string) string {
red := color.New(color.FgRed).SprintFunc()
return red(s)
}

func Info(s string) string {
yellow := color.New(color.FgYellow).SprintFunc()
return yellow(s)
}

func Success(s string) string {
green := color.New(color.FgHiGreen).SprintFunc()
return green(s)
}