2023-10-07 01:25:56 +00:00
|
|
|
package search
|
|
|
|
|
|
|
|
import (
|
2024-01-22 22:43:47 +00:00
|
|
|
"crypto/tls"
|
2023-11-25 23:38:18 +00:00
|
|
|
"fmt"
|
2023-10-07 01:25:56 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2024-01-22 22:43:47 +00:00
|
|
|
const userAgent string = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0"
|
|
|
|
|
2023-10-07 01:25:56 +00:00
|
|
|
type Cadence string
|
|
|
|
|
|
|
|
const (
|
2023-11-25 23:38:18 +00:00
|
|
|
CADENCE_TOP_YEAR Cadence = "y"
|
|
|
|
CADENCE_TOP_MONTH Cadence = "m"
|
2023-10-07 01:25:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type BookType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
BOOK_FICTION BookType = iota
|
|
|
|
BOOK_NON_FICTION
|
|
|
|
)
|
|
|
|
|
2023-11-25 23:38:18 +00:00
|
|
|
type Source string
|
|
|
|
|
|
|
|
const (
|
|
|
|
SOURCE_ANNAS_ARCHIVE Source = "Annas Archive"
|
|
|
|
SOURCE_LIBGEN_FICTION Source = "LibGen Fiction"
|
|
|
|
SOURCE_LIBGEN_NON_FICTION Source = "LibGen Non-fiction"
|
|
|
|
)
|
|
|
|
|
2023-10-07 01:25:56 +00:00
|
|
|
type SearchItem struct {
|
|
|
|
ID string
|
|
|
|
Title string
|
|
|
|
Author string
|
|
|
|
Language string
|
|
|
|
Series string
|
|
|
|
FileType string
|
|
|
|
FileSize string
|
|
|
|
UploadDate string
|
|
|
|
}
|
|
|
|
|
2023-11-25 23:38:18 +00:00
|
|
|
type sourceDef struct {
|
|
|
|
searchURL string
|
|
|
|
downloadURL string
|
|
|
|
parseSearchFunc func(io.ReadCloser) ([]SearchItem, error)
|
|
|
|
parseDownloadFunc func(io.ReadCloser) (string, error)
|
2023-10-07 01:25:56 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 23:38:18 +00:00
|
|
|
var sourceDefs = map[Source]sourceDef{
|
|
|
|
SOURCE_ANNAS_ARCHIVE: {
|
|
|
|
searchURL: "https://annas-archive.org/search?index=&q=%s&ext=epub&sort=&lang=en",
|
2024-08-11 15:02:46 +00:00
|
|
|
downloadURL: "http://library.lol/fiction/%s",
|
2023-11-25 23:38:18 +00:00
|
|
|
parseSearchFunc: parseAnnasArchive,
|
2024-08-11 15:02:46 +00:00
|
|
|
parseDownloadFunc: parseLibGenDownloadURL,
|
2023-11-25 23:38:18 +00:00
|
|
|
},
|
|
|
|
SOURCE_LIBGEN_FICTION: {
|
|
|
|
searchURL: "https://libgen.is/fiction/?q=%s&language=English&format=epub",
|
|
|
|
downloadURL: "http://library.lol/fiction/%s",
|
|
|
|
parseSearchFunc: parseLibGenFiction,
|
|
|
|
parseDownloadFunc: parseLibGenDownloadURL,
|
|
|
|
},
|
|
|
|
SOURCE_LIBGEN_NON_FICTION: {
|
|
|
|
searchURL: "https://libgen.is/search.php?req=%s",
|
|
|
|
downloadURL: "http://library.lol/main/%s",
|
|
|
|
parseSearchFunc: parseLibGenNonFiction,
|
|
|
|
parseDownloadFunc: parseLibGenDownloadURL,
|
|
|
|
},
|
2023-10-07 01:25:56 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 23:38:18 +00:00
|
|
|
func SearchBook(query string, source Source) ([]SearchItem, error) {
|
|
|
|
def := sourceDefs[source]
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Debug("Source: ", def)
|
2023-11-25 23:38:18 +00:00
|
|
|
url := fmt.Sprintf(def.searchURL, url.QueryEscape(query))
|
|
|
|
body, err := getPage(url)
|
2023-10-26 10:20:56 +00:00
|
|
|
if err != nil {
|
2023-11-25 23:38:18 +00:00
|
|
|
return nil, err
|
2023-10-26 10:20:56 +00:00
|
|
|
}
|
2023-11-25 23:38:18 +00:00
|
|
|
return def.parseSearchFunc(body)
|
2023-10-07 01:25:56 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 23:38:18 +00:00
|
|
|
func SaveBook(id string, source Source) (string, error) {
|
|
|
|
def := sourceDefs[source]
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Debug("Source: ", def)
|
2023-11-25 23:38:18 +00:00
|
|
|
url := fmt.Sprintf(def.downloadURL, id)
|
2023-10-07 01:25:56 +00:00
|
|
|
|
2023-11-25 23:38:18 +00:00
|
|
|
body, err := getPage(url)
|
2023-10-26 10:20:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-11-25 23:38:18 +00:00
|
|
|
|
|
|
|
bookURL, err := def.parseDownloadFunc(body)
|
2023-10-26 10:20:56 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Parse Download URL Error: ", err)
|
2024-02-25 01:45:26 +00:00
|
|
|
return "", fmt.Errorf("Download Failure")
|
2023-10-26 10:20:56 +00:00
|
|
|
}
|
2023-10-07 01:25:56 +00:00
|
|
|
|
|
|
|
// Create File
|
|
|
|
tempFile, err := os.CreateTemp("", "book")
|
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("File Create Error: ", err)
|
2024-02-25 01:45:26 +00:00
|
|
|
return "", fmt.Errorf("File Failure")
|
2023-10-07 01:25:56 +00:00
|
|
|
}
|
|
|
|
defer tempFile.Close()
|
|
|
|
|
|
|
|
// Download File
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Info("Downloading Book: ", bookURL)
|
2024-01-22 22:43:47 +00:00
|
|
|
resp, err := downloadBook(bookURL)
|
2023-10-07 01:25:56 +00:00
|
|
|
if err != nil {
|
|
|
|
os.Remove(tempFile.Name())
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Book URL API Failure: ", err)
|
2024-02-25 01:45:26 +00:00
|
|
|
return "", fmt.Errorf("API Failure")
|
2023-10-07 01:25:56 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Copy File to Disk
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Info("Saving Book")
|
2023-10-07 01:25:56 +00:00
|
|
|
_, err = io.Copy(tempFile, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(tempFile.Name())
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("File Copy Error: ", err)
|
2024-02-25 01:45:26 +00:00
|
|
|
return "", fmt.Errorf("File Failure")
|
2023-10-07 01:25:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tempFile.Name(), nil
|
|
|
|
}
|
|
|
|
|
2023-11-25 23:38:18 +00:00
|
|
|
func GetBookURL(id string, bookType BookType) (string, error) {
|
|
|
|
// Derive Info URL
|
|
|
|
var infoURL string
|
|
|
|
if bookType == BOOK_FICTION {
|
|
|
|
infoURL = "http://library.lol/fiction/" + id
|
|
|
|
} else if bookType == BOOK_NON_FICTION {
|
|
|
|
infoURL = "http://library.lol/main/" + id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse & Derive Download URL
|
|
|
|
body, err := getPage(infoURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// downloadURL := parseLibGenDownloadURL(body)
|
|
|
|
return parseLibGenDownloadURL(body)
|
|
|
|
}
|
|
|
|
|
2023-10-26 10:20:56 +00:00
|
|
|
func getPage(page string) (io.ReadCloser, error) {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Debug("URL: ", page)
|
2023-11-25 23:38:18 +00:00
|
|
|
|
2023-10-26 10:20:56 +00:00
|
|
|
// Set 10s Timeout
|
|
|
|
client := http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Page
|
|
|
|
resp, err := client.Get(page)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return Body
|
|
|
|
return resp.Body, err
|
2023-10-07 01:25:56 +00:00
|
|
|
}
|
|
|
|
|
2024-01-22 22:43:47 +00:00
|
|
|
func downloadBook(bookURL string) (*http.Response, error) {
|
|
|
|
// Allow Insecure
|
|
|
|
client := &http.Client{Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}}
|
|
|
|
|
|
|
|
// Start Request
|
|
|
|
req, err := http.NewRequest("GET", bookURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set UserAgent
|
|
|
|
req.Header.Set("User-Agent", userAgent)
|
|
|
|
|
|
|
|
return client.Do(req)
|
|
|
|
}
|