/
dyn_json_get.go
114 lines (109 loc) · 2.91 KB
/
dyn_json_get.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
// author David Li
// initial implementation of isbn fetching from google
// uses dynamic json parsing
// prefer javascript dynamic json parsing over golang
package fetch
import (
"encoding/json"
"fmt"
"net/http"
"io/ioutil"
"text/template"
"strings"
"bytes"
"reflect"
)
func ListToString(data interface{}) []string {
if reflect.TypeOf(data).Kind() != reflect.Slice {
panic("err: data is not slice")
}
slice := reflect.Indirect(reflect.ValueOf(data))
res := make([]string, slice.Len())
for i := 0; i < slice.Len(); i++ {
a := slice.Index(i).Interface().(string)
res[i] = a
}
return res
}
func GetBookDyn(isbn string) string {
if len(isbn) == 0 {
isbn = "9781451648546"
}
url := "https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn
resp, err := http.Get(url)
if err != nil {
// handle error
}
defer resp.Body.Close()
body, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
fmt.Println(readErr)
}
// dynamically reading json data
var results map[string]interface{}
json.Unmarshal(body, &results)
items, ok := results["items"].([]interface{})
if !ok {
fmt.Println("No Items Found")
return ""
}
book, ok := items[0].(map[string]interface{})
if !ok {
fmt.Println("Not able to find book")
return ""
}
accessInfo := book["accessInfo"].(map[string]interface{})
if accessInfo == nil {
fmt.Println("Not able to find accessInfo")
return ""
}
volumeInfo := book["volumeInfo"].(map[string]interface{})
if volumeInfo == nil {
fmt.Println("Not able to find accessInfo")
return ""
}
fmt.Println(accessInfo)
for key, _ := range results {
fmt.Println("Reading Value for Key :", key)
}
authors := volumeInfo["authors"]
authorsString := ""
if authors != nil {
authors = volumeInfo["authors"].([]interface{})
authorsList := ListToString(authors)
authorsString = strings.Join(authorsList," ")
}
publisher := volumeInfo["publisher"]
publisherString := ""
if publisher != nil {
publisherString = volumeInfo["publisher"].(string)
}
publishedDate := volumeInfo["publishedDate"]
dateString := ""
if publishedDate != nil {
dateString = volumeInfo["publishedDate"].(string)
}
accessString := ""
webReaderLink := accessInfo["webReaderLink"]
if webReaderLink != nil {
accessString = accessInfo["webReaderLink"].(string)
}
// Update text template
// https://dev.to/kirklewis/go-text-template-processing-181d
bookData := BookTex{isbn, authorsString, publisherString, dateString, accessString}
latexRef := `@book{book:{{.ISBN}},
author={{"{"}}{{.Authors}}{{"}"}},
publisher={{"{"}}{{.Publisher}}{{"}"}},
isbn={{"{"}}{{.ISBN}}{{"}"}},
year={{"{"}}{{.PublishedDate}}{{"}"}},
url={{"{"}}{{.URL}}{{"}"}}
}`
tmpl := template.New("tmpl")
//parse some content and generate a template
tmpl, err = tmpl.Parse(latexRef)
var tmplBytes bytes.Buffer
//merge template 'tmpl' with content of 's'
tmpl.Execute(&tmplBytes, bookData)
latexString := tmplBytes.String()
return latexString
}