Skip to content

Commit

Permalink
Playing around with data query
Browse files Browse the repository at this point in the history
  • Loading branch information
cgxeiji committed Oct 26, 2018
1 parent 38506ea commit a15dca5
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 45 deletions.
19 changes: 19 additions & 0 deletions ScholarTest/author2018/entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
type: book
key: author2018
req:
author: Author, This
date: "2018"
title: The Book of Things
opt:
doi: ""
editor: ""
isbn: ""
language: ""
location: ""
number: ""
pages: ""
publisher: ""
series: ""
url: ""
urldate: ""
volume: ""
17 changes: 17 additions & 0 deletions ScholarTest/last2009/entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type: article
key: last2009
req:
author: Last, First
date: "2009"
journaltitle: A Journal of Things
title: The Title
opt:
doi: ""
editor: ""
issn: ""
language: ""
number: ""
series: ""
url: ""
urldate: ""
volume: ""
148 changes: 148 additions & 0 deletions entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package scholar

import (
"fmt"
"io/ioutil"
"sort"
"strings"
"time"

yaml "gopkg.in/yaml.v2"
)

type EntryType struct {
Type string
Description string `yaml:"desc"`
Required map[string]string `yaml:"req"`
Optional map[string]string `yaml:"opt"`
}

func (e *EntryType) Get() *Entry {
var c Entry
c.Type = e.Type
c.Required = make(map[string]string)
for k, _ := range e.Required {
c.Required[k] = ""
}

c.Optional = make(map[string]string)
for k, _ := range e.Optional {
c.Optional[k] = ""
}

return &c
}

func (e *EntryType) Show(level int) {
fmt.Println(e.Type, ":", e.Description)

if level > 0 {
var fields []string
for f := range e.Required {
fields = append(fields, f)
}
sort.Strings(fields)
for _, field := range fields {
fmt.Println(" ", field, "->", e.Required[field])
}

if level > 1 {
fields = nil
for f := range e.Optional {
fields = append(fields, f)
}
sort.Strings(fields)
for _, field := range fields {
fmt.Printf(" (%v) -> %v\n", field, e.Optional[field])
}
}
}
}

type Entry struct {
Type string `yaml:"type"`
Key string `yaml:"key"`
Required map[string]string `yaml:"req"`
Optional map[string]string `yaml:"opt"`
}

func (e *Entry) Check() error {
date := e.Required["date"]
_, err := time.Parse("2006-01-02", date)
if err != nil {
_, err = time.Parse("2006-01", date)
if err != nil {
_, err = time.Parse("2006", date)
if err != nil {
return fmt.Errorf("invalid date format (date %s). Please use YYYY[-MM[-DD]]", date)
}
}
}

return nil
}

func (e *Entry) Year() string {
return e.Required["date"][:4]
}

func (e *Entry) FirstAuthorLast() string {
return strings.Split(e.Required["author"], ",")[0]
}

func (e *Entry) GetKey() string {
if e.Key == "" {
e.Key = fmt.Sprintf("%s%s", strings.ToLower(e.FirstAuthorLast()), e.Year())
}
return e.Key
}

func (e *Entry) Bib() string {
bib := fmt.Sprintf("@%s{%s,\n", e.Type, e.GetKey())
for field, value := range e.Required {
if value != "" {
bib = fmt.Sprintf("%s %s = {%s},\n", bib, field, value)
}
}
for field, value := range e.Optional {
if value != "" {
bib = fmt.Sprintf("%s %s = {%s},\n", bib, field, value)
}
}
bib = fmt.Sprintf("%s}", bib)
return bib
}

type Entries struct {
Map map[string]*EntryType
}

func (es *Entries) Load(file string) error {
d, err := ioutil.ReadFile(file)
if err != nil {
return err
}

err = yaml.Unmarshal(d, &es.Map)
if err != nil {
return err
}

for name, entry := range es.Map {
entry.Type = name
}

return nil
}

func (es *Entries) Show(level int) {
var eNames []string
for name := range es.Map {
eNames = append(eNames, name)
}
sort.Strings(eNames)
for _, name := range eNames {
es.Map[name].Show(level)
fmt.Println()
}
}
17 changes: 17 additions & 0 deletions entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type: article
key: customKey
req:
author: Last, First
date: 2010-12
journaltitle: A Journal of Something
title: The Title
opt:
doi: ""
editor: Editing Company
issn: ""
language: "en"
number: ""
series: ""
url: ""
urldate: ""
volume: ""
45 changes: 0 additions & 45 deletions scholar.go

This file was deleted.

147 changes: 147 additions & 0 deletions test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package main

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

"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")
if err != nil {
panic(err)
}
defer resp.Body.Close()
raw, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}

var data map[string]interface{}
json.Unmarshal(raw, &data)

// fmt.Println(data)

content, _ := data["message"].(map[string]interface{})
fmt.Println(content["type"])
fmt.Println("Title:", content["title"])
fmt.Println("In:", content["container-title"])

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"])
}
}

func Add(es *scholar.Entries, entryType string) {

entry := es.Map[entryType].Get()

reader := bufio.NewReader(os.Stdin)
for field, _ := range entry.Required {
fmt.Printf("%v: ", field)
text, _ := reader.ReadString('\n')
text = strings.Trim(text, " \n")
entry.Required[field] = text
}

// entry.Required["author"] = "Last, First"
// entry.Required["date"] = "2010-12"
// entry.Required["title"] = "The Title"
// entry.Required["journaltitle"] = "A Journal of Something"

// entry.Optional["editor"] = "Editing Company"

key := entry.GetKey()
saveTo := filepath.Join(folder, key)

// TODO: check for unique key and directory names

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 Export() {
dirs, err := ioutil.ReadDir(folder)
if err != nil {
panic(err)
}

for _, dir := range dirs {
if dir.IsDir() {
d, err := ioutil.ReadFile(filepath.Join(folder, dir.Name(), "entry.yaml"))
if err != nil {
panic(err)
}

var e scholar.Entry
err = yaml.Unmarshal(d, &e)
if err != nil {
panic(err)
}

fmt.Println(e.Bib())
fmt.Println()
}
}
}

func main() {
fPrintEntryTypes := flag.Bool("types", false, "Show available entry types")
fPrintEntryLevel := flag.Int("level", 0, "Set the level of information to be shown")
fAdd := flag.String("add", "", "Add a new entry")
fExport := flag.Bool("export", false, "Export entries to biblatex")

flag.Parse()

entries := &scholar.Entries{}

err := entries.Load("types.yaml")
if err != nil {
panic(err)
}

if *fPrintEntryTypes {
entries.Show(*fPrintEntryLevel)
}

if *fAdd != "" {
// Add(entries, *fAdd)
AddDOI(entries, "http://dx.doi.org/10.1016/0004-3702(89)90008-8")
}

if *fExport {
Export()
}

}

0 comments on commit a15dca5

Please sign in to comment.