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
|
// Generate CSV from Google Books queries.
// authors, title, publishedDate, publisher, ISBN-13, ISBN-10, URL
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"log"
"os"
"strings"
)
func readFile(name string) []byte {
in, err := os.Open(name)
if err != nil {
log.Fatal(err)
}
defer in.Close()
body, err := io.ReadAll(in)
if err != nil {
log.Fatal(err)
}
return body
}
func write(writer *bufio.Writer, contents string) {
_, err := writer.Write([]byte(contents))
if err != nil {
log.Fatal(err)
}
}
/* Unnecessary complication.
Only one entry needs to be in memory at a time.
type Entry struct {
authors: string
title: string
publishedDate: string
isbn13: string
isbn10: string
url: string
}*/
func main() {
// Read a list of file names, named after google books queries
var fullTitle, authors, publishedDate, isbn13, isbn10, url string
for _, title := range strings.Split(string(readFile("list")), "\n") {
if (len(title) < 3) {
continue
}
fmt.Println(title)
// For each title, create csv file
var out *os.File
_, err := os.Stat(title + ".csv")
if errors.Is(err, fs.ErrNotExist) {
//log.Print(err)
out, err = os.Create(title + ".csv")
if err != nil {
log.Fatal(err)
}
} else if err != nil {
log.Fatal(err)
} else {
out, err = os.Open(title + ".csv")
if err != nil {
log.Fatal(err)
}
}
defer out.Close()
outWriter := bufio.NewWriter(out)
//outWriter := bufio.NewWriter(os.Stdout)
// Print CSV header
write(outWriter, "Title, Authors, Published Date, ISBN-13, ISBN-10, URL\n")
defer outWriter.Flush()
// In each provided file, JSON results
rawJson := readFile(title)
var data interface{}
// Parse the JSON into an associative array
json.Unmarshal(rawJson, &data)
// Get through the cruft
m := data.(map[string]interface{})
for _, subm := range m["items"].([]interface{}) {
switch subm.(type) {
case map[string]interface{}:
m := subm.(map[string]interface{})
m = m["volumeInfo"].(map[string]interface{})
isbns := m["industryIdentifiers"].([]interface{})
for _, v := range isbns {
isbn := v.(map[string]interface{})
isbnText := isbn["identifier"].(string)
// ISBN-10 is sometimes 9 digits, ISBN-13 is always 13 digits
if len(isbnText) == 13 {
isbn13 = isbnText
} else {
isbn10 = isbnText
}
}
if m["authors"] != nil {
authorsMap := m["authors"].([]interface{})
for i, author := range authorsMap {
authorText := author.(string)
if i > 0 {
authors += " & "
authors += authorText
} else {
authors = authorText
}
}
}
fullTitle = strings.Replace(m["title"].(string), ",", "", -1)
if m["publishedDate"] != nil {
publishedDate = m["publishedDate"].(string)
}
url = m["infoLink"].(string)
default:
}
write(outWriter, fmt.Sprintf("%s,%s,%s,%s,%s,%s\n", fullTitle, authors, publishedDate, isbn13, isbn10, url))
}
}
}
|