Skip to content

Commit

Permalink
First working MVP.
Browse files Browse the repository at this point in the history
  • Loading branch information
dalzuga committed Dec 12, 2016
1 parent 69fca21 commit 9b6c81a
Showing 1 changed file with 48 additions and 24 deletions.
72 changes: 48 additions & 24 deletions go_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
)

func main() {
Expand All @@ -23,10 +23,9 @@ func main() {

AuthorID := grbq.Book.Authors[0].ID

fmt.Println("AuthorID:", AuthorID)
// fmt.Println("AuthorID:", AuthorID)

fileBytes, err = ioutil.ReadFile("authorlistbooks.xml")

if err != nil {
log.Fatal(err)
}
Expand All @@ -52,55 +51,80 @@ func main() {
fmt.Println(startBooks, endBooks, totalBooks, totalBooks/endBooks)

/* Code below is for pagination, need to code makeHTTPRequest */
// for totalBooks < endBooks {
// makeHTTPRequest(url string, AuthorID, &graq)
// for _, bookValue := range graq.Author.Books.Book {
// fmt.Println(bookValue.Title)
// }
// }
pageNumber := 1
for totalBooks > endBooks {
fmt.Println("_______________________REQUEST________________________")
makeHTTPRequest("https://www.goodreads.com/author/list.xml", AuthorID, pageNumber, &graq)
startBooks = graq.Author.Books.Start
endBooks = graq.Author.Books.End
totalBooks = graq.Author.Books.Total
for _, bookValue := range graq.Author.Books.Book {
fmt.Println(bookValue.Title)
}
pageNumber++
}

makeHTTPRequest("https://www.goodreads.com/author/list.xml", AuthorID)
fmt.Println("Total requests:", pageNumber-1)
}

func makeHTTPRequest(uri string, AuthorID int) {
/*
* makeHTTPRequest takes the full URL string, makes a request, and parses
* the XML in the response into the struct pointed to by graq
*/
func makeHTTPRequest(uri string, AuthorID int, pageNumber int, graq *GoodReadsAuthorQuery) {

client := &http.Client{}

u, err := url.Parse(uri)
if err != nil {
log.Fatal(err)
}
// fmt.Println("Host:", u.Host)
// u.Scheme = "https"
// u.Host = "goodreads.com"

q := u.Query()
q.Set("key", `kDkKnUxiz8cRBJhVjrtSA`)
q.Set("id", `4`)
q.Set("id", strconv.Itoa(AuthorID))
q.Set("page", strconv.Itoa(pageNumber))

fmt.Println(q.Encode())
// fmt.Println(q.Encode())

u.RawQuery = q.Encode()

fmt.Println(u.Host)
fmt.Println(u.RequestURI())
// fmt.Println(u.Host)
// fmt.Println(u.RequestURI())

fullURL := u.Host + u.RequestURI()
fmt.Println(fullURL)
fullURL := u.String()
// fmt.Println(fullURL)

fmt.Println(u.Scheme)

req, err := http.NewRequest("GET", u.Scheme+"://"+fullURL, nil)
req, err := http.NewRequest("GET", fullURL, nil)
if err != nil {
log.Fatal(err)
}

resp, err := client.Do(req)

if err != nil {
log.Fatal(err)
}

// fmt.Println()
/* Uncomment lines below to dump the http response */
// dump, err := httputil.DumpResponse(resp, true)
// if err != nil {
// log.Fatal(err)
// }
//
// fmt.Printf("%q", dump)

requestBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}

dump, err := httputil.DumpResponse(resp, true)
err = xml.Unmarshal(requestBytes, graq)
if err != nil {
log.Fatal(err)
}

fmt.Printf("%q", dump)
// fmt.Printf("%#v", graq.Author.Books.Book[0].Title)
}

0 comments on commit 9b6c81a

Please sign in to comment.