Merge pull request #58 from stevedylandev/chore/library-migrate-to-openlibrary
71b9be85
Library: migrate to openlibrary
11 file(s) · +216 −157
Library: migrate to openlibrary
Binary file — no preview.
| 1 | 1 | # library |
|
| 2 | 2 | ||
| 3 | - | Personal book tracker with Google Books search. |
|
| 3 | + | Personal book tracker with Open Library search. |
|
| 4 | 4 | ||
| 5 | 5 | ## Install |
|
| 6 | 6 | ||
| 45 | 45 | |---|---|---| |
|
| 46 | 46 | | `ADMIN_PASSWORD` | `changeme` | Admin login password | |
|
| 47 | 47 | | `LIBRARY_DB_PATH` | `library.sqlite` | SQLite path | |
|
| 48 | - | | `GOOGLE_BOOKS_API_KEY` | — | Optional Google Books API key | |
|
| 49 | 48 | | `BASE_URL` | `http://localhost:3000` | Public base URL (OG tags) | |
|
| 50 | 49 | | `HOST` | `0.0.0.0` | Bind host | |
|
| 51 | 50 | | `PORT` | `3000` | Server port | |
|
| 32 | 32 | Templates *template.Template |
|
| 33 | 33 | Sessions *auth.Store |
|
| 34 | 34 | AdminPassword string |
|
| 35 | - | GoogleBooksKey string |
|
| 36 | 35 | CookieSecure bool |
|
| 37 | 36 | BaseURL string |
|
| 38 | 37 | DisplayMode DisplayMode |
| 11 | 11 | CREATE TABLE IF NOT EXISTS books ( |
|
| 12 | 12 | id INTEGER PRIMARY KEY AUTOINCREMENT, |
|
| 13 | 13 | google_id TEXT UNIQUE, |
|
| 14 | + | ol_key TEXT, |
|
| 14 | 15 | title TEXT NOT NULL, |
|
| 15 | 16 | authors TEXT NOT NULL, |
|
| 16 | 17 | isbn TEXT, |
|
| 31 | 32 | type Book struct { |
|
| 32 | 33 | ID int64 `json:"id"` |
|
| 33 | 34 | GoogleID *string `json:"google_id,omitempty"` |
|
| 35 | + | OLKey *string `json:"key,omitempty"` |
|
| 34 | 36 | Title string `json:"title"` |
|
| 35 | 37 | Authors string `json:"authors"` |
|
| 36 | 38 | ISBN *string `json:"isbn,omitempty"` |
|
| 43 | 45 | ||
| 44 | 46 | type NewBook struct { |
|
| 45 | 47 | GoogleID *string |
|
| 48 | + | OLKey *string |
|
| 46 | 49 | Title string |
|
| 47 | 50 | Authors string |
|
| 48 | 51 | ISBN *string |
|
| 61 | 64 | return CategoryLabels{Reading: "Reading", Read: "Read", Want: "Want to Read"} |
|
| 62 | 65 | } |
|
| 63 | 66 | ||
| 64 | - | const selectCols = `id, google_id, title, authors, isbn, cover_url, notes, status, added_at, updated_at` |
|
| 67 | + | const selectCols = `id, google_id, ol_key, title, authors, isbn, cover_url, notes, status, added_at, updated_at` |
|
| 65 | 68 | ||
| 66 | 69 | func scanBook(s interface{ Scan(...any) error }) (*Book, error) { |
|
| 67 | 70 | var b Book |
|
| 68 | - | var gid, isbn, cover, notes sql.NullString |
|
| 69 | - | err := s.Scan(&b.ID, &gid, &b.Title, &b.Authors, &isbn, &cover, ¬es, &b.Status, &b.AddedAt, &b.UpdatedAt) |
|
| 71 | + | var gid, olKey, isbn, cover, notes sql.NullString |
|
| 72 | + | err := s.Scan(&b.ID, &gid, &olKey, &b.Title, &b.Authors, &isbn, &cover, ¬es, &b.Status, &b.AddedAt, &b.UpdatedAt) |
|
| 70 | 73 | if errors.Is(err, sql.ErrNoRows) { |
|
| 71 | 74 | return nil, nil |
|
| 72 | 75 | } |
|
| 77 | 80 | v := gid.String |
|
| 78 | 81 | b.GoogleID = &v |
|
| 79 | 82 | } |
|
| 83 | + | if olKey.Valid { |
|
| 84 | + | v := olKey.String |
|
| 85 | + | b.OLKey = &v |
|
| 86 | + | } |
|
| 80 | 87 | if isbn.Valid { |
|
| 81 | 88 | v := isbn.String |
|
| 82 | 89 | b.ISBN = &v |
|
| 121 | 128 | ||
| 122 | 129 | func insertBook(db *sql.DB, b NewBook) (int64, error) { |
|
| 123 | 130 | now := time.Now().UTC().Unix() |
|
| 124 | - | var gid, isbn, cover, notes any |
|
| 131 | + | var gid, olKey, isbn, cover, notes any |
|
| 125 | 132 | if b.GoogleID != nil && *b.GoogleID != "" { |
|
| 126 | 133 | gid = *b.GoogleID |
|
| 134 | + | } |
|
| 135 | + | if b.OLKey != nil && *b.OLKey != "" { |
|
| 136 | + | olKey = *b.OLKey |
|
| 127 | 137 | } |
|
| 128 | 138 | if b.ISBN != nil && *b.ISBN != "" { |
|
| 129 | 139 | isbn = *b.ISBN |
|
| 135 | 145 | notes = *b.Notes |
|
| 136 | 146 | } |
|
| 137 | 147 | res, err := db.Exec( |
|
| 138 | - | `INSERT INTO books (google_id, title, authors, isbn, cover_url, notes, status, added_at, updated_at) |
|
| 139 | - | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) |
|
| 148 | + | `INSERT INTO books (google_id, ol_key, title, authors, isbn, cover_url, notes, status, added_at, updated_at) |
|
| 149 | + | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
|
| 140 | 150 | ON CONFLICT(google_id) DO UPDATE SET status = excluded.status, updated_at = excluded.updated_at`, |
|
| 141 | - | gid, b.Title, b.Authors, isbn, cover, notes, b.Status, now, now, |
|
| 151 | + | gid, olKey, b.Title, b.Authors, isbn, cover, notes, b.Status, now, now, |
|
| 142 | 152 | ) |
|
| 143 | 153 | if err != nil { |
|
| 144 | 154 | return 0, err |
|
| 69 | 69 | t.Fatalf("labels %#v", labels) |
|
| 70 | 70 | } |
|
| 71 | 71 | ||
| 72 | - | if got := pickISBN([]identifier{{Kind: "ISBN_10", Identifier: "ten"}, {Kind: "ISBN_13", Identifier: "thirteen"}}); got != "thirteen" { |
|
| 73 | - | t.Fatalf("isbn %q", got) |
|
| 74 | - | } |
|
| 75 | 72 | if !isISBNChars("123456789X") || isISBNChars("123ABC") { |
|
| 76 | 73 | t.Fatal("isbn char validation failed") |
|
| 77 | 74 | } |
| 10 | 10 | - PORT=${PORT:-3000} |
|
| 11 | 11 | - LIBRARY_DB_PATH=/data/library-go.sqlite |
|
| 12 | 12 | - ADMIN_PASSWORD=${ADMIN_PASSWORD:-changeme} |
|
| 13 | - | - GOOGLE_BOOKS_API_KEY=${GOOGLE_BOOKS_API_KEY:-} |
|
| 14 | 13 | - BASE_URL=${BASE_URL:-http://localhost:${PORT:-3000}} |
|
| 15 | 14 | - LIBRARY_DISPLAY_MODE=${LIBRARY_DISPLAY_MODE:-inline} |
|
| 16 | 15 | - COOKIE_SECURE=${COOKIE_SECURE:-false} |
| 1 | - | package main |
|
| 2 | - | ||
| 3 | - | import ( |
|
| 4 | - | "context" |
|
| 5 | - | "encoding/json" |
|
| 6 | - | "fmt" |
|
| 7 | - | "net/http" |
|
| 8 | - | "net/url" |
|
| 9 | - | "strings" |
|
| 10 | - | "time" |
|
| 11 | - | ) |
|
| 12 | - | ||
| 13 | - | type SearchHit struct { |
|
| 14 | - | GoogleID string `json:"google_id"` |
|
| 15 | - | Title string `json:"title"` |
|
| 16 | - | Authors string `json:"authors"` |
|
| 17 | - | ISBN *string `json:"isbn,omitempty"` |
|
| 18 | - | CoverURL *string `json:"cover_url,omitempty"` |
|
| 19 | - | } |
|
| 20 | - | ||
| 21 | - | type volumesResponse struct { |
|
| 22 | - | Items []volume `json:"items"` |
|
| 23 | - | } |
|
| 24 | - | ||
| 25 | - | type volume struct { |
|
| 26 | - | ID string `json:"id"` |
|
| 27 | - | VolumeInfo volumeInfo `json:"volumeInfo"` |
|
| 28 | - | } |
|
| 29 | - | ||
| 30 | - | type volumeInfo struct { |
|
| 31 | - | Title string `json:"title"` |
|
| 32 | - | Authors []string `json:"authors"` |
|
| 33 | - | Identifiers []identifier `json:"industryIdentifiers"` |
|
| 34 | - | ImageLinks *imageLinks `json:"imageLinks"` |
|
| 35 | - | } |
|
| 36 | - | ||
| 37 | - | type identifier struct { |
|
| 38 | - | Kind string `json:"type"` |
|
| 39 | - | Identifier string `json:"identifier"` |
|
| 40 | - | } |
|
| 41 | - | ||
| 42 | - | type imageLinks struct { |
|
| 43 | - | Thumbnail string `json:"thumbnail"` |
|
| 44 | - | SmallThumbnail string `json:"smallThumbnail"` |
|
| 45 | - | } |
|
| 46 | - | ||
| 47 | - | func googleBooksSearch(ctx context.Context, query, apiKey string) ([]SearchHit, error) { |
|
| 48 | - | trimmed := strings.TrimSpace(query) |
|
| 49 | - | if trimmed == "" { |
|
| 50 | - | return nil, nil |
|
| 51 | - | } |
|
| 52 | - | normalized := strings.Map(func(r rune) rune { |
|
| 53 | - | if r == ' ' || r == '\t' || r == '\n' || r == '-' { |
|
| 54 | - | return -1 |
|
| 55 | - | } |
|
| 56 | - | return r |
|
| 57 | - | }, trimmed) |
|
| 58 | - | isISBN := (len(normalized) == 10 || len(normalized) == 13) && isISBNChars(normalized) |
|
| 59 | - | q := trimmed |
|
| 60 | - | if isISBN { |
|
| 61 | - | q = "isbn:" + strings.ToUpper(normalized) |
|
| 62 | - | } |
|
| 63 | - | u := "https://www.googleapis.com/books/v1/volumes?q=" + url.QueryEscape(q) + "&maxResults=10&printType=books" |
|
| 64 | - | if apiKey != "" { |
|
| 65 | - | u += "&key=" + url.QueryEscape(apiKey) |
|
| 66 | - | } |
|
| 67 | - | ||
| 68 | - | client := &http.Client{Timeout: 8 * time.Second} |
|
| 69 | - | req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil) |
|
| 70 | - | if err != nil { |
|
| 71 | - | return nil, err |
|
| 72 | - | } |
|
| 73 | - | req.Header.Set("User-Agent", "andromeda-library/0.1") |
|
| 74 | - | resp, err := client.Do(req) |
|
| 75 | - | if err != nil { |
|
| 76 | - | return nil, fmt.Errorf("request: %w", err) |
|
| 77 | - | } |
|
| 78 | - | defer resp.Body.Close() |
|
| 79 | - | if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
|
| 80 | - | return nil, fmt.Errorf("google books status %s", resp.Status) |
|
| 81 | - | } |
|
| 82 | - | var data volumesResponse |
|
| 83 | - | if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { |
|
| 84 | - | return nil, fmt.Errorf("parse: %w", err) |
|
| 85 | - | } |
|
| 86 | - | hits := make([]SearchHit, 0, len(data.Items)) |
|
| 87 | - | for _, v := range data.Items { |
|
| 88 | - | title := v.VolumeInfo.Title |
|
| 89 | - | if title == "" { |
|
| 90 | - | title = "Untitled" |
|
| 91 | - | } |
|
| 92 | - | hit := SearchHit{ |
|
| 93 | - | GoogleID: v.ID, |
|
| 94 | - | Title: title, |
|
| 95 | - | Authors: strings.Join(v.VolumeInfo.Authors, ", "), |
|
| 96 | - | } |
|
| 97 | - | if isbn := pickISBN(v.VolumeInfo.Identifiers); isbn != "" { |
|
| 98 | - | hit.ISBN = &isbn |
|
| 99 | - | } |
|
| 100 | - | if v.VolumeInfo.ImageLinks != nil { |
|
| 101 | - | cover := v.VolumeInfo.ImageLinks.Thumbnail |
|
| 102 | - | if cover == "" { |
|
| 103 | - | cover = v.VolumeInfo.ImageLinks.SmallThumbnail |
|
| 104 | - | } |
|
| 105 | - | if cover != "" { |
|
| 106 | - | cover = strings.Replace(cover, "http://", "https://", 1) |
|
| 107 | - | hit.CoverURL = &cover |
|
| 108 | - | } |
|
| 109 | - | } |
|
| 110 | - | hits = append(hits, hit) |
|
| 111 | - | } |
|
| 112 | - | return hits, nil |
|
| 113 | - | } |
|
| 114 | - | ||
| 115 | - | func pickISBN(ids []identifier) string { |
|
| 116 | - | for _, i := range ids { |
|
| 117 | - | if i.Kind == "ISBN_13" { |
|
| 118 | - | return i.Identifier |
|
| 119 | - | } |
|
| 120 | - | } |
|
| 121 | - | for _, i := range ids { |
|
| 122 | - | if i.Kind == "ISBN_10" { |
|
| 123 | - | return i.Identifier |
|
| 124 | - | } |
|
| 125 | - | } |
|
| 126 | - | return "" |
|
| 127 | - | } |
|
| 128 | - | ||
| 129 | - | func isISBNChars(s string) bool { |
|
| 130 | - | for _, c := range s { |
|
| 131 | - | if (c < '0' || c > '9') && c != 'X' && c != 'x' { |
|
| 132 | - | return false |
|
| 133 | - | } |
|
| 134 | - | } |
|
| 135 | - | return true |
|
| 136 | - | } |
| 176 | 176 | } |
|
| 177 | 177 | ||
| 178 | 178 | func (a *App) adminSearch(w http.ResponseWriter, r *http.Request) { |
|
| 179 | - | hits, err := googleBooksSearch(r.Context(), r.URL.Query().Get("q"), a.GoogleBooksKey) |
|
| 179 | + | hits, err := openLibrarySearch(r.Context(), r.URL.Query().Get("q")) |
|
| 180 | 180 | if err != nil { |
|
| 181 | - | a.Log.Warn("google books search failed", "err", err) |
|
| 181 | + | a.Log.Warn("Open Library search failed", "err", err) |
|
| 182 | 182 | web.WriteError(w, http.StatusBadGateway, err.Error()) |
|
| 183 | 183 | return |
|
| 184 | 184 | } |
|
| 199 | 199 | return |
|
| 200 | 200 | } |
|
| 201 | 201 | b := NewBook{Title: r.FormValue("title"), Authors: r.FormValue("authors"), Status: status} |
|
| 202 | - | if v := strings.TrimSpace(r.FormValue("google_id")); v != "" { |
|
| 203 | - | b.GoogleID = &v |
|
| 202 | + | if v := strings.TrimSpace(r.FormValue("key")); v != "" { |
|
| 203 | + | b.OLKey = &v |
|
| 204 | 204 | } |
|
| 205 | 205 | if v := strings.TrimSpace(r.FormValue("isbn")); v != "" { |
|
| 206 | 206 | b.ISBN = &v |
|
| 21 | 21 | if err != nil { |
|
| 22 | 22 | log.Fatal(err) |
|
| 23 | 23 | } |
|
| 24 | + | db.Exec(`ALTER TABLE books ADD COLUMN ol_key TEXT`) |
|
| 24 | 25 | defer db.Close() |
|
| 25 | 26 | ||
| 26 | 27 | sessions := &auth.Store{DB: db, CookieName: "session", CookieSecure: config.GetenvBool("COOKIE_SECURE", false)} |
|
| 36 | 37 | Templates: tmpl, |
|
| 37 | 38 | Sessions: sessions, |
|
| 38 | 39 | AdminPassword: os.Getenv("ADMIN_PASSWORD"), |
|
| 39 | - | GoogleBooksKey: os.Getenv("GOOGLE_BOOKS_API_KEY"), |
|
| 40 | 40 | CookieSecure: sessions.CookieSecure, |
|
| 41 | 41 | BaseURL: config.Getenv("BASE_URL", "http://localhost:3000"), |
|
| 42 | 42 | DisplayMode: parseDisplayMode(config.Getenv("LIBRARY_DISPLAY_MODE", "inline")), |
|
| 1 | + | package main |
|
| 2 | + | ||
| 3 | + | import ( |
|
| 4 | + | "context" |
|
| 5 | + | "encoding/json" |
|
| 6 | + | "fmt" |
|
| 7 | + | "net/http" |
|
| 8 | + | "net/url" |
|
| 9 | + | "strings" |
|
| 10 | + | "time" |
|
| 11 | + | ) |
|
| 12 | + | ||
| 13 | + | type SearchHit struct { |
|
| 14 | + | OLKey string `json:"key"` |
|
| 15 | + | Title string `json:"title"` |
|
| 16 | + | Authors string `json:"author_name"` |
|
| 17 | + | ISBN *string `json:"isbn,omitempty"` // canonical ISBN-13 |
|
| 18 | + | CoverURL *string `json:"cover_url,omitempty"` |
|
| 19 | + | } |
|
| 20 | + | ||
| 21 | + | type olResponse struct { |
|
| 22 | + | Docs []olDoc `json:"docs"` |
|
| 23 | + | } |
|
| 24 | + | ||
| 25 | + | type olDoc struct { |
|
| 26 | + | OLKey string `json:"key"` |
|
| 27 | + | Title string `json:"title"` |
|
| 28 | + | Authors []string `json:"author_name"` |
|
| 29 | + | ISBN []string `json:"isbn"` |
|
| 30 | + | CoverID int64 `json:"cover_i"` |
|
| 31 | + | } |
|
| 32 | + | ||
| 33 | + | func openLibrarySearch(ctx context.Context, query string) ([]SearchHit, error) { |
|
| 34 | + | trimmed := strings.TrimSpace(query) |
|
| 35 | + | if trimmed == "" { |
|
| 36 | + | return nil, nil |
|
| 37 | + | } |
|
| 38 | + | queries := url.Values{ |
|
| 39 | + | "limit": {"10"}, |
|
| 40 | + | "fields": {"key,title,author_name,isbn,cover_i"}, |
|
| 41 | + | } |
|
| 42 | + | q := trimmed |
|
| 43 | + | if isISBNQuery(trimmed) { |
|
| 44 | + | q = strings.ToUpper(stripISBNSep(trimmed)) |
|
| 45 | + | queries.Add("isbn", q) |
|
| 46 | + | } else { |
|
| 47 | + | queries.Add("q", q) |
|
| 48 | + | } |
|
| 49 | + | u := "https://openlibrary.org/search.json?" + queries.Encode() |
|
| 50 | + | ||
| 51 | + | client := &http.Client{Timeout: 8 * time.Second} |
|
| 52 | + | req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil) |
|
| 53 | + | if err != nil { |
|
| 54 | + | return nil, err |
|
| 55 | + | } |
|
| 56 | + | req.Header.Set("User-Agent", "andromeda-library/0.1") |
|
| 57 | + | resp, err := client.Do(req) |
|
| 58 | + | if err != nil { |
|
| 59 | + | return nil, fmt.Errorf("request: %w", err) |
|
| 60 | + | } |
|
| 61 | + | defer resp.Body.Close() |
|
| 62 | + | if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
|
| 63 | + | return nil, fmt.Errorf("openlibrary status %s", resp.Status) |
|
| 64 | + | } |
|
| 65 | + | var data olResponse |
|
| 66 | + | if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { |
|
| 67 | + | return nil, fmt.Errorf("parse: %w", err) |
|
| 68 | + | } |
|
| 69 | + | ||
| 70 | + | hits := make([]SearchHit, 0, len(data.Docs)) |
|
| 71 | + | for _, d := range data.Docs { |
|
| 72 | + | title := d.Title |
|
| 73 | + | if title == "" { |
|
| 74 | + | title = "Untitled" |
|
| 75 | + | } |
|
| 76 | + | hit := SearchHit{ |
|
| 77 | + | OLKey: d.OLKey, |
|
| 78 | + | Title: title, |
|
| 79 | + | Authors: strings.Join(d.Authors, ", "), |
|
| 80 | + | } |
|
| 81 | + | if isbn := pickISBN(d.ISBN); isbn != "" { |
|
| 82 | + | hit.ISBN = &isbn |
|
| 83 | + | } |
|
| 84 | + | if d.CoverID != 0 { |
|
| 85 | + | cover := fmt.Sprintf("https://covers.openlibrary.org/b/id/%d-L.jpg", d.CoverID) |
|
| 86 | + | hit.CoverURL = &cover |
|
| 87 | + | } |
|
| 88 | + | hits = append(hits, hit) |
|
| 89 | + | } |
|
| 90 | + | return hits, nil |
|
| 91 | + | } |
|
| 92 | + | ||
| 93 | + | func isISBNChars(s string) bool { |
|
| 94 | + | for _, c := range s { |
|
| 95 | + | if (c < '0' || c > '9') && c != 'X' && c != 'x' { |
|
| 96 | + | return false |
|
| 97 | + | } |
|
| 98 | + | } |
|
| 99 | + | return true |
|
| 100 | + | } |
|
| 101 | + | ||
| 102 | + | func isISBNQuery(s string) bool { |
|
| 103 | + | n := stripISBNSep(s) |
|
| 104 | + | return (len(n) == 10 || len(n) == 13) && isISBNChars(n) |
|
| 105 | + | } |
|
| 106 | + | ||
| 107 | + | func stripISBNSep(s string) string { |
|
| 108 | + | return strings.Map(func(r rune) rune { |
|
| 109 | + | if r == '-' || r == ' ' { |
|
| 110 | + | return -1 |
|
| 111 | + | } |
|
| 112 | + | return r |
|
| 113 | + | }, s) |
|
| 114 | + | } |
|
| 115 | + | ||
| 116 | + | // pickISBN normalizes to ISBN-13 and returns one canonical value. |
|
| 117 | + | func pickISBN(raw []string) string { |
|
| 118 | + | seen := map[string]struct{}{} |
|
| 119 | + | var firstValid string |
|
| 120 | + | for _, r := range raw { |
|
| 121 | + | n := strings.ToUpper(stripISBNSep(r)) |
|
| 122 | + | if len(n) == 10 { |
|
| 123 | + | if !validISBN10(n) { |
|
| 124 | + | continue |
|
| 125 | + | } |
|
| 126 | + | n, _ = isbn10to13(n) |
|
| 127 | + | } |
|
| 128 | + | if len(n) != 13 || !validISBN13(n) { |
|
| 129 | + | continue |
|
| 130 | + | } |
|
| 131 | + | if _, dup := seen[n]; dup { |
|
| 132 | + | continue |
|
| 133 | + | } |
|
| 134 | + | seen[n] = struct{}{} |
|
| 135 | + | if firstValid == "" { |
|
| 136 | + | firstValid = n |
|
| 137 | + | } |
|
| 138 | + | } |
|
| 139 | + | return firstValid |
|
| 140 | + | } |
|
| 141 | + | ||
| 142 | + | func validISBN13(s string) bool { |
|
| 143 | + | sum := 0 |
|
| 144 | + | for i, c := range s { |
|
| 145 | + | d := int(c - '0') |
|
| 146 | + | if d < 0 || d > 9 { |
|
| 147 | + | return false |
|
| 148 | + | } |
|
| 149 | + | if i%2 == 1 { |
|
| 150 | + | d *= 3 |
|
| 151 | + | } |
|
| 152 | + | sum += d |
|
| 153 | + | } |
|
| 154 | + | return sum%10 == 0 |
|
| 155 | + | } |
|
| 156 | + | ||
| 157 | + | func validISBN10(s string) bool { |
|
| 158 | + | sum := 0 |
|
| 159 | + | for i, c := range s { |
|
| 160 | + | d := 10 |
|
| 161 | + | if c != 'X' { |
|
| 162 | + | d = int(c - '0') |
|
| 163 | + | if d < 0 || d > 9 { |
|
| 164 | + | return false |
|
| 165 | + | } |
|
| 166 | + | } else if i != 9 { |
|
| 167 | + | return false |
|
| 168 | + | } |
|
| 169 | + | sum += d * (10 - i) |
|
| 170 | + | } |
|
| 171 | + | return sum%11 == 0 |
|
| 172 | + | } |
|
| 173 | + | ||
| 174 | + | func isbn10to13(s string) (string, bool) { |
|
| 175 | + | if len(s) != 10 { |
|
| 176 | + | return "", false |
|
| 177 | + | } |
|
| 178 | + | base := "978" + s[:9] |
|
| 179 | + | sum := 0 |
|
| 180 | + | for i, c := range base { |
|
| 181 | + | d := int(c - '0') |
|
| 182 | + | if d < 0 || d > 9 { |
|
| 183 | + | return "", false |
|
| 184 | + | } |
|
| 185 | + | if i%2 == 1 { |
|
| 186 | + | d *= 3 |
|
| 187 | + | } |
|
| 188 | + | sum += d |
|
| 189 | + | } |
|
| 190 | + | return base + string(rune('0'+(10-sum%10)%10)), true |
|
| 191 | + | } |
| 43 | 43 | </section> |
|
| 44 | 44 | ||
| 45 | 45 | <section class="admin-form"> |
|
| 46 | - | <h3>Search Books (Google)</h3> |
|
| 46 | + | <h3>Search Books (Open Library)</h3> |
|
| 47 | 47 | <div class="search-row"> |
|
| 48 | 48 | <input type="text" id="book-query" placeholder="title, author, isbn" /> |
|
| 49 | 49 | <button type="button" id="search-btn" onclick="searchBooks()">Search</button> |
|
| 239 | 239 | el.value = value || ''; |
|
| 240 | 240 | return el; |
|
| 241 | 241 | }; |
|
| 242 | - | info.appendChild(hidden('google_id', hit.google_id)); |
|
| 242 | + | info.appendChild(hidden('key', hit.key)); |
|
| 243 | 243 | info.appendChild(hidden('title', hit.title)); |
|
| 244 | 244 | info.appendChild(hidden('authors', hit.authors)); |
|
| 245 | 245 | info.appendChild(hidden('isbn', hit.isbn)); |
|