-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetcher.go
456 lines (396 loc) · 11.1 KB
/
fetcher.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Copyright 2023 The NLP Odyssey Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gdelt
import (
"archive/zip"
"bytes"
"crypto/md5"
"encoding/csv"
"errors"
"fmt"
"html"
"io"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/rs/zerolog/log"
)
const (
// LastUpdateURL provides the last 15 Minutes CSV Data File List – English.
// (Updated every 15 minutes).
LastUpdateURL = "http://data.gdeltproject.org/gdeltv2/lastupdate.txt"
// LastUpdateTranslationURL provides the last 15 Minutes CSV Data File
// List – GDELT Translingual. (Updated every 15 minutes).
LastUpdateTranslationURL = "http://data.gdeltproject.org/gdeltv2/lastupdate-translation.txt"
)
var DefaultOpts = Opts{
AllowedCameoRootCodes: []string{"13", "14", "15", "17", "18", "19", "20"},
SkipDuplicates: true,
SkipFutureEvents: true,
Translingual: false,
MaxTitleLength: 150,
}
// BadStatusCodeError indicates an unexpected HTTP response status code.
// It provides minimal information. It can be wrapped and recognized
// using IsBadStatusCodeError.
type BadStatusCodeError struct {
StatusCode int
}
func (err BadStatusCodeError) Error() string {
return fmt.Sprintf("bad HTTP response status code %d", err.StatusCode)
}
func NewBadStatusCodeError(statusCode int) BadStatusCodeError {
return BadStatusCodeError{StatusCode: statusCode}
}
func IsBadStatusCodeError(err error) bool {
return errors.As(err, &BadStatusCodeError{})
}
// Opts contains options for FetchLatestEvents.
type Opts struct {
SkipDuplicates bool
SkipFutureEvents bool
MaxTitleLength int
Translingual bool
AllowedCameoRootCodes []string
}
// FetchLatestEvents returns the latest GDELT events.
func FetchLatestEvents(opts Opts) (_ []*Event, err error) {
a, err := getLatestEvents(LastUpdateURL)
if err != nil {
return nil, fmt.Errorf("failed to get latest events from %q: %w", LastUpdateURL, err)
}
if !opts.Translingual {
return filterEvents(a, opts)
}
b, err := getLatestEvents(LastUpdateTranslationURL)
if err != nil {
return nil, fmt.Errorf("failed to get latest events from %q: %w", LastUpdateTranslationURL, err)
}
return filterEvents(append(a, b...), opts)
}
func filterEvents(evs []*Event, opts Opts) (_ []*Event, err error) {
result := make([]*Event, 0, len(evs))
visitedURLs := make(map[string]struct{}, len(evs))
for _, ev := range evs {
if len(ev.SourceURL) == 0 || ev.GKGArticle == nil || len(ev.GKGArticle.Extras.PageTitle) == 0 {
continue
}
publishedAt, err := ev.DateAddedTime()
if err != nil {
// Ignore the error, just discard the event.
continue
}
if opts.SkipFutureEvents && publishedAt.After(time.Now()) {
// Ignore future events.
continue
}
if len([]rune(ev.GKGArticle.Extras.PageTitle)) > opts.MaxTitleLength {
// Ignore events with long titles.
continue
}
if !isEventCodeAllowed(opts.AllowedCameoRootCodes, ev.EventRootCode) {
continue
}
if _, ok := visitedURLs[ev.SourceURL]; ok && opts.SkipDuplicates {
continue
}
result = append(result, ev)
visitedURLs[ev.SourceURL] = struct{}{}
}
return result, nil
}
func getLatestEvents(url string) (_ []*Event, err error) {
defer func() {
// Avoid hard failures because of bad server responses.
if IsBadStatusCodeError(err) {
log.Warn().Err(err).Str("URL", url).Msgf("failed to get latest GDELT events")
err = nil
}
}()
fr, err := getFileReferences(url)
if err != nil {
return nil, err
}
evs, err := getEventsFromURL(fr.Export.URL, fr.Export.MD5Sum, fr.Export.Size)
if err != nil {
return nil, fmt.Errorf("failed to get export data: %w", err)
}
articles, err := getArticleFromURL(fr.GKG.URL, fr.GKG.MD5Sum, fr.GKG.Size)
if err != nil {
return nil, fmt.Errorf("failed to get GKG data: %w", err)
}
am := make(map[string]*Article, len(articles))
for _, a := range articles {
if _, ok := am[a.DocumentIdentifier]; ok {
return nil, fmt.Errorf("duplicate document identifier in articles: %q", a.DocumentIdentifier)
}
am[a.DocumentIdentifier] = a
}
for _, e := range evs {
a, ok := am[e.SourceURL]
if !ok {
continue
}
e.GKGArticle = a
}
return evs, nil
}
func isEventCodeAllowed(allowedEventRootCodes []string, currentEventCode string) bool {
if allowedEventRootCodes == nil || len(allowedEventRootCodes) == 0 {
return true
}
for _, code := range allowedEventRootCodes {
if code == currentEventCode {
return true
}
}
return false
}
type fileReference struct {
Size int
MD5Sum string
URL string
}
type fileReferences struct {
Export fileReference
Mentions fileReference
GKG fileReference
}
func getFileReferences(url string) (_ *fileReferences, err error) {
resp, err := httpGetFileReferences(url)
if err != nil {
return nil, fmt.Errorf("failed to HTTP get %q: %w", url, err)
}
frs, err := parseFileReferencesResponse(resp)
if err != nil {
return nil, fmt.Errorf("failed to parse response from %q: %w", url, err)
}
return frs, nil
}
func httpGetFileReferences(url string) (_ string, err error) {
resp, err := http.Get(url)
if err != nil {
return "", fmt.Errorf("HTTP getFileReferences error: %w", err)
}
defer func() {
if e := resp.Body.Close(); e != nil && err == nil {
err = e
}
}()
if resp.StatusCode != http.StatusOK {
return "", NewBadStatusCodeError(resp.StatusCode)
}
bs, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %w", err)
}
return string(bs), err
}
func parseFileReferencesResponse(resp string) (*fileReferences, error) {
resp = strings.TrimSpace(resp)
rows := strings.Split(resp, "\n")
if len(rows) != 3 {
return nil, fmt.Errorf("want 3 rows, got %d", len(rows))
}
frs := new(fileReferences)
for _, row := range rows {
err := parseFileReferencesRow(row, frs)
if err != nil {
return nil, err
}
}
return frs, nil
}
func parseFileReferencesRow(row string, frs *fileReferences) error {
fields := strings.Split(row, " ")
if len(fields) != 3 {
return fmt.Errorf("want 3 fields, got %d", len(fields))
}
size, err := strconv.Atoi(fields[0])
if err != nil {
return fmt.Errorf("failed to parse Size field as int: %q", fields[0])
}
fr := fileReference{
Size: size,
MD5Sum: fields[1],
URL: fields[2],
}
switch {
case strings.HasSuffix(fr.URL, ".export.CSV.zip"):
frs.Export = fr
case strings.HasSuffix(fr.URL, ".mentions.CSV.zip"):
frs.Mentions = fr
case strings.HasSuffix(fr.URL, ".gkg.csv.zip"):
frs.GKG = fr
default:
return fmt.Errorf("unexpected suffix for URL: %q", fr.URL)
}
return nil
}
func getArticleFromURL(url, md5sum string, size int) ([]*Article, error) {
content, err := httpGet(url)
if err != nil {
return nil, fmt.Errorf("failed to HTTP get %q: %w", url, err)
}
if len(content) != size {
return nil, fmt.Errorf("expected content size %d, actual %d", size, len(content))
}
err = checkMD5Sum(content, md5sum)
if err != nil {
return nil, fmt.Errorf("failed to validate %q: %w", url, err)
}
zipReader, err := zip.NewReader(bytes.NewReader(content), int64(size))
if err != nil {
return nil, fmt.Errorf("zip reader error: %w", err)
}
if len(zipReader.File) != 1 {
return nil, fmt.Errorf("want 1 file in zip, got %d", len(zipReader.File))
}
records, err := processArticleFile(zipReader.File[0])
if err != nil {
return nil, err
}
return records, nil
}
func processArticleFile(zf *zip.File) (records []*Article, err error) {
f, err := zf.Open()
if err != nil {
return nil, fmt.Errorf("failed to open zip file: %w", err)
}
defer func() {
if e := f.Close(); e != nil && err == nil {
err = e
}
}()
records = make([]*Article, 0)
r := csv.NewReader(f)
r.Comma = '\t'
r.LazyQuotes = true
for i := 0; ; i++ {
fields, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Warn().Err(err).Int("row", i).Msg("failed to read GDELT GKG CSV record")
continue
}
a, err := makeArticle(fields)
if err != nil {
return nil, err
}
records = append(records, a)
}
return records, nil
}
func makeArticle(fields []string) (a *Article, err error) {
if len(fields) != 27 {
return nil, fmt.Errorf("expected 27 CSV columns, actual %d", len(fields))
}
a = new(Article)
a.ID = fields[0]
a.DocumentIdentifier = fields[4]
a.SharingImage = strings.TrimSpace(fields[18])
a.Extras = parseArticleExtras(fields[26])
return
}
var pageTitleRe = regexp.MustCompile(`<PAGE_TITLE>(.*)</PAGE_TITLE>`)
var spaceRegexp = regexp.MustCompile(`\s`)
func parseArticleExtras(extrasXML string) (ex ArticleExtras) {
sm := pageTitleRe.FindStringSubmatch(extrasXML)
if len(sm) == 2 {
s := html.UnescapeString(sm[1])
s = spaceRegexp.ReplaceAllString(s, " ")
ex.PageTitle = strings.TrimSpace(s)
}
return
}
func httpGet(url string) (_ []byte, err error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer func() {
if e := resp.Body.Close(); e != nil && err == nil {
err = e
}
}()
if resp.StatusCode != http.StatusOK {
return nil, NewBadStatusCodeError(resp.StatusCode)
}
bs, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
return bs, err
}
func getEventsFromURL(url, md5sum string, size int) ([]*Event, error) {
content, err := httpGet(url)
if err != nil {
return nil, fmt.Errorf("failed to HTTP get %q: %w", url, err)
}
if len(content) != size {
return nil, fmt.Errorf("expected content size %d, actual %d", size, len(content))
}
err = checkMD5Sum(content, md5sum)
if err != nil {
return nil, fmt.Errorf("failed to validate %q: %w", url, err)
}
zipReader, err := zip.NewReader(bytes.NewReader(content), int64(size))
if err != nil {
return nil, fmt.Errorf("zip reader error: %w", err)
}
if len(zipReader.File) != 1 {
return nil, fmt.Errorf("want 1 file in zip, got %d", len(zipReader.File))
}
records, err := processEventFile(zipReader.File[0])
if err != nil {
return nil, err
}
return records, nil
}
func processEventFile(zf *zip.File) (records []*Event, err error) {
f, err := zf.Open()
if err != nil {
return nil, fmt.Errorf("failed to open zip file: %w", err)
}
defer func() {
if e := f.Close(); e != nil && err == nil {
err = e
}
}()
records = make([]*Event, 0)
r := newEventsCsvReader(f)
for i := 0; ; i++ {
event, err := r.read()
if err == io.EOF {
break
}
if err != nil {
log.Warn().Err(err).Int("row", i).Msg("failed to read GDELT export CSV record")
continue
}
records = append(records, event)
}
return records, nil
}
func checkMD5Sum(content []byte, expected string) error {
actual := fmt.Sprintf("%x", md5.Sum(content))
if actual != expected {
return fmt.Errorf("md5 sum: expected %q, actual %q", expected, actual)
}
return nil
}