crossref

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2020 License: MIT Imports: 10 Imported by: 3

README

crossref

Access to Crossref API with Go

Full documentation of this package is here.

For a detailed explanation of the JSON fields, go to Crossref API Documentation.

Example Code

package main

import (
	"fmt"

	"github.com/cgxeiji/crossref"
)

var client = crossref.NewClient("Crossref Go", "mail@example.com")

func main() {
	// If you want to debug the library, uncomment the following line:
	// crossref.Debug()

	// Search for a publication by doing a query. This returns up to 10 works
	// that match the query terms.
	search := "Slow Robots for Unobtrusive Posture Correction"
	works, err := client.Query(search)
	switch err {
	case crossref.ErrZeroWorks:
		fmt.Println("No works found")
	case crossref.ErrEmptyQuery:
		fmt.Println("An empty query was requested")
	case nil:
	default:
		panic(err)
	}

	fmt.Printf("Found %d article(s) for query:\n > %q\n", len(works), search)

	// Retrieve information directly from a DOI
	doi := works[0].DOI
	work, err := client.DOI(doi)
	switch err {
	case crossref.ErrZeroWorks:
		fmt.Println("No works found")
	case crossref.ErrEmptyQuery:
		fmt.Println("An empty query was requested")
	case nil:
	default:
		panic(err)
	}

	fmt.Printf("For DOI: %q found:\n", doi)
	fmt.Printf(" > %q, (%v) %q\n", work.Title, work.Date, work.Authors)

	// Output:
	// Found 10 article(s) for query:
	//  > "Slow Robots for Unobtrusive Posture Correction"
	// For DOI: "10.1145/3290605.3300843" found:
	//  > "Slow Robots for Unobtrusive Posture Correction", (2019) ["Shin, Joon-Gi" "Onchi, Eiji" "Reyes, Maria Jose" "Song, Junbong" "Lee, Uichin" "Lee, Seung-Hee" "Saakes, Daniel"]
}

Documentation

Overview

Package crossref gives native Go access to Crossref's API to request metadata about a work/publication.

Example
package main

import (
	"fmt"

	"github.com/cgxeiji/crossref"
)

var client = crossref.NewClient("Crossref Go", "mail@example.com")

func main() {
	// If you want to debug the library, uncomment the following line:
	// crossref.Debug()

	// Search for a publication by doing a query. This returns up to 10 works
	// that match the query terms.
	search := "Slow Robots for Unobtrusive Posture Correction"
	works, err := client.Query(search)
	switch err {
	case crossref.ErrZeroWorks:
		fmt.Println("No works found")
	case crossref.ErrEmptyQuery:
		fmt.Println("An empty query was requested")
	case nil:
	default:
		panic(err)
	}

	fmt.Printf("Found %d article(s) for query:\n > %q\n", len(works), search)

	// Retrieve information directly from a DOI
	doi := works[0].DOI
	work, err := client.DOI(doi)
	switch err {
	case crossref.ErrZeroWorks:
		fmt.Println("No works found")
	case crossref.ErrEmptyQuery:
		fmt.Println("An empty query was requested")
	case nil:
	default:
		panic(err)
	}

	fmt.Printf("For DOI: %q found:\n", doi)
	fmt.Printf(" > %q, (%v) %q\n", work.Title, work.Date, work.Authors)

}
Output:

Found 10 article(s) for query:
 > "Slow Robots for Unobtrusive Posture Correction"
For DOI: "10.1145/3290605.3300843" found:
 > "Slow Robots for Unobtrusive Posture Correction", (2019) ["Shin, Joon-Gi" "Onchi, Eiji" "Reyes, Maria Jose" "Song, Junbong" "Lee, Uichin" "Lee, Seung-Hee" "Saakes, Daniel"]

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyQuery returns an error if an empty query was requested.
	ErrEmptyQuery = errors.New("empty query requested")
	// ErrZeroWorks returns an error if the query did not find any results.
	ErrZeroWorks = errors.New("no works were found")
)

Functions

func Debug

func Debug()

Debug checks the communication between the library and the API in detail.

Types

type Client

type Client struct {
	http.Client
	// contains filtered or unexported fields
}

Client communicates with CrossRef API and ensures politeness.

func NewClient

func NewClient(appname, malito string) *Client

NewClient returns a new crossref client with an attached mailto and app name. The request timeout is set to 5 seconds.

func (*Client) DOI

func (c *Client) DOI(doi string) (*Work, error)

DOI gets a processed metadata from a DOI.

Example
// Search for a DOI metadata on Crossref.
work, err := client.DOI("10.1145/3290605.3300843")
if err != nil {
	panic(err)
}

fmt.Println(work.Title)

// If no works can be found, DOI returns with ErrZeroWorks error.
_, err = client.DOI("10.1145/3290605.330084") // deleted the last digit
switch err {
case crossref.ErrZeroWorks:
	fmt.Println("No work found")
case crossref.ErrEmptyQuery:
	fmt.Println("An empty DOI was requested")
case nil:
default:
	panic(err)
}
Output:

Slow Robots for Unobtrusive Posture Correction
No work found

func (*Client) DOIJSON

func (c *Client) DOIJSON(doi string) ([]byte, error)

DOIJSON returns the raw JSON of a DOI search. Use this if you want to manually process the JSON data.

func (*Client) Query

func (c *Client) Query(search string) ([]*Work, error)

Query returns a list of processed Work metadata from a search query. A search term must be specified, otherwise it will return an ErrEmptyQuery error. If no works are found, it returns an ErrZeroWorks error.

Example
// Search for a work on Crossref.
works, err := client.Query("Slow Robots for Unobtrusive Posture Correction")
if err != nil {
	panic(err)
}

work := works[0]
fmt.Println(work.DOI)

// If no works can be found, Query returns with ErrZeroWorks error.
_, err = client.Query("jtfiejfrlsadaksljablkjoifajebwoijffal")
switch err {
case crossref.ErrZeroWorks:
	fmt.Println("No works found")
case crossref.ErrEmptyQuery:
	fmt.Println("An empty query was requested")
case nil:
default:
	panic(err)
}
Output:

10.1145/3290605.3300843
No works found

func (*Client) QueryJSON

func (c *Client) QueryJSON(search string) ([]byte, error)

QueryJSON gets the raw JSON from CrossRef. A search term must be specified, otherwise it will return an ErrEmptyQuery error. Use this if you want to manually manipulate the JSON data.

func (*Client) String

func (c *Client) String() string

String implements the Stringer interface.

type Contributor

type Contributor struct {
	// First is the given name of the contributor.
	First string `json:"given"`
	// Last is the family name of the contributor.
	Last string `json:"family"`
}

Contributor saves the name of the author in First Last name.

func (*Contributor) String

func (c *Contributor) String() string

String implements the Stringer interface.

type Work

type Work struct {
	Type string
	DOI  string `json:"DOI"`

	Titles []string `json:"title"`
	Title  string

	BookTitles []string `json:"container-title"`
	BookTitle  string

	Authors []*Contributor `json:"author"`
	Editors []*Contributor `json:"editor"`

	Issued *dateParts `json:"issued"`
	Date   string

	Publisher string `json:"publisher"`

	Issue    string   `json:"issue"`
	Volume   string   `json:"volume"`
	Pages    string   `json:"page"`
	ISSNs    []string `json:"ISSN"`
	ISSN     string
	ISBNs    []string `json:"ISBN"`
	ISBN     string
	Abstract string `json:"abstract"`
}

Work is the processed JSON for direct access to the information.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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