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 ¶
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 ¶
Types ¶
type Client ¶
Client communicates with CrossRef API and ensures politeness.
func NewClient ¶
NewClient returns a new crossref client with an attached mailto and app name. The request timeout is set to 5 seconds.
func (*Client) DOI ¶
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 ¶
DOIJSON returns the raw JSON of a DOI search. Use this if you want to manually process the JSON data.
func (*Client) Query ¶
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
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.