Skip to content

Commit

Permalink
Reformat Code (gofmt) (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
laureanray authored Jul 13, 2023
2 parents 5f2ea0d + 540675f commit a3d1ea1
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 234 deletions.
4 changes: 2 additions & 2 deletions internal/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ func Success(format string, a ...any) string {
}

func Normal(format string, a ...any) string {
white := color.New(color.FgWhite).SprintFunc()
return white(fmt.Sprintf(format, a...))
white := color.New(color.FgWhite).SprintFunc()
return white(fmt.Sprintf(format, a...))
}
28 changes: 14 additions & 14 deletions internal/document_parser/current_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ func NewCurrentDocumentParserFromReader(r io.Reader) *CurrentDocumentParser {
}

func findTitle(selection *goquery.Selection) string {
var title string
selection.Find("a").EachWithBreak(func(v int, s *goquery.Selection) bool {
if s.Text() != "" && len(s.Text()) > 1 {
title = s.Text()
// Break out of the loop
return false
}
return true
})

return title
var title string
selection.Find("a").EachWithBreak(func(v int, s *goquery.Selection) bool {
if s.Text() != "" && len(s.Text()) > 1 {
title = s.Text()
// Break out of the loop
return false
}
return true
})

return title
}

func (cdp *CurrentDocumentParser) GetBookDataFromDocument() []book.Book {
Expand Down Expand Up @@ -82,7 +82,7 @@ func (cdp *CurrentDocumentParser) GetBookDataFromDocument() []book.Book {
}

func (cdp *CurrentDocumentParser) getDownloadLinkFromDocument() (string, bool) {
return cdp.doc.Find("#main a").First().Attr("href")
return cdp.doc.Find("#main a").First().Attr("href")
}

func (cdp *CurrentDocumentParser) getBookTitleFromSelection(selection *goquery.Selection) string {
Expand All @@ -103,8 +103,8 @@ func (cdp *CurrentDocumentParser) getBookTitleFromSelection(selection *goquery.S
func (cdp *CurrentDocumentParser) GetDirectDownloadLink(selectedBook book.Book) string {
fmt.Println("Obtaining direct download link")

// TODO Implement retry?
link := selectedBook.Mirrors[0]
// TODO Implement retry?
link := selectedBook.Mirrors[0]

resp, err := http.Get(link)
defer func(Body io.ReadCloser) {
Expand Down
5 changes: 2 additions & 3 deletions internal/document_parser/document_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
// }

type DocumentParser interface {
GetBookDataFromDocument() []book.Book
GetDownloadLinkFromDocument() (string, bool)
GetBookDataFromDocument() []book.Book
GetDownloadLinkFromDocument() (string, bool)
}

53 changes: 24 additions & 29 deletions internal/document_parser/legacy_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import (
)

type LegacyDocumentParser struct {
doc *goquery.Document
doc *goquery.Document
}

func NewLegacyDocumentParser(document *goquery.Document) *LegacyDocumentParser {
return &LegacyDocumentParser{doc: document}
}

func NewLegacyDocumentParserFromReader(r io.Reader) *LegacyDocumentParser {
document, _ := goquery.NewDocumentFromReader(r)
return &LegacyDocumentParser{doc: document}
document, _ := goquery.NewDocumentFromReader(r)
return &LegacyDocumentParser{doc: document}
}

func (ldp *LegacyDocumentParser) GetBookDataFromDocument() []book.Book {
Expand Down Expand Up @@ -48,7 +48,7 @@ func (ldp *LegacyDocumentParser) GetBookDataFromDocument() []book.Book {
case 9, 10, 11:
href, hrefExists := columnSelection.Find("a").Attr("href")
if hrefExists {
mirrors = append(mirrors, href)
mirrors = append(mirrors, href)
}
}
})
Expand All @@ -68,12 +68,10 @@ func (ldp *LegacyDocumentParser) GetBookDataFromDocument() []book.Book {
return books
}


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


func getBookTitleFromSelection(selection *goquery.Selection) string {
var title string
selection.Find("a").Each(func(v int, s *goquery.Selection) {
Expand All @@ -89,7 +87,6 @@ func getBookTitleFromSelection(selection *goquery.Selection) string {
return title
}


func GetDirectDownloadLinkFromLegacy(link string) string {
fmt.Println("Obtaining direct download link")
resp, err := http.Get(link)
Expand All @@ -105,17 +102,17 @@ func GetDirectDownloadLinkFromLegacy(link string) string {
fmt.Println("Error getting response:", err)
}

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

fmt.Println("Direct download link:", directDownloadLink)

fmt.Println("Direct download link:", directDownloadLink)
if exists {
return directDownloadLink
}

if exists {
return directDownloadLink
}

return ""
return ""
}

func GetDirectDownloadLinkFromCurrent(link string) string {
Expand All @@ -133,17 +130,15 @@ func GetDirectDownloadLinkFromCurrent(link string) string {
fmt.Println("Error getting response:", err)
}

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

fmt.Println("Direct download link:", directDownloadLink)
page := NewCurrentDocumentParserFromReader(resp.Body)
// TODO: I think this can be improved
directDownloadLink, exists := page.getDownloadLinkFromDocument()

if exists {
return directDownloadLink
}

return ""
}
fmt.Println("Direct download link:", directDownloadLink)

if exists {
return directDownloadLink
}

return ""
}
24 changes: 12 additions & 12 deletions internal/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ import (
)

type Downloader struct {
selectedBook book.Book
directLink string
outputFileDir string
selectedBook book.Book
directLink string
outputFileDir string
}

func NewDownloader(selectedBook book.Book, directLink string, outputFileDir string) *Downloader {
return &Downloader{
selectedBook: selectedBook,
directLink: directLink,
outputFileDir: outputFileDir,
}
return &Downloader{
selectedBook: selectedBook,
directLink: directLink,
outputFileDir: outputFileDir,
}
}

func (d *Downloader) Download() error {
fmt.Println(console.Info("Initializing download "))

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

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

defer resp.Body.Close()
filename := sanitize.Path(strings.Trim(d.selectedBook.Title, " ") + "." + d.selectedBook.Extension)
filename = filepath.Clean(d.outputFileDir + "/" + filename)
filename = filepath.Clean(d.outputFileDir + "/" + filename)

fmt.Println("Downloading to: ", filename)
fmt.Println("Downloading to: ", filename)

f, _ := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0666)
defer f.Close()
Expand All @@ -61,5 +61,5 @@ func (d *Downloader) Download() error {
fmt.Println(console.Success("File successfully downloaded: %s", f.Name()))
}

return err
return err
}
37 changes: 19 additions & 18 deletions internal/libgen/libgen.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
package libgen

type Domain string

const (
RS = "rs"
IS = "is"
ST = "st"
RS = "rs"
IS = "is"
ST = "st"

LI = "li"
LC = "lc"
GS = "gs"
TOP = "top"
CLICK = "click"
LI = "li"
LC = "lc"
GS = "gs"
TOP = "top"
CLICK = "click"
)

type Filter string

const (
TITLE = "title"
AUTHOR = "author"
SERIES = "series"
PUBLISHER = "publisher"
YEAR = "year"
ISBN = "isbn"
MD5 = "md5"
TAGS = "tags"
EXTENSION = "extension"
TITLE = "title"
AUTHOR = "author"
SERIES = "series"
PUBLISHER = "publisher"
YEAR = "year"
ISBN = "isbn"
MD5 = "md5"
TAGS = "tags"
EXTENSION = "extension"
)

Loading

0 comments on commit a3d1ea1

Please sign in to comment.