1
1
package main
2
2
3
3
import (
4
+ "database/sql"
4
5
"encoding/json"
6
+ "encoding/xml"
7
+ "io/ioutil"
5
8
"log"
6
9
"net/http"
10
+ "net/url"
7
11
"text/template"
8
12
9
13
_ "github.com/mattn/go-sqlite3"
@@ -17,18 +21,33 @@ type Page struct {
17
21
}
18
22
19
23
type SearchResult struct {
20
- Title string
21
- Author string
22
- Year string
23
- ID string
24
+ Title string `xml:"title,attr"`
25
+ Author string `xml:"author,attr"`
26
+ Year string `xml:"hyr,attr"`
27
+ ID string `xml:"owi,attr"`
28
+ }
29
+
30
+ type ClassifySearchResponse struct {
31
+ Results []SearchResult `xml:"works>work"`
32
+ }
33
+
34
+ type ClassifyBookResponse struct {
35
+ BookData struct {
36
+ Title string `xml:"title,attr"`
37
+ Author string `xml:"author,attr"`
38
+ ID string `xml:"owi,attr"`
39
+ } `xml:"work"`
40
+ Classification struct {
41
+ MostPopular string `xml:"sfa,attr"`
42
+ } `xml:"recommandations>ddc>mostPopular"`
24
43
}
25
44
26
45
func main () {
27
46
log .SetFlags (log .Ldate | log .Ltime | log .Lmicroseconds | log .Llongfile )
28
47
29
48
templates := template .Must (template .ParseFiles ("templates/index.html" ))
30
49
31
- // db, _ := sql.Open("sqlite3", "dev.db")
50
+ db , _ := sql .Open ("sqlite3" , "dev.db" )
32
51
33
52
http .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
34
53
err := templates .ExecuteTemplate (w , "index.html" , nil )
@@ -38,18 +57,113 @@ func main() {
38
57
})
39
58
40
59
http .HandleFunc ("/search" , func (w http.ResponseWriter , r * http.Request ) {
41
- results := [] SearchResult {
42
- SearchResult { "Nagarahvu" , "T R Subbarao" , "1972" , "51JeF5abJuXT1zoNjlJe" },
43
- SearchResult { "And then ther were none" , "Agatha Christie" , "1939" , "eeSqQGbnJxMgQSk9oCDL" },
44
- SearchResult { "IT" , "Stephen King" , "1986" , "ZOlu3Nvwxz6vlTjDwLtE" },
45
- SearchResult { "Eradu Kanasu" , "Vani" , "1974" , "9YVvLgV8dN60DMg0HTGn" },
60
+ qs := r . FormValue ( "queryString" )
61
+ log . Println ( "/search => qs = " , qs )
62
+ results , err := search ( qs )
63
+ if err != nil {
64
+ http . Error ( w , err . Error (), http . StatusInternalServerError )
46
65
}
47
66
encoder := json .NewEncoder (w )
48
- err := encoder .Encode (results )
67
+ err = encoder .Encode (results )
68
+ if err != nil {
69
+ http .Error (w , err .Error (), http .StatusInternalServerError )
70
+ }
71
+ })
72
+
73
+ http .HandleFunc ("/books/add" , func (w http.ResponseWriter , r * http.Request ) {
74
+ qs := r .FormValue ("id" )
75
+ log .Println ("/book/add => qs = " , qs )
76
+
77
+ book , err := find (qs )
78
+ if err != nil {
79
+ log .Println ("/books/add qs = " , qs , " error while finding " , " error = " , err .Error ())
80
+ http .Error (w , err .Error (), http .StatusInternalServerError )
81
+ }
82
+
83
+ err = db .Ping ()
84
+ if err != nil {
85
+ log .Println ("/books/add qs = " , qs , " DB not connected" )
86
+ http .Error (w , err .Error (), http .StatusInternalServerError )
87
+ }
88
+
89
+ _ , err = db .Exec ("insert into books (pk, title, author, id, classification) values (?, ?, ?, ?, ?)" ,
90
+ nil , book .BookData .Title , book .BookData .Author , book .BookData .ID , book .Classification .MostPopular )
91
+ if err != nil {
92
+ log .Println ("/books/add qs = " , qs , " error while inserting into DB error = " , err .Error ())
93
+ http .Error (w , err .Error (), http .StatusInternalServerError )
94
+ }
95
+
96
+ log .Println ("/books/add qs = " , qs , " successfully inserted into db" )
97
+
98
+ encoder := json .NewEncoder (w )
99
+ err = encoder .Encode (book )
49
100
if err != nil {
50
101
http .Error (w , err .Error (), http .StatusInternalServerError )
51
102
}
52
103
})
53
104
54
105
log .Println (http .ListenAndServe (port , nil ))
55
106
}
107
+
108
+ func search (query string ) (results []SearchResult , err error ) {
109
+ var searchURL = "http://classify.oclc.org/classify2/Classify?&summary=true&title="
110
+ var body []byte
111
+ var csr ClassifySearchResponse
112
+
113
+ searchURL = searchURL + url .QueryEscape (query )
114
+
115
+ body , err = classifyAPI (searchURL )
116
+ if err != nil {
117
+ log .Println ("func search :: err while requesting " , "url = " , searchURL , " error = " , err .Error ())
118
+ return
119
+ }
120
+
121
+ err = xml .Unmarshal (body , & csr )
122
+ if err != nil {
123
+ log .Println ("func search :: err while Unmarshalling " , "url = " , searchURL , " error = " , err .Error ())
124
+ return
125
+ }
126
+ results = csr .Results
127
+
128
+ return
129
+ }
130
+
131
+ func find (id string ) (cbr ClassifyBookResponse , err error ) {
132
+ var searchURL = "http://classify.oclc.org/classify2/Classify?&summary=true&owi="
133
+ var body []byte
134
+
135
+ searchURL = searchURL + url .QueryEscape (id )
136
+
137
+ body , err = classifyAPI (searchURL )
138
+ if err != nil {
139
+ log .Println ("func find :: err while requesting " , "url = " , searchURL , " error = " , err .Error ())
140
+ return
141
+ }
142
+
143
+ err = xml .Unmarshal (body , & cbr )
144
+ if err != nil {
145
+ log .Println ("func find :: err while Unmarshalling " , "url = " , searchURL , " error = " , err .Error ())
146
+ return
147
+ }
148
+
149
+ return
150
+ }
151
+
152
+ func classifyAPI (url string ) (body []byte , err error ) {
153
+ var resp * http.Response
154
+
155
+ resp , err = http .Get (url )
156
+ if err != nil {
157
+ log .Println ("func classifyAPI :: err while requesting " , "url = " , url , " error = " , err .Error ())
158
+ return
159
+ }
160
+ defer resp .Body .Close ()
161
+
162
+ body , err = ioutil .ReadAll (resp .Body )
163
+ if err != nil {
164
+ log .Println ("func classifyAPI :: err while parsing the body " , "url = " , url , " error = " , err .Error ())
165
+ return
166
+ }
167
+
168
+ return
169
+ }
0 commit comments