Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
returns all unpaywall file locations (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredgalanis authored May 11, 2020
1 parent 05a3e7e commit 5c2c35a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
46 changes: 23 additions & 23 deletions unpaywall.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type UnpaywallService struct {

// DOI lookup response from unpaywall
type unpaywallDOIResponse struct {
BestOaLocation unpaywallLocation `json:"best_oa_location"`
OaLocations []unpaywallLocation `json:"oa_locations"`
}

type unpaywallLocation struct {
Expand All @@ -41,28 +41,28 @@ func (u UnpaywallService) Lookup(doi string) (*DoiInfo, error) {

var doiResponse DoiInfo

// For now we'll only return the best location for the manuscript
location := results.BestOaLocation

// Get the file name from the decoded url for pdf
// but log any problems do not cause response to fail
var fileName string
decodedURLForPdf, err := url.QueryUnescape(location.URLForPdf)
if err != nil {
log.Printf("file name decoding failed: %s", err)
} else {
splitURLForPdf := strings.Split(decodedURLForPdf, "/")
fileName = splitURLForPdf[len(splitURLForPdf)-1]
}

if location.URLForPdf != "" {
doiResponse.Manuscripts = append(doiResponse.Manuscripts, Manuscript{
Location: location.URLForPdf,
RepositoryInstitution: location.RepositoryInstitution,
Type: "application/pdf",
Source: "Unpaywall",
Name: fileName,
})
for _, location := range results.OaLocations {
if location.URLForPdf != "" {

// Get the file name from the decoded url for pdf
// but log any problems do not cause response to fail
var fileName string
decodedURLForPdf, err := url.QueryUnescape(location.URLForPdf)
if err != nil {
log.Printf("file name decoding failed: %s", err)
} else {
splitURLForPdf := strings.Split(decodedURLForPdf, "/")
fileName = splitURLForPdf[len(splitURLForPdf)-1]
}

doiResponse.Manuscripts = append(doiResponse.Manuscripts, Manuscript{
Location: location.URLForPdf,
RepositoryInstitution: location.RepositoryInstitution,
Type: "application/pdf",
Source: "Unpaywall",
Name: fileName,
})
}
}

return &doiResponse, nil
Expand Down
14 changes: 14 additions & 0 deletions unpaywall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ func TestUnpaywall(t *testing.T) {
Source: "Unpaywall",
Name: "Nanometer-Scale Thermometry.pdf",
},
{
Location: "http://europepmc.org/articles/pmc4221854?pdf=render",
RepositoryInstitution: "pubmedcentral.nih.gov",
Type: "application/pdf",
Source: "Unpaywall",
Name: "pmc4221854?pdf=render",
},
{
Location: "http://arxiv.org/pdf/1304.1068",
RepositoryInstitution: "arXiv.org",
Type: "application/pdf",
Source: "Unpaywall",
Name: "1304.1068",
},
},
}

Expand Down

0 comments on commit 5c2c35a

Please sign in to comment.