-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
206 lines (174 loc) · 4.52 KB
/
Copy pathmain.go
File metadata and controls
206 lines (174 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"strings"
"github.com/carlmjohnson/requests"
"github.com/carlmjohnson/versioninfo"
"github.com/charmbracelet/log"
"github.com/nickng/bibtex"
"github.com/sourcegraph/conc/stream"
"github.com/tidwall/gjson"
"github.com/urfave/cli/v3"
"golang.org/x/time/rate"
)
var ErrDOIExists = errors.New("DOI exists")
type WrongTitleError struct {
expected string
actual string
}
func (e WrongTitleError) Error() string {
return fmt.Sprintf("title don't match: expected %q, actual %q", e.expected, e.actual)
}
func main() {
cmd := cli.Command{
Name: "doizer",
Version: versioninfo.Version,
Usage: "Add missing DOIs to your bibtex files.",
UsageText: "doizer -i <INPUT.bib> -o <OUTPUT.bib>",
Description: `For all entries of the input bibtex file with a missing DOI, queries crossref
with the title, authors and date for highest scoring reference and picks its
DOI. Flags any case where title differs.
Developped at github.com/lvignoli/doizer.
`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "input",
Aliases: []string{"i"},
Usage: "input file",
OnlyOnce: true,
Required: true,
TakesFile: true,
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "output file",
OnlyOnce: true,
Required: true,
TakesFile: true,
},
cli.VersionFlag,
},
Action: action,
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}
func action(ctx context.Context, c *cli.Command) error {
inputFile := c.String("input")
f, err := os.Open(inputFile)
if err != nil {
return fmt.Errorf("%s: %w", inputFile, err)
}
refs, err := bibtex.Parse(f)
if err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
s := stream.New()
for i, e := range refs.Entries {
e := e
s.Go(func() stream.Callback {
return func() {
err := process(e)
prefix := fmt.Sprintf("%03d: %s", i, e.CiteName)
log := log.WithPrefix(prefix)
if err == nil {
log.Infof("Got DOI %s", e.Fields["doi"])
} else if errors.Is(err, ErrDOIExists) {
log.Infof("skipped: existing DOI: %s", e.Fields["doi"])
} else if errors.As(err, &WrongTitleError{}) {
log.Infof("Got DOI %s", e.Fields["doi"])
log.Warnf("%v", err.(WrongTitleError))
} else {
log.Errorf("%v", err)
}
}
})
}
s.Wait()
out := refs.PrettyString()
if err := os.WriteFile(c.String("output"), []byte(out), 0755); err != nil {
return err
}
return nil
}
func process(e *bibtex.BibEntry) error {
if _, ok := e.Fields["doi"]; ok {
return ErrDOIExists
}
DOI, err := getDOI(context.Background(), e)
e.Fields["doi"] = bibtex.NewBibConst(DOI)
return err
}
var l = rate.NewLimiter(10, 10)
func RateLimitTransport(ctx context.Context, rt http.RoundTripper) http.RoundTripper {
if rt == nil {
rt = http.DefaultTransport
}
return requests.RoundTripFunc(func(req *http.Request) (res *http.Response, err error) {
if err := l.Wait(ctx); err != nil {
return nil, errors.New("context was cancelled")
}
return rt.RoundTrip(req)
})
}
func bibliographyLikeString(e *bibtex.BibEntry) string {
var fields []string
if t, ok := e.Fields["title"]; ok {
fields = append(fields, t.String())
}
if a, ok := e.Fields["author"]; ok {
fields = append(fields, a.String())
}
if y, ok := e.Fields["year"]; ok {
fields = append(fields, y.String())
}
fields = append([]string{"\""}, fields...)
fields = append(fields, "\"")
return strings.Join(fields, ", ")
}
func getDOI(ctx context.Context, e *bibtex.BibEntry) (string, error) {
title := e.Fields["title"].String()
q := bibliographyLikeString(e)
var s string
r := requests.URL("api.crossref.org/works").
Transport(RateLimitTransport(ctx, nil)).
ParamInt("rows", 5).
Param("query.bibliographic", q).
Param("sort", "score").
Param("order", "desc").
Accept("application/json").
ToString(&s)
if err := r.Fetch(ctx); err != nil {
return "", err
}
bestScore := 0
DOI := ""
titleFromCrossref := ""
gjson.Get(s, "message.items").ForEach(func(key, value gjson.Result) bool {
d := value.Get("DOI").Str
t := value.Get("title.0").Str
score := int(value.Get("score").Int())
if score > bestScore {
bestScore = score
DOI = d
titleFromCrossref = t
}
return true
})
if DOI == "" {
return "", errors.New("no DOI found")
}
if strings.ToLower(titleFromCrossref) != strings.ToLower(title) {
return DOI, WrongTitleError{expected: title, actual: titleFromCrossref}
}
return DOI, nil
}