googlebooks.go 5.13 KB
Newer Older
ale's avatar
ale committed
1 2 3 4 5 6
package liber

import (
	"crypto/md5"
	"encoding/xml"
	"errors"
7
	"flag"
ale's avatar
ale committed
8 9 10 11 12 13 14 15 16 17
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"path/filepath"
	"strings"
	"time"
)

18 19
var googleAPIKey = flag.String("google-api-key", "", "Google API key")

20 21 22 23 24 25
type googleBooksRefiner struct{}

func (r *googleBooksRefiner) Name() string {
	return "gbooks"
}

ale's avatar
ale committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
type atomResultEntry struct {
	GoogleId    string   `xml:"id"`
	Title       string   `xml:"http://purl.org/dc/terms title"`
	Date        string   `xml:"http://purl.org/dc/terms date"`
	Creator     []string `xml:"http://purl.org/dc/terms creator"`
	Description []string `xml:"http://purl.org/dc/terms description"`
	Language    []string `xml:"http://purl.org/dc/terms language"`
	Identifier  []string `xml:"http://purl.org/dc/terms identifier"`
	Publisher   []string `xml:"http://purl.org/dc/terms publisher"`
	Format      []string `xml:"http://purl.org/dc/terms format"`
}

func (e *atomResultEntry) ToMetadata() *Metadata {
	m := &Metadata{
		Title:     e.Title,
		Date:      toYear(e.Date),
		Creator:   e.Creator,
		Language:  e.Language,
		Publisher: e.Publisher,
		Format:    e.Format,
		Sources: []MetadataSource{{
			Name: "gbooks",
			ID:   e.GoogleId,
		}},
	}
	if len(e.Description) > 0 {
		m.Description = e.Description[0]
	}
	for _, id := range e.Identifier {
		if strings.HasPrefix(id, "ISBN:") {
			m.ISBN = append(m.ISBN, strings.TrimPrefix(id, "ISBN:"))
		}
	}
	return m
}

type atomResult struct {
	XMLName xml.Name           `xml:"http://www.w3.org/2005/Atom feed"`
	Entries []*atomResultEntry `xml:"entry"`
}

// Build an appropriate query for Google Books, trying to be as
// specific as possible based on the provided metadata.
func googleBooksQuery(m *Metadata) string {
	var query []string

	// If the ISBN is specified, query it directly.
	if len(m.ISBN) > 0 {
		for _, isbn := range m.ISBN {
			query = append(query, fmt.Sprintf("isbn:%s", isbn))
		}
		return strings.Join(query, " ")
	}

	// If we have explicitly tagged metadata, use it.
	if len(m.Title) > 0 {
		for _, s := range strings.Fields(m.Title) {
			query = append(query, fmt.Sprintf("intitle:%s", s))
		}
	}
	if len(m.Creator) > 0 {
		for _, author := range m.Creator {
			for _, s := range strings.Fields(author) {
				query = append(query, fmt.Sprintf("inauthor:%s", s))
			}
		}
	}
	if query != nil {
		return strings.Join(query, "+")
	}

	// Try to make a generic query using keywords.
	if len(m.Keywords) > 0 {
		return strings.Join(m.Keywords, " ")
	}

	return ""
}

// Parse the ATOM response and decode metadata.
func googleBooksParse(in io.Reader) ([]*Metadata, error) {
	var result atomResult
	if err := xml.NewDecoder(in).Decode(&result); err != nil {
		return nil, err
	}
	var out []*Metadata
	for _, e := range result.Entries {
		out = append(out, e.ToMetadata())
	}
	return out, nil
}

// Limit the number of concurrent queries to Google.
var gbSemaphore = make(chan bool, 3)

// Make a HTTP GET query to Google Books, with exponential backoff in
// case of rate limiting.
func googleBooksGet(uri string) (*http.Response, error) {
	gbSemaphore <- true
	defer func() {
		<-gbSemaphore
	}()

129 130
	// Set a deadline to 10 minutes.
	timeout := 900 * time.Second
ale's avatar
ale committed
131
	backoff := 500 * time.Millisecond
132 133
	maxBackoff := 60 * time.Second
	deadline := time.Now().Add(timeout)
ale's avatar
ale committed
134 135 136 137 138 139 140 141 142 143 144
	for time.Now().Before(deadline) {
		resp, err := http.Get(uri)
		if err != nil {
			return nil, err
		}
		switch resp.StatusCode {
		case 403:
			// We've been ratelimited. Back off.
			resp.Body.Close()
			time.Sleep(backoff)
			backoff += backoff / 2
145 146 147
			if backoff > maxBackoff {
				backoff = maxBackoff
			}
ale's avatar
ale committed
148 149 150 151 152 153 154 155 156 157 158
			continue
		case 200:
			return resp, nil
		default:
			resp.Body.Close()
			return nil, fmt.Errorf("HTTP status %s", resp.Status)
		}
	}
	return nil, errors.New("deadline exceeded")
}

159
func (r *googleBooksRefiner) Lookup(m *Metadata) ([]*Metadata, error) {
ale's avatar
ale committed
160 161 162 163 164 165
	qstr := googleBooksQuery(m)
	if qstr == "" {
		return nil, errors.New("insufficient metadata for query")
	}
	values := make(url.Values)
	values.Set("q", qstr)
166 167 168
	if *googleAPIKey != "" {
		values.Set("key", *googleAPIKey)
	}
ale's avatar
ale committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	uri := "http://books.google.com/books/feeds/volumes?" + values.Encode()
	resp, err := googleBooksGet(uri)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	return googleBooksParse(resp.Body)
}

func getGoogleBooksId(m *Metadata) string {
	for _, s := range m.Sources {
		if s.Name == "gbooks" {
			return filepath.Base(s.ID)
		}
	}
	return ""
}

// MD5 of the "image not available" placeholder image served by Google
// Books. We'd like to avoid it using space on the filesystem.
var imageUnavailableMD5 = [16]byte{
	0x0d, 0xe4, 0x38, 0x3e, 0xba, 0xd0, 0xad, 0xad, 0x5e, 0xeb, 0x89, 0x75, 0xcd, 0x79, 0x66, 0x57,
}

193
func (r *googleBooksRefiner) GetBookCover(m *Metadata) ([]byte, error) {
ale's avatar
ale committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	gbid := getGoogleBooksId(m)
	if gbid == "" {
		return nil, errors.New("no ID")
	}

	uri := fmt.Sprintf("http://books.google.com/books?id=%s&printsec=frontcover&img=1", gbid)
	resp, err := googleBooksGet(uri)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	if md5.Sum(data) == imageUnavailableMD5 {
		return nil, errors.New("image unavailable")
	}

	return data, nil
}