-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
261 lines (215 loc) · 6.98 KB
/
main.go
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package main
import (
"bufio"
"encoding/csv"
"encoding/xml"
"flag"
"fmt"
"io"
"log"
"os"
"regexp"
"strconv"
"strings"
"time"
)
var etdCSVFilePath = flag.String("in", "etd-output.csv", "Path to etd CSV file.")
var prefix = flag.String("prefix", "", "DOI prefix.")
var depositorName = flag.String("depositor", "", "Name of the organization registering the DOIs. The name placed in this element should match the name under which a depositing organization has registered with CrossRef.")
var depositorEmail = flag.String("email", "", "Email address to which batch success and/or error messages are sent. It is recommended that this address be unique to a position within the organization submitting data (e.g. \"doi@...\") rather than unique to a person. In this way, the alias for delivery of this mail can be changed as responsibility for submission of DOI data within the organization changes from one person to another.")
var registrant = flag.String("registrant", "", "The organization that owns the information being registered.")
var starting = flag.Int("starting", 1, "The starting value for the incrementing integer section of the DOI pattern 'prefix/etd/year-intvalue'")
func main() {
flag.Parse()
if *depositorName == "" {
log.Fatalln("depositor required")
}
if *depositorEmail == "" {
log.Fatalln("email required")
}
if *registrant == "" {
log.Fatalln("registrant required")
}
if *prefix == "" {
log.Fatalln("prefix required")
}
dois := make(map[string]bool)
// Open the ETD export from CURVE.
etdCSVFile, err := os.Open(*etdCSVFilePath)
if err != nil {
log.Fatal(err)
}
etdCSVReader := csv.NewReader(etdCSVFile)
dissertations := []*Dissertation{}
lineNumber := 0
identifier := *starting
for {
lineNumber = lineNumber + 1
record, err := etdCSVReader.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
// If this record already has a DOI, skip it
if strings.Contains(record[4], "https://doi.org") {
continue
}
dissertation := NewDissertation()
dissertation.Title = strings.TrimSpace(record[0])
if dissertation.Title == "" {
log.Fatalf("On line %v: Empty title!", lineNumber)
}
mononymous := false
splitName := strings.Split(record[1], ",")
if len(splitName) < 2 {
log.Printf("Found record with only one name: %v\n", record[1])
if !askForConfirmation("Mononymous name?") {
log.Fatalln("Exit!")
} else {
mononymous = true
}
}
if mononymous {
dissertation.Person.Surname = strings.TrimSpace(splitName[0])
} else {
dissertation.Person.Surname = strings.TrimSpace(splitName[0])
if dissertation.Person.Surname == "" {
log.Fatalf("On line %v: Empty Surname!\n", lineNumber)
}
restOfName := strings.TrimSpace(splitName[1])
dissertation.Person.GivenName = strings.TrimSpace(strings.Split(restOfName, " ")[0])
if dissertation.Person.GivenName == "" {
log.Fatalf("On line %v: Empty GivenName!\n", lineNumber)
}
}
if record[2] == "" {
log.Fatalf("On line %v: Empty Year!\n", lineNumber)
}
dissertation.ApprovalDate.Year = record[2][0:4]
value, err := strconv.Atoi(dissertation.ApprovalDate.Year)
if err != nil {
log.Fatalf("On line %v: Couldn't convert Year to int value!\n", lineNumber)
}
if value < 1930 {
log.Fatalf("On line %v: Likely an invalid year\n", lineNumber)
}
if value > 2999 {
log.Fatalf("On line %v: Likely an invalid year\n", lineNumber)
}
dissertation.DegreeName = strings.TrimSpace(record[3])
if dissertation.DegreeName == "" {
log.Fatalf("On line %v: Empty DegreeName!\n", lineNumber)
}
findProquestIDRegexp := regexp.MustCompile(`pqdiss\: (\w+)\|http`)
regexpResult := findProquestIDRegexp.FindStringSubmatch(record[4])
if len(regexpResult) > 1 {
dissertation.Identifier = &Identifier{
Value: regexpResult[1],
IDType: "dai",
}
}
dissertation.UUID = strings.TrimSpace(record[5])
if dissertation.UUID == "" {
log.Fatalf("On line %v: Empty UUID!", lineNumber)
}
dissertation.URI = "https://curve.carleton.ca/" + dissertation.UUID
dissertation.DOI = fmt.Sprintf("%v/etd/%v-%05v", *prefix, dissertation.ApprovalDate.Year, identifier)
if _, ok := dois[dissertation.DOI]; ok {
log.Fatalln("DOI collision!")
} else {
dois[dissertation.DOI] = true
}
dissertations = append(dissertations, dissertation)
identifier++
}
fullBatches := len(dissertations) / 5000
remainder := len(dissertations) % 5000
batches := []*TemplateData{}
for i := 0; i < fullBatches; i++ {
templateData := NewTemplateData()
runAt := time.Now().UTC()
templateData.Head = HeadData{
DOIBatch: runAt.Unix(),
Timestamp: runAt.UnixNano(),
DepositorName: *depositorName,
DepositorEmail: *depositorEmail,
Registrant: *registrant,
}
templateData.Body.Dissertations = dissertations[i*5000 : ((i + 1) * 5000)]
batches = append(batches, templateData)
time.Sleep(1 * time.Second)
}
if remainder != 0 {
templateData := NewTemplateData()
runAt := time.Now().UTC()
templateData.Head = HeadData{
DOIBatch: runAt.Unix(),
Timestamp: runAt.UnixNano(),
DepositorName: *depositorName,
DepositorEmail: *depositorEmail,
Registrant: *registrant,
}
templateData.Body.Dissertations = dissertations[fullBatches*5000:]
batches = append(batches, templateData)
}
for i, templateData := range batches {
output, err := os.Create(fmt.Sprintf("%v-crossref-%v.xml", time.Now().UTC().Format("2006-01-02"), i+1))
if err != nil {
log.Fatalln(err)
}
defer output.Close()
header := `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
_, err = output.WriteString(header)
if err != nil {
log.Fatalln(err)
}
enc := xml.NewEncoder(output)
enc.Indent("", "\t")
err = enc.Encode(templateData)
if err != nil {
log.Fatalln(err)
}
}
report, err := os.Create(fmt.Sprintf("%v-report.csv", time.Now().UTC().Format("2006-01-02")))
if err != nil {
log.Fatalln(err)
}
defer report.Close()
w := csv.NewWriter(report)
err = w.Write([]string{"uuid", "doi"})
if err != nil {
log.Fatalln("Error writing to csv:", err)
}
for _, dissertation := range dissertations {
err = w.Write([]string{dissertation.UUID, dissertation.DOI})
if err != nil {
log.Fatalln("Error writing to csv:", err)
}
}
w.Flush()
if err := w.Error(); err != nil {
log.Fatalln(err)
}
}
// askForConfirmation asks the user for confirmation. A user must type in "yes" or "no" and
// then press enter. It has fuzzy matching, so "y", "Y", "yes", "YES", and "Yes" all count as
// confirmations. If the input is not recognized, it will ask again. The function does not return
// until it gets a valid response from the user.
func askForConfirmation(s string) bool {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s [y/n]: ", s)
response, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
return false
}
}
}