aboutsummaryrefslogtreecommitdiffstats
path: root/skate/cmd/skate-dot/main.go
blob: 97b70ad2ab0a3798e0a82b36cccc7da110ab031b (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
80
// skate-dot generates dot files from inbound and outbound citation links.
package main

import (
	"context"
	"flag"
	"fmt"
	"io"
	"log"
	"os"

	"github.com/elastic/go-elasticsearch/esapi"
	elasticsearch "github.com/elastic/go-elasticsearch/v7"
)

var (
	es     = flag.String("es", "http://localhost:9200", "elasticsearch holding fatcat_ref index")
	index  = flag.String("x", "fatcat_ref_v01", "index name")
	fatcat = flag.String("f", "https://api.fatcat.wiki/v0", "fatcat api")
	ident  = flag.String("i", "2kw3xjf2cbcmdlm3ihkoz2t4lu", "release ident")
)

func main() {
	flag.Parse()
	// s := "The Determination of Concentration and Type of Ownership on Bank Performance and Risks in Indonesia"
	// fmt.Printf("%s\n", wordwrap.WrapString(s, 20))
	cfg := elasticsearch.Config{
		Addresses: []string{
			*es,
		},
	}
	es, err := elasticsearch.NewClient(cfg)
	if err != nil {
		log.Fatal(err)
	}
	client := &Client{
		Api:   *fatcat,
		Es:    es,
		Index: *index,
	}
	log.Println(client)
	client.Outbound(*ident)
	client.Inbound(*ident)
}

// A client for fatcat and elasticsearch.
type Client struct {
	Api   string
	Es    *elasticsearch.Client
	Index string
}

func (c *Client) String() string {
	info, _ := c.Es.Info()
	return fmt.Sprintf("%s %s (%s) %s", c.Api, info, elasticsearch.Version, c.Index)
}

func (c *Client) Inbound(ident string) []string {
	resp, err := esapi.Search(
		esapi.Search.WithContext(context.Background()),
		esapi.Search.WithIndex(c.Index),
	)
	if err != nil {
		log.Fatal(err)
	}
	io.Copy(os.Stdout, resp.Body)
	return nil
}

func (c *Client) Outbound(ident string) []string {
	req := &esapi.SearchRequest{
		Query: fmt.Sprintf("source_release_ident:%s", ident),
	}
	resp, err := req.Do(context.Background(), c.Es)
	if err != nil {
		log.Fatal(err)
	}
	io.Copy(os.Stdout, resp.Body)
	return nil
}