unpaywall

package module
v0.0.0-...-6e416d7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2018 License: MIT Imports: 13 Imported by: 1

README

go-unpaywall

GoDoc Build Status Go Report Card

Simple unofficial library to send requests to the unpaywall API ( http://unpaywall.org/products/api ) and automatically download documents.

"Yes! We harvest content from legal sources including repositories run by universities, governments, and scholarly societies, as well as open content hosted by publishers themselves." (source, April 28, 2018)

Examples

Both Methods are also available for multiple requests / downloads (see godoc), which will be executed concurrently. By default 5 workers are started simultaneously.

Example Request
// your email address
var email string
// DOI
var doi string
u, _ := unpaywall.New(email)
// Request example
result, err := u.RequestByDOI(doi)
if err != nil {
	log.Fatalf("Error: %v", err)
}
fmt.Printf("Search result: %v", result)
Example Download
// your email address
var email string
// DOI
var doi string
u, _ := unpaywall.New(email)
var targetPath = "./"

file, err := u.DownloadByDOI(doi, targetPath)
if err != nil {
	log.Fatalf("Error: %v", err)
}
log.Printf("Success! Downloaded file to %s", file)
Example Multi-Request
// your email address
var email string
//DOIs
var dois []string
u, _ := unpaywall.New(email)
res, err := u.RequestByDOIs(dois)

for err != nil || res != nil {
	select {
	case r, ok := <-res:
		if !ok {
			res = nil
			continue
		}
		fmt.Printf("Found: %s \n", r.BestOaLocation.URLForPdf)
	case e, ok := <-err:
		if !ok {
			err = nil
			continue
		}
		fmt.Printf("Error: %s \n", e)
	}
}

CLI-Tool

The CLI tool in the cmd folder can be used like this:

unpaywall-cmd -doi "your-DOI" -email "your-email-address"

Documentation

Overview

Package unpaywall provides utitlity functions for the unpaywall.org api

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SearchResult

type SearchResult struct {
	StatusCode     int
	BestOaLocation struct {
		Evidence          string `json:"evidence"`
		HostType          string `json:"host_type"`
		IsBest            bool   `json:"is_best"`
		License           string `json:"license"`
		PmhID             string `json:"pmh_id"`
		Updated           string `json:"updated"`
		URL               string `json:"url"`
		URLForLandingPage string `json:"url_for_landing_page"`
		URLForPdf         string `json:"url_for_pdf"`
		Version           string `json:"version"`
	} `json:"best_oa_location"`
	DataStandard    int    `json:"data_standard"`
	Doi             string `json:"doi"`
	DoiURL          string `json:"doi_url"`
	Genre           string `json:"genre"`
	IsOa            bool   `json:"is_oa"`
	JournalIsInDoaj bool   `json:"journal_is_in_doaj"`
	JournalIsOa     bool   `json:"journal_is_oa"`
	JournalIssns    string `json:"journal_issns"`
	JournalName     string `json:"journal_name"`
	OaLocations     []struct {
		Evidence          string `json:"evidence"`
		HostType          string `json:"host_type"`
		IsBest            bool   `json:"is_best"`
		License           string `json:"license"`
		PmhID             string `json:"pmh_id"`
		Updated           string `json:"updated"`
		URL               string `json:"url"`
		URLForLandingPage string `json:"url_for_landing_page"`
		URLForPdf         string `json:"url_for_pdf"`
		Version           string `json:"version"`
	} `json:"oa_locations"`
	PublishedDate               string        `json:"published_date"`
	Publisher                   string        `json:"publisher"`
	Title                       string        `json:"title"`
	Updated                     string        `json:"updated"`
	XReportedNoncompliantCopies []interface{} `json:"x_reported_noncompliant_copies"`
	Year                        int           `json:"year"`
	ZAuthors                    []struct {
		Family string `json:"family"`
		Given  string `json:"given"`
	} `json:"z_authors"`
}

SearchResult describes the returned data structure from unpaywall

type Unpaywall

type Unpaywall struct {
	// contains filtered or unexported fields
}

Unpaywall provides methods to request the unpaywall api

func New

func New(email string) (*Unpaywall, error)

New returns a new unpaywall object to send requests to the API

func (*Unpaywall) DownloadByDOI

func (u *Unpaywall) DownloadByDOI(doi string, targetPath string) (string, error)

DownloadByDOI searches for the paper by DOI and downloads it to the target path returns the filename / error

Example
package main

import (
	"log"

	unpaywall "github.com/JVecsei/go-unpaywall"
)

func main() {
	// your email address
	var email string
	// DOI
	var doi string
	u, _ := unpaywall.New(email)
	var targetPath = "./"

	file, err := u.DownloadByDOI(doi, targetPath)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}
	log.Printf("Success! Downloaded file to %s", file)
}
Output:

func (*Unpaywall) DownloadByDOIs

func (u *Unpaywall) DownloadByDOIs(dois []string, targetPath string) (<-chan string, <-chan error)

DownloadByDOIs searches for the papers by DOI and downloads all found documents to the target path returns the filenames / errors

Example
package main

import (
	"fmt"

	unpaywall "github.com/JVecsei/go-unpaywall"
)

func main() {
	// your email address
	var email string

	// DOIs
	var dois []string

	// target directory
	var target string

	u, _ := unpaywall.New(email)
	res, err := u.DownloadByDOIs(dois, target)

	for err != nil || res != nil {
		select {
		case r, ok := <-res:
			if !ok {
				res = nil
				continue
			}
			fmt.Printf("Downloaded file to: %s \n", r)
		case e, ok := <-err:
			if !ok {
				err = nil
				continue
			}
			fmt.Printf("Error: %s \n", e)
		}
	}
}
Output:

func (*Unpaywall) RequestByDOI

func (u *Unpaywall) RequestByDOI(doi string) (*SearchResult, error)

RequestByDOI sends a new request to unpaywall with the given DOI

Example
package main

import (
	"fmt"
	"log"

	unpaywall "github.com/JVecsei/go-unpaywall"
)

func main() {
	// your email address
	var email string
	// DOI
	var doi string
	u, _ := unpaywall.New(email)
	// Request example
	result, err := u.RequestByDOI(doi)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}
	fmt.Printf("Search result: %v", result)
}
Output:

func (*Unpaywall) RequestByDOIs

func (u *Unpaywall) RequestByDOIs(dois []string) (<-chan *SearchResult, <-chan error)

RequestByDOIs searches for the papers by DOI and returns all found results / errors

Example
package main

import (
	"fmt"

	unpaywall "github.com/JVecsei/go-unpaywall"
)

func main() {
	// your email address
	var email string

	// DOIs
	var dois []string

	u, _ := unpaywall.New(email)
	res, err := u.RequestByDOIs(dois)

	for err != nil || res != nil {
		select {
		case r, ok := <-res:
			if !ok {
				res = nil
				continue
			}
			fmt.Printf("Found: %s \n", r.BestOaLocation.URLForPdf)
		case e, ok := <-err:
			if !ok {
				err = nil
				continue
			}
			fmt.Printf("Error: %s \n", e)
		}
	}
}
Output:

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL