aboutsummaryrefslogtreecommitdiffstats
path: root/skate/cmd
diff options
context:
space:
mode:
authorMartin Czygan <martin.czygan@gmail.com>2021-05-20 13:23:09 +0200
committerMartin Czygan <martin.czygan@gmail.com>2021-05-20 13:23:09 +0200
commit7be09009b42d3af96ca8875c698922710d92d074 (patch)
treea7dea586f68f588438bd9ffa39e128d44fead3b7 /skate/cmd
parentcec830ea500b50e9d38b0d6193b22cd10f577370 (diff)
downloadrefcat-7be09009b42d3af96ca8875c698922710d92d074.tar.gz
refcat-7be09009b42d3af96ca8875c698922710d92d074.zip
wip: cdx lookup
Diffstat (limited to 'skate/cmd')
-rw-r--r--skate/cmd/skate-cdx-lookup/main.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/skate/cmd/skate-cdx-lookup/main.go b/skate/cmd/skate-cdx-lookup/main.go
new file mode 100644
index 0000000..e26102f
--- /dev/null
+++ b/skate/cmd/skate-cdx-lookup/main.go
@@ -0,0 +1,71 @@
+package main
+
+import (
+ "bufio"
+ "flag"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "log"
+ "os"
+ "path"
+ "strings"
+ "time"
+
+ "git.archive.org/martin/cgraph/skate"
+ "github.com/adrg/xdg"
+)
+
+var (
+ sleep = flag.Duration("s", 200*time.Millisecond, "sleep between requests")
+ cacheDir = flag.String("c", path.Join(xdg.CacheHome, "skate-cdx"), "cache dir")
+ quiet = flag.Bool("q", false, "no logging")
+)
+
+func main() {
+ flag.Parse()
+ var r io.Reader = os.Stdin
+ if flag.NArg() > 0 {
+ f, err := os.Open(flag.Arg(0))
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer f.Close()
+ r = f
+ }
+ if *quiet {
+ log.SetOutput(ioutil.Discard)
+ }
+ var cache = skate.Cache{Dir: *cacheDir}
+ br := bufio.NewReader(r)
+ for {
+ line, err := br.ReadString('\n')
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ log.Fatal(err)
+ }
+ line = strings.TrimSpace(line)
+
+ if !cache.Has(line) {
+ _, b, err := skate.LookupCDX(line)
+ if err != nil {
+ log.Fatal(err)
+ }
+ if err := cache.Set(line, b); err != nil {
+ log.Fatal(err)
+ }
+ time.Sleep(*sleep)
+ }
+ b, err := cache.Get(line)
+ if err != nil {
+ log.Fatal(err)
+ }
+ rows, err := skate.ParseCDX(b)
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Printf("% 10d %s\n", len(rows), line)
+ }
+}