aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan <me@jordan.im>2021-03-15 17:20:06 -0700
committerJordan <me@jordan.im>2021-03-15 17:20:06 -0700
commitce8cb95eb45ed6711f8d6c28dfd6db6e7eed7848 (patch)
tree375fb91fb87a662ef41d36e1e0820f7df29ab2ce
parentf03d77f73be72a934a99541b30a9663cc99a1733 (diff)
downloadcrane-ce8cb95eb45ed6711f8d6c28dfd6db6e7eed7848.tar.gz
crane-ce8cb95eb45ed6711f8d6c28dfd6db6e7eed7848.zip
force cache of paper if doi unrecognized (sci-hub)
-rw-r--r--crane.go24
1 files 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)
}