Skip to content

Commit

Permalink
Changed CrossRef access with crossref library
Browse files Browse the repository at this point in the history
  • Loading branch information
cgxeiji committed Oct 27, 2018
1 parent 11694e2 commit bf8dd4a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
16 changes: 16 additions & 0 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ type Entries struct {
Map map[string]*EntryType
}

func (es *Entries) Parse(service, eType string) *Entry {
switch service {
case "crossref":
switch eType {
case "journal-article":
return es.Map["article"].Get()
case "proceedings-article":
return es.Map["inproceedings"].Get()
default:
return es.Map["article"].Get()
}
}

return &Entry{}
}

func (es *Entries) Load(file string) error {
d, err := ioutil.ReadFile(file)
if err != nil {
Expand Down
62 changes: 40 additions & 22 deletions test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,68 @@ package main

import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"reflect"
"strings"

"github.com/cgxeiji/crossref"
"github.com/cgxeiji/scholar"
"gopkg.in/yaml.v2"
)

var folder string = "ScholarTest"

func AddDOI(es *scholar.Entries, doi string) {
fmt.Println(fmt.Sprintf("https://api.crossref.org/v1/works/%s", doi))
resp, err := http.Get(fmt.Sprintf("https://api.crossref.org/v1/works/%s", doi))
//resp, err := http.Get("https://google.com")
client := crossref.NewClient("Scholar", "mail@example.com")

work, err := client.Works(doi)
if err != nil {
panic(err)
}
defer resp.Body.Close()
raw, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)

entry := es.Parse("crossref", work.Type)

for _, a := range work.Authors {
entry.Required["author"] = fmt.Sprintf("%s%s, %s and ", entry.Required["author"], a.Last, a.First)
}
entry.Required["author"] = strings.TrimSuffix(entry.Required["author"], " and ")
entry.Required["date"] = work.Date
entry.Required["title"] = work.Titles[0]

if entry.Type == "inproceedings" {
entry.Required["booktitle"] = work.BookTitles[0]
} else {
entry.Required["journaltitle"] = work.BookTitles[0]
}

var data map[string]interface{}
json.Unmarshal(raw, &data)
entry.Optional["volume"] = work.Volume
entry.Optional["number"] = work.Issue
entry.Optional["doi"] = work.DOI

// fmt.Println(data)
key := entry.GetKey()
saveTo := filepath.Join(folder, key)

content, _ := data["message"].(map[string]interface{})
fmt.Println(content["type"])
fmt.Println("Title:", content["title"])
fmt.Println("In:", content["container-title"])
// TODO: check for unique key and directory names

fmt.Println("Author:")
authors := reflect.ValueOf(content["author"])
for i := 0; i < authors.Len(); i++ {
author := authors.Index(i).Interface().(map[string]interface{})
fmt.Println(" ", author["given"], author["family"])
err = os.MkdirAll(saveTo, os.ModePerm)
if err != nil {
panic(err)
}

d, err := yaml.Marshal(entry)
if err != nil {
panic(err)
}
fmt.Println(string(d))
ioutil.WriteFile(filepath.Join(saveTo, "entry.yaml"), d, 0644)

var en scholar.Entry
yaml.Unmarshal(d, &en)
fmt.Println(en.Bib())
en.Check()
}

func Add(es *scholar.Entries, entryType string) {
Expand Down Expand Up @@ -138,6 +155,7 @@ func main() {
if *fAdd != "" {
// Add(entries, *fAdd)
AddDOI(entries, "http://dx.doi.org/10.1016/0004-3702(89)90008-8")
AddDOI(entries, "http://dx.doi.org/10.1117/12.969296")
}

if *fExport {
Expand Down

0 comments on commit bf8dd4a

Please sign in to comment.