...

Source file src/google.golang.org/api/examples/books.go

Documentation: google.golang.org/api/examples

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"log"
    10  	"net/http"
    11  	"os"
    12  	"strconv"
    13  	"strings"
    14  
    15  	books "google.golang.org/api/books/v1"
    16  )
    17  
    18  func init() {
    19  	registerDemo("books", books.BooksScope, booksMain)
    20  }
    21  
    22  // booksMain is an example that demonstrates calling the Books API.
    23  //
    24  // Example usage:
    25  //
    26  //	go build -o go-api-demo *.go
    27  //	go-api-demo -clientid="my-clientid" -secret="my-secret" books
    28  func booksMain(client *http.Client, argv []string) {
    29  	if len(argv) != 0 {
    30  		fmt.Fprintln(os.Stderr, "Usage: books")
    31  		return
    32  	}
    33  
    34  	svc, err := books.New(client)
    35  	if err != nil {
    36  		log.Fatalf("Unable to create Books service: %v", err)
    37  	}
    38  
    39  	bs, err := svc.Mylibrary.Bookshelves.List().Do()
    40  	if err != nil {
    41  		log.Fatalf("Unable to retrieve mylibrary bookshelves: %v", err)
    42  	}
    43  
    44  	if len(bs.Items) == 0 {
    45  		log.Fatal("You have no bookshelves to explore.")
    46  	}
    47  	for _, b := range bs.Items {
    48  		// Note that sometimes VolumeCount is not populated, so it may erroneously say '0'.
    49  		log.Printf("You have %v books on bookshelf %q:", b.VolumeCount, b.Title)
    50  
    51  		// List the volumes on this shelf.
    52  		vol, err := svc.Mylibrary.Bookshelves.Volumes.List(strconv.FormatInt(b.Id, 10)).Do()
    53  		if err != nil {
    54  			log.Fatalf("Unable to retrieve mylibrary bookshelf volumes: %v", err)
    55  		}
    56  		for _, v := range vol.Items {
    57  			var s []string
    58  			if v.VolumeInfo.ReadingModes.Image {
    59  				s = append(s, "image")
    60  			} else {
    61  				s = append(s, "text")
    62  			}
    63  			extra := fmt.Sprintf("; formats: %v", s)
    64  			if v.VolumeInfo.ImageLinks != nil {
    65  				extra += fmt.Sprintf("; thumbnail: %v", v.VolumeInfo.ImageLinks.Thumbnail)
    66  			}
    67  			log.Printf("  %q by %v%v", v.VolumeInfo.Title, strings.Join(v.VolumeInfo.Authors, ", "), extra)
    68  		}
    69  	}
    70  }
    71  

View as plain text