From 50125368ccbc31dbb065e82106954de7b6733de1 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 1 Oct 2019 16:18:20 -0500 Subject: [PATCH 1/4] add google books data fetcher * still need to convert to requried structure * dates can be wrong depending on version publication --- book-fetcher/fetcher.go | 92 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 book-fetcher/fetcher.go diff --git a/book-fetcher/fetcher.go b/book-fetcher/fetcher.go new file mode 100644 index 0000000..1af24a1 --- /dev/null +++ b/book-fetcher/fetcher.go @@ -0,0 +1,92 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "net/http" + "os" + "strings" + "time" +) + +const apiURL = "https://www.googleapis.com/books/v1/volumes" + +var cli = http.Client{ + Timeout: time.Duration(5 * time.Second), +} + +type Book struct { + title string +} + +type GoogleBook struct { + VolumeInfo VolumeInfo `json:"volumeInfo"` +} + +type GoogleBookResponse struct { + Items []GoogleBook `json:"items"` +} + +type VolumeInfo struct { + Title string `json:"title"` + Authors []string `json:"authors"` + Publisher string `json:"publisher"` + PublishedOn string `json:"publishedDate"` + Description string `json:"description"` + Pages int `json:"pageCount"` + Categories []string `json:"categories"` +} + +func main() { + // get os args + if len(os.Args) < 2 { + log.Println("No book title given") + } + + titles := make([]string, 0) + + for i := 1; i < len(os.Args); i++ { + titles = append(titles, os.Args[i]) + } + + for _, title := range titles { + searchtitle := strings.ReplaceAll(title, "-", "%20") + queryString := fmt.Sprintf("?q=%s&maxResults=1", searchtitle) + log.Printf("using queryString of %s", queryString) + resp, err := cli.Get(apiURL + queryString) + + log.Printf("got status of %s", resp.Status) + + if err != nil { + panic(err) + } + + book := GoogleBookResponse{} + + err = json.NewDecoder(resp.Body).Decode(&book) + + if err != nil { + panic(err) + } + + if len(book.Items) != 1 { + log.Println(book.Items) + panic("bad length of items") + } + + fil, err := os.Create(fmt.Sprintf("../books/%s.json", title)) + defer fil.Close() + + if err != nil { + panic(err) + } + + err = json.NewEncoder(fil).Encode(&book.Items[0].VolumeInfo) + + if err != nil { + panic(err) + } + } + +} From 5273f957789a12bf7f9afd251b2d8f7f8387d15d Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 1 Oct 2019 17:19:08 -0500 Subject: [PATCH 2/4] changes books into correct json format --- book-fetcher/fetcher.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/book-fetcher/fetcher.go b/book-fetcher/fetcher.go index 1af24a1..cf12cac 100644 --- a/book-fetcher/fetcher.go +++ b/book-fetcher/fetcher.go @@ -17,7 +17,13 @@ var cli = http.Client{ } type Book struct { - title string + Title string `json:"title"` + Authors []string `json:"authors"` + Publisher string `json:"publisher"` + PublishedOn string `json:"publishedDate"` + Description string `json:"description"` + Pages int `json:"pageCount"` + Categories []string `json:"categories"` } type GoogleBook struct { @@ -82,7 +88,9 @@ func main() { panic(err) } - err = json.NewEncoder(fil).Encode(&book.Items[0].VolumeInfo) + final := book.Items[0].VolumeInfo.toBook() + + err = json.NewEncoder(fil).Encode(&final) if err != nil { panic(err) @@ -90,3 +98,15 @@ func main() { } } + +func (vi VolumeInfo) toBook() Book { + return Book{ + Title: vi.Title, + Authors: vi.Authors, + Publisher: vi.Publisher, + PublishedOn: vi.PublishedOn, + Description: vi.Description, + Pages: vi.Pages, + Categories: vi.Categories, + } +} From 63d548ec1369d5110c78689afe03909a45d426f9 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 1 Oct 2019 17:23:34 -0500 Subject: [PATCH 3/4] add missing books *adds Snow Crash *adds Magic Bites *adds Redwall *adds Gulliver's Travels *adds The Hunger Games *adds Alice in Wonderland The published date will reflect the editions publication date, not the original --- books/alice-in-wonderland.json | 1 + books/gullivers-travels.json | 1 + books/magic-bites.json | 1 + books/redwall.json | 1 + books/snow-crash.json | 1 + books/the-hunger-games.json | 1 + 6 files changed, 6 insertions(+) create mode 100644 books/alice-in-wonderland.json create mode 100644 books/gullivers-travels.json create mode 100644 books/magic-bites.json create mode 100644 books/redwall.json create mode 100644 books/snow-crash.json create mode 100644 books/the-hunger-games.json diff --git a/books/alice-in-wonderland.json b/books/alice-in-wonderland.json new file mode 100644 index 0000000..d0bb1e1 --- /dev/null +++ b/books/alice-in-wonderland.json @@ -0,0 +1 @@ +{"title":"Alice's Adventures in Wonderland","authors":["Lewis Carroll"],"publisher":"","publishedDate":"1909","description":"","pageCount":0,"categories":null} diff --git a/books/gullivers-travels.json b/books/gullivers-travels.json new file mode 100644 index 0000000..fbab740 --- /dev/null +++ b/books/gullivers-travels.json @@ -0,0 +1 @@ +{"title":"Gulliver's Travels","authors":["D. D Jonathan Swift"],"publisher":"SC Active Business Development Srl","publishedDate":"2016-10-12","description":"Gulliver's Travels into Several Remote Nations of the World, is both a satire on human nature and a parody of the \"travellers' tales\" literary sub-genre. It is Swift's most celebrated work, as well as one of the indisputable classics of English literature. This is probably one of the most beautiful digital version ever made of this story, containing many gorgeous illustrations.","pageCount":176,"categories":["Juvenile Fiction"]} diff --git a/books/magic-bites.json b/books/magic-bites.json new file mode 100644 index 0000000..c26e28c --- /dev/null +++ b/books/magic-bites.json @@ -0,0 +1 @@ +{"title":"Magic Bites","authors":["Ilona Andrews"],"publisher":"Penguin","publishedDate":"2012-12-31","description":"New York Times bestselling author Ilona Andrews invites you to experience the first novel in the “intriguing world” (Locus) of Kate Daniels with this special edition of Magic Bites... Kate Daniels is a down-on-her-luck mercenary who makes her living cleaning up magical problems. But when Kate’s guardian is murdered, her quest for justice draws her into a power struggle between two strong factions within Atlanta’s magic circles. Pressured by both sides to find the killer, Kate realizes she’s way out of her league—but she wouldn’t want it any other way… This special edition includes in-depth information about the world of Kate Daniels, with descriptions of its characters and factions. Explore Kate’s Atlanta like never before with answers to FAQ and a quiz to find your place there. And don’t miss the prequel story “A Questionable Client,” as well as scenes of events in Magic Bites from Curran’s point of view.","pageCount":384,"categories":["Fiction"]} diff --git a/books/redwall.json b/books/redwall.json new file mode 100644 index 0000000..eeb0945 --- /dev/null +++ b/books/redwall.json @@ -0,0 +1 @@ +{"title":"Mossflower","authors":["Brian Jacques"],"publisher":"Penguin","publishedDate":"2002-09-30","description":"The thrilling prequel to \"Redwall\". The clever and greedy wildcat Tsarmina becomes ruler of all Mossflower Woods and is determined to govern the peaceful woodlanders with an iron paw. The brave mouse Martin and quick-talking mouse thief Gonff meet in the depths of Kotir Castle's dungeon. The two escape and resolve to end Tsarmina's tyrannical rule. Joined by Kinny the mole, Martin and Gonff set off on a dangerous quest for Salamandastron, where they are convinced that their only hope, Boar the Fighter, still lives.","pageCount":432,"categories":["Juvenile Fiction"]} diff --git a/books/snow-crash.json b/books/snow-crash.json new file mode 100644 index 0000000..c22c87c --- /dev/null +++ b/books/snow-crash.json @@ -0,0 +1 @@ +{"title":"Snow Crash","authors":["Neal Stephenson"],"publisher":"Penguin UK","publishedDate":"1994-10-27","description":"The only relief from the sea of logos is within the well-guarded borders of the Burbclaves. Is it any wonder that most sane folks have forsaken the real world and chosen to live in the computer-generated universe of virtual reality? In a major city, the size of a dozen Manhattans, is a domain of pleasures limited only by the imagination. But now a strange new computer virus called Snow Crash is striking down hackers everywhere, leaving an unlikely young man as humankind's last best hope.","pageCount":448,"categories":["Fiction"]} diff --git a/books/the-hunger-games.json b/books/the-hunger-games.json new file mode 100644 index 0000000..cd5391d --- /dev/null +++ b/books/the-hunger-games.json @@ -0,0 +1 @@ +{"title":"The Hunger Games","authors":["Suzanne Collins"],"publisher":"","publishedDate":"2011","description":"Set in a dark vision of the near future, a terrifying reality TV show is taking place. Twelve boys and twelve girls are forced to appear in a live event called The Hunger Games. There is only one rule: kill or be killed. When sixteen-year-old Katniss Everdeen steps forward to take her younger sister's place in the games, she sees it as a death sentence. But Katniss has been close to death before. For her, survival is second nature.","pageCount":1392,"categories":["Competition"]} From 0ccf4c06b331a02f324f433581fd2c364edda625 Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Wed, 2 Oct 2019 12:49:25 +0530 Subject: [PATCH 4/4] Delete snow-crash.json --- books/snow-crash.json | 1 - 1 file changed, 1 deletion(-) delete mode 100644 books/snow-crash.json diff --git a/books/snow-crash.json b/books/snow-crash.json deleted file mode 100644 index c22c87c..0000000 --- a/books/snow-crash.json +++ /dev/null @@ -1 +0,0 @@ -{"title":"Snow Crash","authors":["Neal Stephenson"],"publisher":"Penguin UK","publishedDate":"1994-10-27","description":"The only relief from the sea of logos is within the well-guarded borders of the Burbclaves. Is it any wonder that most sane folks have forsaken the real world and chosen to live in the computer-generated universe of virtual reality? In a major city, the size of a dozen Manhattans, is a domain of pleasures limited only by the imagination. But now a strange new computer virus called Snow Crash is striking down hackers everywhere, leaving an unlikely young man as humankind's last best hope.","pageCount":448,"categories":["Fiction"]}