-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgetsindexer.go
159 lines (128 loc) · 3.6 KB
/
widgetsindexer.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package vivoupdater
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"github.com/pkg/errors"
)
type WidgetsIndexer struct {
Url string
Username string
Password string
Metrics bool
}
type WidgetsBatchIndexer struct {
Indexer WidgetsIndexer
Suffix string
Regex *regexp.Regexp
Uris []string
}
func NewWidgetsBatchIndexer(wi WidgetsIndexer, suffix string, regex *regexp.Regexp) *WidgetsBatchIndexer {
arr := make([]string, 0)
return &WidgetsBatchIndexer{wi, suffix, regex, arr}
}
type WidgetsUpdateMessage struct {
Uris []string `json:"uris"`
}
func (wi WidgetsIndexer) Name() string {
return "WidgetsIndexer"
}
// This pulls out URLs the particular indexer might be interested in
// typically they would look like one of the following:
//
// https://scholars.duke.edu/individual/per3336942
// https://scholars.duke.edu/individual/org50344699
//
func (wbi *WidgetsBatchIndexer) Gather(u string) {
if wbi.Regex.MatchString(u) {
wbi.Uris = append(wbi.Uris, u)
}
}
// NOTE: idx has username, password (and base url)
func PostToWidgets(idx WidgetsIndexer, postUrl string, uris ...string) error {
m := WidgetsUpdateMessage{Uris: uris}
j, err := json.Marshal(m)
if err != nil {
return err
}
var data = url.Values{}
data.Set("message", string(j))
client := &http.Client{}
req, err := http.NewRequest("POST", postUrl, strings.NewReader(data.Encode()))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(idx.Username, idx.Password)
resp, err := client.Do(req)
//stackoverflow.com/questions/16280176/go-panic-runtime-error-invalid-memory-address-or-nil-pointer-dereference
if err != nil {
return err
}
if !(resp.StatusCode >= 200 && resp.StatusCode <= 299) {
return errors.New(fmt.Sprintf("widgets indexer response=%d\n", resp.StatusCode))
}
defer resp.Body.Close()
return err
}
func (wbi WidgetsBatchIndexer) IndexUris(logger *log.Logger) error {
var err error
size := len(wbi.Uris)
start := time.Now()
if size <= 0 {
// TODO: is this really an error?
// err = errors.New("size of uris is <= 0")
} else {
logger.Printf("widgets-indexer-url:%#v", wbi.Indexer.Url+wbi.Suffix)
for _, uri := range wbi.Uris {
logger.Printf("->widgets-index:%#v\n", uri)
}
err = PostToWidgets(wbi.Indexer, wbi.Indexer.Url+wbi.Suffix, wbi.Uris...)
if err != nil {
err = errors.Wrap(err, "posting to widgets")
}
}
end := time.Now()
metrics := IndexMetrics{Start: start,
End: end,
Uris: wbi.Uris,
Name: "widgets",
Success: (err == nil)}
// NOTE: neccessary just because of test
if wbi.Indexer.Metrics != false {
SendMetrics(metrics)
}
// TODO: if fail, add to a topic or even back to same topic
// with attempt #numbers tracked
// if err != nil {
//
//}
return err
}
const PersonFilterRegex = `.*individual/per[0-9A-Za-z]{3,}`
const OrgFilterRegex = `.*individual/org[0-9]{8}`
var perRegx = regexp.MustCompile(PersonFilterRegex)
var orgRegx = regexp.MustCompile(OrgFilterRegex)
func (wi WidgetsIndexer) Index(batch map[string]bool, logger *log.Logger) (map[string]bool, error) {
widgetsPeopleIndexer := NewWidgetsBatchIndexer(wi, "/people/uris", perRegx)
widgetsOrganizationIndexer := NewWidgetsBatchIndexer(wi, "/organizations/uris", orgRegx)
// maybe don't need context
for u := range batch {
widgetsPeopleIndexer.Gather(u)
widgetsOrganizationIndexer.Gather(u)
}
err := widgetsPeopleIndexer.IndexUris(logger)
if err != nil {
return batch, err
}
err = widgetsOrganizationIndexer.IndexUris(logger)
if err != nil {
return batch, err
}
return batch, nil
}