fix(search): set user agent for dl
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Evan Reichard 2024-08-13 22:32:16 -04:00
parent d4c8e4d2da
commit 0a1dfeab65
3 changed files with 30 additions and 19 deletions

View File

@ -87,7 +87,7 @@ func GetWordCount(filepath string) (*int64, error) {
} }
return &totalWords, nil return &totalWords, nil
} else { } else {
return nil, fmt.Errorf("Invalid extension") return nil, fmt.Errorf("Invalid extension: %s", fileExtension)
} }
} }

View File

@ -17,15 +17,15 @@ func parseAnnasArchiveDownloadURL(body io.ReadCloser) (string, error) {
doc, _ := goquery.NewDocumentFromReader(body) doc, _ := goquery.NewDocumentFromReader(body)
// Return Download URL // Return Download URL
downloadURL, exists := doc.Find("body > table > tbody > tr > td > a").Attr("href") downloadPath, exists := doc.Find("body > table > tbody > tr > td > a").Attr("href")
if !exists { if !exists {
return "", fmt.Errorf("Download URL not found") return "", fmt.Errorf("Download URL not found")
} }
// Possible Funky URL // Possible Funky URL
downloadURL = strings.ReplaceAll(downloadURL, "\\", "/") downloadPath = strings.ReplaceAll(downloadPath, "\\", "/")
return downloadURL, nil return fmt.Sprintf("http://libgen.li/%s", downloadPath), nil
} }
// getAnnasArchiveBookSelection parses potentially commented out HTML. For some reason // getAnnasArchiveBookSelection parses potentially commented out HTML. For some reason

View File

@ -12,7 +12,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
const userAgent string = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0" const userAgent string = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:127.0) Gecko/20100101 Firefox/127.0"
type Cadence string type Cadence string
@ -57,21 +57,21 @@ type sourceDef struct {
var sourceDefs = map[Source]sourceDef{ var sourceDefs = map[Source]sourceDef{
SOURCE_ANNAS_ARCHIVE: { SOURCE_ANNAS_ARCHIVE: {
searchURL: "https://annas-archive.org/search?index=&q=%s&ext=epub&sort=&lang=en", searchURL: "https://annas-archive.org/search?index=&q=%s&ext=epub&sort=&lang=en",
downloadURL: "http://library.lol/fiction/%s", downloadURL: "http://libgen.li/ads.php?md5=%s",
parseSearchFunc: parseAnnasArchive, parseSearchFunc: parseAnnasArchive,
parseDownloadFunc: parseLibGenDownloadURL, parseDownloadFunc: parseAnnasArchiveDownloadURL,
}, },
SOURCE_LIBGEN_FICTION: { SOURCE_LIBGEN_FICTION: {
searchURL: "https://libgen.is/fiction/?q=%s&language=English&format=epub", searchURL: "https://libgen.is/fiction/?q=%s&language=English&format=epub",
downloadURL: "http://library.lol/fiction/%s", downloadURL: "http://libgen.li/ads.php?md5=%s",
parseSearchFunc: parseLibGenFiction, parseSearchFunc: parseLibGenFiction,
parseDownloadFunc: parseLibGenDownloadURL, parseDownloadFunc: parseAnnasArchiveDownloadURL,
}, },
SOURCE_LIBGEN_NON_FICTION: { SOURCE_LIBGEN_NON_FICTION: {
searchURL: "https://libgen.is/search.php?req=%s", searchURL: "https://libgen.is/search.php?req=%s",
downloadURL: "http://library.lol/main/%s", downloadURL: "http://libgen.li/ads.php?md5=%s",
parseSearchFunc: parseLibGenNonFiction, parseSearchFunc: parseLibGenNonFiction,
parseDownloadFunc: parseLibGenDownloadURL, parseDownloadFunc: parseAnnasArchiveDownloadURL,
}, },
} }
@ -155,12 +155,19 @@ func getPage(page string) (io.ReadCloser, error) {
log.Debug("URL: ", page) log.Debug("URL: ", page)
// Set 10s Timeout // Set 10s Timeout
client := http.Client{ client := http.Client{Timeout: 10 * time.Second}
Timeout: 10 * time.Second,
// Start Request
req, err := http.NewRequest("GET", page, nil)
if err != nil {
return nil, err
} }
// Get Page // Set User-Agent
resp, err := client.Get(page) req.Header.Set("User-Agent", userAgent)
// Do Request
resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -170,10 +177,14 @@ func getPage(page string) (io.ReadCloser, error) {
} }
func downloadBook(bookURL string) (*http.Response, error) { func downloadBook(bookURL string) (*http.Response, error) {
log.Debug("URL: ", bookURL)
// Allow Insecure // Allow Insecure
client := &http.Client{Transport: &http.Transport{ client := &http.Client{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, Transport: &http.Transport{
}} TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
// Start Request // Start Request
req, err := http.NewRequest("GET", bookURL, nil) req, err := http.NewRequest("GET", bookURL, nil)
@ -181,7 +192,7 @@ func downloadBook(bookURL string) (*http.Response, error) {
return nil, err return nil, err
} }
// Set UserAgent // Set User-Agent
req.Header.Set("User-Agent", userAgent) req.Header.Set("User-Agent", userAgent)
return client.Do(req) return client.Do(req)