summaryrefslogtreecommitdiff
path: root/cmd/rescribe
diff options
context:
space:
mode:
authorNick White <git@njw.name>2022-03-21 16:26:55 +0000
committerNick White <git@njw.name>2022-03-21 16:26:55 +0000
commit63b6942f6b2649c70c30cdced6c033ff2607724f (patch)
tree7f8ff3dc741a3977e5f48c7680a0f2e749c28c94 /cmd/rescribe
parentea6f43514ea470fac399a8155155babfbabec118 (diff)
rescribe: move getBookIdFromUrl() to gbook.go, and add tests for it
Diffstat (limited to 'cmd/rescribe')
-rw-r--r--cmd/rescribe/gbook.go47
-rw-r--r--cmd/rescribe/gbook_test.go31
-rw-r--r--cmd/rescribe/gui.go47
3 files changed, 78 insertions, 47 deletions
diff --git a/cmd/rescribe/gbook.go b/cmd/rescribe/gbook.go
index f0e9e1e..fe2f4b8 100644
--- a/cmd/rescribe/gbook.go
+++ b/cmd/rescribe/gbook.go
@@ -14,6 +14,7 @@ import (
"os"
"os/exec"
"path"
+ "regexp"
"strings"
"unicode"
@@ -206,3 +207,49 @@ func getGoogleBook(ctx context.Context, gbookcmd string, id string, basedir stri
return dir, nil
}
+
+// getBookIdFromUrl returns a 12 character Google Book ID from
+// a Google URL, or an error if one can't be found.
+// Example URLs:
+// https://books.google.it/books?id=QjQepCuN8JYC
+// https://www.google.it/books/edition/_/VJbr-Oe2au0C
+func getBookIdFromUrl(url string) (string, error) {
+ lurl := strings.ToLower(url)
+ if len(url) == 12 && !strings.ContainsAny(url, "?/:") {
+ return url, nil
+ }
+
+ matchUrl, err := regexp.MatchString("https://www.google.[^\\/]*/books/", url)
+ if err != nil {
+ return "", err
+ }
+
+ if strings.HasPrefix(lurl, "https://books.google") {
+ start := strings.Index(lurl, "?id=")
+ if start == -1 {
+ start = strings.Index(lurl, "&id=")
+ }
+
+ if start >= 0 {
+ start += 4
+ if len(url[start:]) < 12 {
+ return "", fmt.Errorf("Could not find book ID in URL")
+ }
+ return url[start : start+12], nil
+ }
+
+ return "", fmt.Errorf("Could not find book ID in URL")
+ }
+ if matchUrl == true {
+ start := strings.Index(lurl, "edition/_/")
+
+ if start >= 0 {
+ start += 10
+ if len(url[start:]) < 12 {
+ return "", fmt.Errorf("Could not find book ID in URL")
+ }
+ return url[start : start+12], nil
+ }
+ }
+ return "", fmt.Errorf("Could not find book ID in URL")
+}
diff --git a/cmd/rescribe/gbook_test.go b/cmd/rescribe/gbook_test.go
new file mode 100644
index 0000000..6cd5a63
--- /dev/null
+++ b/cmd/rescribe/gbook_test.go
@@ -0,0 +1,31 @@
+// Copyright 2022 Nick White.
+// Use of this source code is governed by the GPLv3
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "testing"
+)
+
+func Test_getBookIdFromUrl(t *testing.T) {
+ cases := []struct {
+ url string
+ id string
+ }{
+ {"https://books.google.it/books?id=QjQepCuN8JYC", "QjQepCuN8JYC"},
+ {"https://www.google.it/books/edition/_/VJbr-Oe2au0C", "VJbr-Oe2au0C"},
+ }
+
+ for _, c := range cases {
+ t.Run(c.url, func(t *testing.T) {
+ id, err := getBookIdFromUrl(c.url)
+ if err != nil {
+ t.Fatalf("Error running test: %v", err)
+ }
+ if id != c.id {
+ t.Fatalf("Expected %s, got %s", c.id, id)
+ }
+ })
+ }
+}
diff --git a/cmd/rescribe/gui.go b/cmd/rescribe/gui.go
index a75b816..a9d5fbb 100644
--- a/cmd/rescribe/gui.go
+++ b/cmd/rescribe/gui.go
@@ -13,7 +13,6 @@ import (
"log"
"os"
"path/filepath"
- "regexp"
"strings"
"fyne.io/fyne/v2"
@@ -43,52 +42,6 @@ var trainingNames = map[string]string{
"rescribev8_fast": "Latin (early printing)",
}
-// getBookIdFromUrl returns a 12 character Google Book ID from
-// a Google URL, or an error if one can't be found.
-// Example URLs:
-// https://books.google.it/books?id=QjQepCuN8JYC
-// https://www.google.it/books/edition/_/VJbr-Oe2au0C
-func getBookIdFromUrl(url string) (string, error) {
- lurl := strings.ToLower(url)
- if len(url) == 12 && !strings.ContainsAny(url, "?/:") {
- return url, nil
- }
-
- matchUrl, err := regexp.MatchString("https://www.google.[^\\/]*/books/", url)
- if err != nil {
- return "", err
- }
-
- if strings.HasPrefix(lurl, "https://books.google") {
- start := strings.Index(lurl, "?id=")
- if start == -1 {
- start = strings.Index(lurl, "&id=")
- }
-
- if start >= 0 {
- start += 4
- if len(url[start:]) < 12 {
- return "", fmt.Errorf("Could not find book ID in URL")
- }
- return url[start : start+12], nil
- }
-
- return "", fmt.Errorf("Could not find book ID in URL")
- }
- if matchUrl == true {
- start := strings.Index(lurl, "edition/_/")
-
- if start >= 0 {
- start += 10
- if len(url[start:]) < 12 {
- return "", fmt.Errorf("Could not find book ID in URL")
- }
- return url[start : start+12], nil
- }
- }
- return "", fmt.Errorf("Could not find book ID in URL")
-}
-
// copyStdoutToChan creates a pipe to copy anything written
// to the file also to a rune channel.
func copyStdoutToChan() (chan rune, error) {