summaryrefslogtreecommitdiff
path: root/cmd/getsamplepages/main.go
blob: 36c7a8e184d2a7125c10fe5e0c40abd884cd7c51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
// Copyright 2020 Nick White.
// Use of this source code is governed by the GPLv3
// license that can be found in the LICENSE file.

// getsamplepages downloads sample pages from each book in a
// set of OCRed books
package main

import (
	"flag"
	"fmt"
	"log"
	"strings"

	"rescribe.xyz/bookpipeline"
)

const usage = `Usage: getsamplepages

Downloads a sample page hocr and image from each book in a set
of OCRed books. These can then be used for various testing,
statistics, and so on.
`

const pgnum = "0100"

// null writer to enable non-verbose logging to be discarded
type NullWriter bool

func (w NullWriter) Write(p []byte) (n int, err error) {
	return len(p), nil
}

type Pipeliner interface {
	Init() error
	ListObjectPrefixes(bucket string) ([]string, error)
	ListObjects(bucket string, prefix string) ([]string, error)
	Download(bucket string, key string, fn string) error
	WIPStorageId() string
}

func main() {
	flag.Usage = func() {
		fmt.Fprintf(flag.CommandLine.Output(), usage)
		flag.PrintDefaults()
	}
	flag.Parse()

	var n NullWriter
	verboselog := log.New(n, "", log.LstdFlags)

	var conn Pipeliner
	conn = &bookpipeline.AwsConn{Region: "eu-west-2", Logger: verboselog}

	err := conn.Init()
	if err != nil {
		log.Fatalln("Error setting up cloud connection:", err)
	}

	log.Println("Getting list of all books")
	prefixes, err := conn.ListObjectPrefixes(conn.WIPStorageId())
	if err != nil {
		log.Fatalln("Failed to get list of books", err)
	}

	for _, p := range prefixes {
		name := strings.Split(p, "/")[0]
		log.Printf("Downloading a page from %s\n", name)

		fn := pgnum + ".jpg"
		err = conn.Download(conn.WIPStorageId(), p+fn, name+fn)
		if err != nil && strings.HasPrefix(err.Error(), "NoSuchKey:") {
			log.Printf("Skipping %s as no page %s found\n", p, pgnum)
			continue
		} else if err != nil {
			log.Fatalf("Download of %s%s failed: %v\n", p+fn, err)
		}
	}
}