2023-09-18 23:57:18 +00:00
|
|
|
package metadata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-09-23 02:12:36 +00:00
|
|
|
type MetadataInfo struct {
|
|
|
|
Title *string
|
|
|
|
Author *string
|
|
|
|
Description *string
|
|
|
|
GBID *string
|
|
|
|
ISBN []*string
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 02:12:36 +00:00
|
|
|
type gBooksIdentifiers struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Identifier string `json:"identifier"`
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 02:12:36 +00:00
|
|
|
type gBooksInfo struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Authors []string `json:"authors"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Identifiers []gBooksIdentifiers `json:"industryIdentifiers"`
|
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2023-09-23 02:12:36 +00:00
|
|
|
type gBooksQueryItem struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Info gBooksInfo `json:"volumeInfo"`
|
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2023-09-23 02:12:36 +00:00
|
|
|
type gBooksQueryResponse struct {
|
|
|
|
TotalItems int `json:"totalItems"`
|
|
|
|
Items []gBooksQueryItem `json:"items"`
|
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2023-09-23 02:12:36 +00:00
|
|
|
const GBOOKS_QUERY_URL string = "https://www.googleapis.com/books/v1/volumes?q=%s&filter=ebooks&download=epub"
|
|
|
|
const GBOOKS_GBID_INFO_URL string = "https://www.googleapis.com/books/v1/volumes/%s"
|
|
|
|
const GBOOKS_GBID_COVER_URL string = "https://books.google.com/books/content/images/frontcover/%s?fife=w480-h690"
|
|
|
|
|
2023-09-23 18:14:57 +00:00
|
|
|
func GetMetadata(metadataSearch MetadataInfo) ([]MetadataInfo, error) {
|
|
|
|
var queryResults []gBooksQueryItem
|
|
|
|
if metadataSearch.GBID != nil {
|
2023-09-23 02:12:36 +00:00
|
|
|
// Use GBID
|
2023-09-23 18:14:57 +00:00
|
|
|
resp, err := performGBIDRequest(*metadataSearch.GBID)
|
2023-09-23 02:12:36 +00:00
|
|
|
if err != nil {
|
2023-09-23 18:14:57 +00:00
|
|
|
return nil, err
|
2023-09-23 02:12:36 +00:00
|
|
|
}
|
2023-09-23 18:14:57 +00:00
|
|
|
queryResults = []gBooksQueryItem{*resp}
|
|
|
|
} else if len(metadataSearch.ISBN) > 0 {
|
|
|
|
searchQuery := "isbn:" + *metadataSearch.ISBN[0]
|
2023-09-23 02:12:36 +00:00
|
|
|
resp, err := performSearchRequest(searchQuery)
|
|
|
|
if err != nil {
|
2023-09-23 18:14:57 +00:00
|
|
|
return nil, err
|
2023-09-23 02:12:36 +00:00
|
|
|
}
|
2023-09-23 18:14:57 +00:00
|
|
|
queryResults = resp.Items
|
|
|
|
} else if metadataSearch.Title != nil && metadataSearch.Author != nil {
|
|
|
|
searchQuery := url.QueryEscape(fmt.Sprintf("%s %s", *metadataSearch.Title, *metadataSearch.Author))
|
2023-09-23 02:12:36 +00:00
|
|
|
resp, err := performSearchRequest(searchQuery)
|
|
|
|
if err != nil {
|
2023-09-23 18:14:57 +00:00
|
|
|
return nil, err
|
2023-09-23 02:12:36 +00:00
|
|
|
}
|
2023-09-23 18:14:57 +00:00
|
|
|
queryResults = resp.Items
|
2023-09-23 02:12:36 +00:00
|
|
|
} else {
|
2023-09-23 18:14:57 +00:00
|
|
|
return nil, errors.New("Invalid Data")
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 18:14:57 +00:00
|
|
|
// Normalize Data
|
|
|
|
allMetadata := []MetadataInfo{}
|
|
|
|
for _, item := range queryResults {
|
|
|
|
itemResult := MetadataInfo{
|
|
|
|
GBID: &item.ID,
|
|
|
|
Title: &item.Info.Title,
|
|
|
|
Description: &item.Info.Description,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(item.Info.Authors) > 0 {
|
|
|
|
itemResult.Author = &item.Info.Authors[0]
|
|
|
|
}
|
|
|
|
for _, item := range item.Info.Identifiers {
|
|
|
|
if item.Type == "ISBN_10" || item.Type == "ISBN_13" {
|
|
|
|
itemResult.ISBN = append(itemResult.ISBN, &item.Identifier)
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
2023-09-23 02:12:36 +00:00
|
|
|
|
2023-09-23 18:14:57 +00:00
|
|
|
allMetadata = append(allMetadata, itemResult)
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 18:14:57 +00:00
|
|
|
return allMetadata, nil
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 02:12:36 +00:00
|
|
|
func SaveCover(id string, safePath string) error {
|
2023-09-18 23:57:18 +00:00
|
|
|
// Validate File Doesn't Exists
|
|
|
|
_, err := os.Stat(safePath)
|
|
|
|
if err == nil {
|
2023-09-23 02:12:36 +00:00
|
|
|
log.Warn("[SaveCover] File Alreads Exists")
|
|
|
|
return nil
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create File
|
|
|
|
out, err := os.Create(safePath)
|
|
|
|
if err != nil {
|
2023-09-23 02:12:36 +00:00
|
|
|
log.Error("[SaveCover] File Create Error")
|
|
|
|
return errors.New("File Failure")
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
// Download File
|
2023-09-23 02:12:36 +00:00
|
|
|
log.Info("[SaveCover] Downloading Cover")
|
|
|
|
coverURL := fmt.Sprintf(GBOOKS_GBID_COVER_URL, id)
|
2023-09-18 23:57:18 +00:00
|
|
|
resp, err := http.Get(coverURL)
|
|
|
|
if err != nil {
|
2023-09-23 02:12:36 +00:00
|
|
|
log.Error("[SaveCover] Cover URL API Failure")
|
|
|
|
return errors.New("API Failure")
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Copy File to Disk
|
2023-09-23 02:12:36 +00:00
|
|
|
log.Info("[SaveCover] Saving Cover")
|
2023-09-18 23:57:18 +00:00
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
if err != nil {
|
2023-09-23 02:12:36 +00:00
|
|
|
log.Error("[SaveCover] File Copy Error")
|
|
|
|
return errors.New("File Failure")
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return FilePath
|
2023-09-23 02:12:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func performSearchRequest(searchQuery string) (*gBooksQueryResponse, error) {
|
|
|
|
apiQuery := fmt.Sprintf(GBOOKS_QUERY_URL, searchQuery)
|
|
|
|
|
|
|
|
log.Info("[performSearchRequest] Acquiring CoverID")
|
|
|
|
resp, err := http.Get(apiQuery)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[performSearchRequest] Cover URL API Failure")
|
|
|
|
return nil, errors.New("API Failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedResp := gBooksQueryResponse{}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&parsedResp)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[performSearchRequest] Google Books Query API Decode Failure")
|
|
|
|
return nil, errors.New("API Failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parsedResp.Items) == 0 {
|
|
|
|
log.Warn("[performSearchRequest] No Results")
|
|
|
|
return nil, errors.New("No Results")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &parsedResp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func performGBIDRequest(id string) (*gBooksQueryItem, error) {
|
|
|
|
apiQuery := fmt.Sprintf(GBOOKS_GBID_INFO_URL, id)
|
|
|
|
|
|
|
|
log.Info("[performGBIDRequest] Acquiring CoverID")
|
|
|
|
resp, err := http.Get(apiQuery)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[performGBIDRequest] Cover URL API Failure")
|
|
|
|
return nil, errors.New("API Failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedResp := gBooksQueryItem{}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&parsedResp)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[performGBIDRequest] Google Books ID API Decode Failure")
|
|
|
|
return nil, errors.New("API Failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &parsedResp, nil
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|