From ce8cb95eb45ed6711f8d6c28dfd6db6e7eed7848 Mon Sep 17 00:00:00 2001 From: Jordan Date: Mon, 15 Mar 2021 17:20:06 -0700 Subject: force cache of paper if doi unrecognized (sci-hub) --- crane.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/crane.go b/crane.go index bd5bb8a..b569eb0 100644 --- a/crane.go +++ b/crane.go @@ -267,16 +267,23 @@ func (papers *Papers) NewPaperFromDOI(doi []byte, category string) (*Paper, paper.MetaPath = filepath.Join(filepath.Join(papers.Path, category), paper.PaperName+".meta.xml") - // parse scihubURL and join it w/ the DOI (accounts for no trailing slash) - url, _ := url.Parse(scihubURL) - url.Path = filepath.Join(url.Path, string(doi)) - // make outbound request to sci-hub, save paper to temporary location - tmpPDF, err := getPaper(client, url.String()) + url := scihubURL + string(doi) + tmpPDF, err := getPaper(client, url) + defer os.Remove(tmpPDF) if err != nil { - return nil, err + // try passing resource URL (from doi.org metadata) to sci-hub instead + // (force cache) + if meta.Resource != "" { + url = scihubURL + meta.Resource + tmpPDF, err = getPaper(client, url) + if err != nil { + return nil, err + } + } else { + return nil, err + } } - defer os.Remove(tmpPDF) if err := renameFile(tmpPDF, paper.PaperPath); err != nil { return nil, err @@ -613,6 +620,9 @@ func main() { papers.Path, _ = filepath.Abs(papers.Path) + if !strings.HasSuffix(scihubURL, "/") { + scihubURL = scihubURL + "/" + } if _, err := os.Stat(papers.Path); os.IsNotExist(err) { os.Mkdir(papers.Path, os.ModePerm) } -- cgit v1.2.3-54-g00ecf