Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions book-fetcher/fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
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 `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 {
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)
}

final := book.Items[0].VolumeInfo.toBook()

err = json.NewEncoder(fil).Encode(&final)

if err != nil {
panic(err)
}
}

}

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,
}
}
1 change: 1 addition & 0 deletions books/alice-in-wonderland.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Alice's Adventures in Wonderland","authors":["Lewis Carroll"],"publisher":"","publishedDate":"1909","description":"","pageCount":0,"categories":null}
1 change: 1 addition & 0 deletions books/gullivers-travels.json
Original file line number Diff line number Diff line change
@@ -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"]}
1 change: 1 addition & 0 deletions books/magic-bites.json
Original file line number Diff line number Diff line change
@@ -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"]}
1 change: 1 addition & 0 deletions books/redwall.json
Original file line number Diff line number Diff line change
@@ -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"]}
1 change: 1 addition & 0 deletions books/the-hunger-games.json
Original file line number Diff line number Diff line change
@@ -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"]}