diff --git a/cmd/quotepick/main.go b/cmd/quotepick/main.go new file mode 100644 index 0000000..d224070 --- /dev/null +++ b/cmd/quotepick/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "math/rand" + "os" + "time" + + "ur.gs/lysenko/handlers/quotedb" +) + +// This is the same seeding as done by lysenko himself, so should provide the +// same results. +// +// Note, however, that we actually use the sqlite RANDOM() function to get a +// random quote ID, not this seed. +func init() { + rand.Seed(time.Now().Unix()) +} + +func main() { + if len(os.Args) != 3 { + fmt.Fprintf(os.Stderr, "Usage: %v \n", os.Args[0]) + os.Exit(1) + } + + filename := os.Args[1] + channel := os.Args[2] + + if _, err := os.Stat(filename); os.IsNotExist(err) { + fmt.Fprintf(os.Stderr, "File %q doesn't exist\n", filename) + os.Exit(1) + } + + db, err := quotedb.New(filename) + if err != nil { + fmt.Fprintf(os.Stderr, "Couldn't open database %q: %v\n", filename, err) + os.Exit(1) + } + + for count := uint64(0); ; count++ { + quote, err := db.FindRandomQuote(channel) + if err != nil { + fmt.Fprintf(os.Stderr, "i=%v: couldn't find random quote: %v\n", count, err) + os.Exit(1) + } + + fmt.Fprintf(os.Stdout, "%v ", quote.Id) + } + + os.Exit(0) +}