This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
270 lines (226 loc) · 6.31 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
262
263
264
265
266
267
268
269
270
package main
import (
"bufio"
"encoding/csv"
"encoding/json"
"flag"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
)
type Record struct {
Publication
APIResponses []APIResponse
}
type Publication struct {
Identifier []struct {
Scheme string `json:"scheme"`
Value string `json:"value"`
} `json:"identifier"`
ID string `json:"__id__"`
Type string `json:"type"`
Attachment []struct {
OpenAccess string `json:"open_access"`
BlobKey string `json:"blob_key"`
ExternalURL interface{} `json:"external_url"`
Type string `json:"type"`
} `json:"attachment"`
}
type APIResponse struct {
HTTPStatus string
APIResponseBody
JSONDecodeError string
GETError string
}
type APIResponseBody struct {
BestOaLocation struct {
Evidence string `json:"evidence"`
HostType string `json:"host_type"`
ID string `json:"id"`
URL string `json:"url"`
URLForLandingPage string `json:"url_for_landing_page"`
URLForPdf string `json:"url_for_pdf"`
Version string `json:"version"`
} `json:"best_oa_location"`
DataStandard int `json:"data_standard"`
Doi string `json:"doi"`
DoiURL string `json:"doi_url"`
IsOa bool `json:"is_oa"`
JournalIsOa bool `json:"journal_is_oa"`
JournalIssns string `json:"journal_issns"`
JournalName string `json:"journal_name"`
Publisher string `json:"publisher"`
Title string `json:"title"`
Updated string `json:"updated"`
Year int `json:"year"`
}
const OADOIURL string = "https://api.oadoi.org/v2/"
var attachmentTypeToWeightMap = map[string]int{
"missing": 0,
"other": 1,
"submittedManuscript": 2,
"acceptedManuscript": 3,
"finalVersion": 4,
}
var email = flag.String("email", "", "Email to pass to the oaDOI API")
var httplimit = flag.Int("httplimit", 5, "Number of HTTP requests that can run concurrently")
func findFilesToProcess() []string {
if len(flag.Args()) == 0 {
log.Println("No file names provided, trying to find files ending with Publication-export.json in current working directory.")
workingDir, err := os.Getwd()
if err != nil {
log.Fatalln("Error getting working directory. ", err)
}
matches, err := filepath.Glob(filepath.Join(workingDir, "*Publication-export.json"))
if err != nil {
log.Fatalln("Error finding matching files. ", err)
}
return matches
} else {
return flag.Args()
}
}
func processFile(fileName string) {
file, err := os.Open(fileName)
if err != nil {
log.Println(err)
return
}
defer file.Close()
output := make(chan Record)
ticketToHTTP := make(chan bool, *httplimit)
for i := 0; i < *httplimit; i++ {
ticketToHTTP <- true
}
var waitgroupLines sync.WaitGroup
fileScanner := bufio.NewScanner(file)
buf := make([]byte, 0, 1024*1024)
fileScanner.Buffer(buf, 1024*1024*32)
for fileScanner.Scan() {
waitgroupLines.Add(1)
publicationBytes := append([]byte{}, fileScanner.Bytes()...)
go processPublication(publicationBytes, &waitgroupLines, ticketToHTTP, output)
}
err = fileScanner.Err()
if err != nil {
log.Fatalln(err)
}
var waitgroupOutput sync.WaitGroup
waitgroupOutput.Add(1)
go processOutput(output, &waitgroupOutput)
waitgroupLines.Wait()
close(output)
close(ticketToHTTP)
waitgroupOutput.Wait()
}
func processOutput(output <-chan Record, waitgroupOutput *sync.WaitGroup) {
defer waitgroupOutput.Done()
w := csv.NewWriter(os.Stdout)
header := []string{
"Artudis - ID",
"Artudis - Publication Type",
"Artudis - Available OA",
"Artudis - Best Type OA",
"API - Available OA",
"API - Best OA Location Version",
"API - DOI",
"API - Best OA Location URL",
"API - Title",
"API - HTTP Response Status",
"API - JSON Decode Error",
"API - GET Error",
}
err := w.Write(header)
for record := range output {
artudisOA := false
highestLevel := "missing"
for _, attachment := range record.Attachment {
if attachment.OpenAccess == "true" {
artudisOA = true
if attachmentTypeToWeightMap[attachment.Type] > attachmentTypeToWeightMap[highestLevel] {
highestLevel = attachment.Type
}
}
}
for _, apiresponse := range record.APIResponses {
toCSVOutput := []string{
record.Publication.ID,
record.Publication.Type,
strconv.FormatBool(artudisOA),
highestLevel,
strconv.FormatBool(apiresponse.APIResponseBody.IsOa),
apiresponse.APIResponseBody.BestOaLocation.Version,
apiresponse.APIResponseBody.Doi,
apiresponse.APIResponseBody.BestOaLocation.URL,
apiresponse.APIResponseBody.Title,
apiresponse.HTTPStatus,
apiresponse.JSONDecodeError,
apiresponse.GETError,
}
err := w.Write(toCSVOutput)
if err != nil {
log.Println("error writing record to csv:", err)
return
}
}
}
w.Flush()
err = w.Error()
if err != nil {
log.Println("error writing record to csv:", err)
return
}
}
func processPublication(publicationBytes []byte, waitgroupLines *sync.WaitGroup, ticketToHTTP chan bool, output chan<- Record) {
defer waitgroupLines.Done()
var record Record
err := json.Unmarshal(publicationBytes, &record.Publication)
if err != nil {
log.Println(err)
return
}
for _, identifier := range record.Publication.Identifier {
if identifier.Scheme == "doi" {
record.APIResponses = append(record.APIResponses, doAPIRequest(identifier.Value, ticketToHTTP))
}
}
output <- record
}
func doAPIRequest(doi string, ticketToHTTP chan bool) APIResponse {
defer func() { ticketToHTTP <- true }()
// Wait for ticket
<-ticketToHTTP
var apiResponse APIResponse
url := OADOIURL + strings.TrimPrefix(doi, "http://dx.doi.org/") + "?email=" + *email
resp, err := http.Get(url)
if err != nil {
apiResponse.GETError = err.Error()
return apiResponse
}
defer resp.Body.Close()
apiResponse.HTTPStatus = resp.Status
err = json.NewDecoder(resp.Body).Decode(&apiResponse.APIResponseBody)
if err != nil {
apiResponse.JSONDecodeError = err.Error()
return apiResponse
}
return apiResponse
}
func main() {
flag.Parse()
if *email == "" {
log.Fatal("FATAL: An email is required.")
}
filesToProcess := findFilesToProcess()
if len(filesToProcess) == 0 {
log.Fatalln("Could not find any files to process.")
}
for _, fileName := range filesToProcess {
log.Println("Processing", fileName)
processFile(fileName)
}
}