-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroutes.go
655 lines (581 loc) · 18.9 KB
/
routes.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// Social Harvest is a social media analytics platform.
// Copyright (C) 2014 Tom Maiaroto, Shift8Creative, LLC (http://www.socialharvest.io)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// This file contains all the functions to handle the API routes.
package main
import (
"bytes"
"github.com/SocialHarvest/harvester/lib/config"
"github.com/advancedlogic/GoOse"
"github.com/ant0ine/go-json-rest/rest"
"log"
"net/http"
"strconv"
"strings"
"time"
)
// Returns information about the currently configured database, if it's reachable, etc.
func DatabaseInfo(w rest.ResponseWriter, r *rest.Request) {
res := config.NewHypermediaResource()
res.Links["database:info"] = config.HypermediaLink{
Href: "/database/info",
}
if db.Postgres != nil {
res.Data["type"] = "postgres"
// SELECT * FROM has_database_privilege('username', 'database', 'connect');
// var r struct {
// hasAccess string `db:"has_database_privilege" json:"has_database_privilege"`
// }
//err := socialHarvest.Database.Postgres.Get(&r, "SELECT * FROM has_database_privilege("+socialHarvest.Config.Database.User+", "+socialHarvest.Config.Database.Database+", 'connect')")
//res.Data["r"] = r
//res.Data["err"] = err
res.Data["hasAccess"] = db.HasAccess()
}
if db.InfluxDB != nil {
res.Data["type"] = "infxludb"
}
res.Success()
w.WriteJson(res.End())
}
// Territory aggregates (gender, language, etc.) shows a breakdown and count of various values and their percentage of total
func TerritoryAggregateData(w rest.ResponseWriter, r *rest.Request) {
res := setTerritoryLinks("territory:aggregate")
params, fields, extraParams := buildAggregateParams(r)
if params.Territory != "" && params.Series != "" && len(fields) > 0 {
var total ResultCount
res.Data["aggregate"], total = db.FieldCounts(params, fields, extraParams)
res.Data["total"] = total.Count
res.Success()
} else {
res.Data["aggregate"] = nil
res.Data["total"] = 0
}
w.WriteJson(res.End())
}
// Returns a simple count based on various conditions.
func TerritoryCountData(w rest.ResponseWriter, r *rest.Request) {
res := setTerritoryLinks("territory:count")
territory := r.PathParam("territory")
series := r.PathParam("series")
field := r.PathParam("field")
queryParams := r.URL.Query()
timeFrom := ""
if len(queryParams["from"]) > 0 {
timeFrom = queryParams["from"][0]
}
timeTo := ""
if len(queryParams["to"]) > 0 {
timeTo = queryParams["to"][0]
}
fieldValue := ""
if len(queryParams["fieldValue"]) > 0 {
fieldValue = queryParams["fieldValue"][0]
}
network := ""
if len(queryParams["network"]) > 0 {
network = queryParams["network"][0]
}
// Limit and Skip
limit := uint64(100)
if len(queryParams["limit"]) > 0 {
l, lErr := strconv.ParseUint(queryParams["limit"][0], 10, 64)
if lErr == nil {
limit = uint64(l)
}
if limit > 100 {
limit = 100
}
if limit < 1 {
limit = 1
}
}
skip := uint64(0)
if len(queryParams["skip"]) > 0 {
sk, skErr := strconv.ParseUint(queryParams["skip"][0], 10, 64)
if skErr == nil {
skip = uint64(sk)
}
if skip < 0 {
skip = 0
}
}
params := CommonQueryParams{
Series: series,
Territory: territory,
Field: field,
Network: network,
From: timeFrom,
To: timeTo,
Skip: skip,
Limit: limit,
}
var count ResultCount
count = db.Count(params, fieldValue)
res.Data["count"] = count.Count
res.Data["limit"] = limit
res.Data["skip"] = skip
res.Meta.From = count.TimeFrom
res.Meta.To = count.TimeTo
res.Success()
w.WriteJson(res.End())
}
// Returns the top images for a given territory
func TerritoryTopImages(w rest.ResponseWriter, r *rest.Request) {
res := setTerritoryLinks("territory:top-images")
params, fields, extraParams := buildAggregateParams(r)
// override, we know the field we want and its just one in this case
fields = []string{"expanded_url"}
// same with the series
params.Series = "shared_links"
// special params
extraParams["type"] = " IN('photo','image')"
if params.Territory != "" && params.Series != "" && len(fields) > 0 {
var total ResultCount
res.Data["aggregate"], total = db.FieldCounts(params, fields, extraParams)
res.Data["total"] = total.Count
res.Success()
} else {
res.Data["aggregate"] = nil
res.Data["total"] = 0
}
w.WriteJson(res.End())
}
// Returns the top videos for a given territory
func TerritoryTopVideos(w rest.ResponseWriter, r *rest.Request) {
res := setTerritoryLinks("territory:top-videos")
params, fields, extraParams := buildAggregateParams(r)
// override, we know the field we want and its just one in this case
fields = []string{"expanded_url"}
// same with the series
params.Series = "shared_links"
// special params
extraParams["type"] = " = 'video'"
if params.Territory != "" && params.Series != "" && len(fields) > 0 {
var total ResultCount
res.Data["aggregate"], total = db.FieldCounts(params, fields, extraParams)
res.Data["total"] = total.Count
res.Success()
} else {
res.Data["aggregate"] = nil
res.Data["total"] = 0
}
w.WriteJson(res.End())
}
// Returns the top audio for a given territory
func TerritoryTopAudio(w rest.ResponseWriter, r *rest.Request) {
res := setTerritoryLinks("territory:top-audio")
params, fields, extraParams := buildAggregateParams(r)
// override, we know the field we want and its just one in this case
fields = []string{"expanded_url"}
// same with the series
params.Series = "shared_links"
// special params
extraParams["type"] = " = 'audio'"
if params.Territory != "" && params.Series != "" && len(fields) > 0 {
var total ResultCount
res.Data["aggregate"], total = db.FieldCounts(params, fields, extraParams)
res.Data["total"] = total.Count
res.Success()
} else {
res.Data["aggregate"] = nil
res.Data["total"] = 0
}
w.WriteJson(res.End())
}
// Returns the top non video/image/audio links for a given territory
func TerritoryTopLinks(w rest.ResponseWriter, r *rest.Request) {
res := setTerritoryLinks("territory:top-links")
params, fields, extraParams := buildAggregateParams(r)
// override, we know the field we want and its just one in this case
fields = []string{"expanded_url"}
// same with the series
params.Series = "shared_links"
// special params
extraParams["type"] = " = ''"
if params.Territory != "" && params.Series != "" && len(fields) > 0 {
var total ResultCount
res.Data["aggregate"], total = db.FieldCounts(params, fields, extraParams)
res.Data["total"] = total.Count
res.Success()
} else {
res.Data["aggregate"] = nil
res.Data["total"] = 0
}
w.WriteJson(res.End())
}
// Returns the top keywords for a given territory (primarily a convenience route for a simple aggregate, also makes use of LOWER())
func TerritoryTopKeywords(w rest.ResponseWriter, r *rest.Request) {
res := setTerritoryLinks("territory:top-keywords")
params, fields, extraParams := buildAggregateParams(r)
// override, we know the field we want and its just one in this case
fields = []string{"LOWER(keyword)"}
// same with the series
params.Series = "hashtags"
if params.Territory != "" && params.Series != "" && len(fields) > 0 {
var total ResultCount
res.Data["aggregate"], total = db.FieldCounts(params, fields, extraParams)
res.Data["total"] = total.Count
res.Success()
} else {
res.Data["aggregate"] = nil
res.Data["total"] = 0
}
w.WriteJson(res.End())
}
// Returns the top hashtags for a given territory (primarily a convenience route for a simple aggregate, also makes use of LOWER())
func TerritoryTopHashtags(w rest.ResponseWriter, r *rest.Request) {
res := setTerritoryLinks("territory:top-hashtags")
params, fields, extraParams := buildAggregateParams(r)
// override, we know the field we want and its just one in this case
fields = []string{"LOWER(tag)"}
// same with the series
params.Series = "hashtags"
if params.Territory != "" && params.Series != "" && len(fields) > 0 {
var total ResultCount
res.Data["aggregate"], total = db.FieldCounts(params, fields, extraParams)
res.Data["total"] = total.Count
res.Success()
} else {
res.Data["aggregate"] = nil
res.Data["total"] = 0
}
w.WriteJson(res.End())
}
// Returns the top locations for a given territory
func TerritoryTopLocations(w rest.ResponseWriter, r *rest.Request) {
res := setTerritoryLinks("territory:top-locations")
queryParams := r.URL.Query()
params, fields, extraParams := buildAggregateParams(r)
// override the fields, we know the field we want and its just one in this case ... but with an optional precision value
precision := 7
var err error
if len(queryParams["precision"]) > 0 {
precision, err = strconv.Atoi(queryParams["precision"][0])
if err != nil {
precision = 7
}
}
// Keep it within the limits
if precision > 12 {
precision = 12
}
if precision < 1 {
precision = 1
}
var buffer bytes.Buffer
buffer.WriteString("substring(contributor_geohash, 1,")
buffer.WriteString(strconv.Itoa(precision))
buffer.WriteString(")")
geohash := buffer.String()
buffer.Reset()
fields = []string{geohash}
// same with the series
params.Series = "messages"
if params.Territory != "" && params.Series != "" && len(fields) > 0 {
var total ResultCount
res.Data["aggregate"], total = db.FieldCounts(params, fields, extraParams)
res.Data["total"] = total.Count
res.Success()
} else {
res.Data["aggregate"] = nil
res.Data["total"] = 0
}
w.WriteJson(res.End())
}
// Returns a simple count based on various conditions in a streaming time series.
func TerritoryTimeseriesCountData(w rest.ResponseWriter, r *rest.Request) {
territory := r.PathParam("territory")
series := r.PathParam("series")
field := r.PathParam("field")
queryParams := r.URL.Query()
timeFrom := ""
if len(queryParams["from"]) > 0 {
timeFrom = queryParams["from"][0]
}
timeTo := ""
if len(queryParams["to"]) > 0 {
timeTo = queryParams["to"][0]
}
fieldValue := ""
if len(queryParams["fieldValue"]) > 0 {
fieldValue = queryParams["fieldValue"][0]
}
network := ""
if len(queryParams["network"]) > 0 {
network = queryParams["network"][0]
}
params := CommonQueryParams{
Series: series,
Territory: territory,
Field: field,
Network: network,
From: timeFrom,
To: timeTo,
}
// in minutes
resolution := 0
if len(queryParams["resolution"]) > 0 {
parsedResolution, err := strconv.Atoi(queryParams["resolution"][0])
if err == nil {
resolution = parsedResolution
}
}
if resolution != 0 && territory != "" && series != "" {
// only accepting days for now - not down to minutes or hours (yet)
tF, _ := time.Parse("2006-01-02", timeFrom)
tT, _ := time.Parse("2006-01-02", timeTo)
timeRange := tT.Sub(tF)
//totalRangeMinutes := int(timeRange.Minutes())
periodsInRange := int(timeRange.Minutes() / float64(resolution))
w.Header().Set("Content-Type", "application/json")
var count ResultCount
for i := 0; i < periodsInRange; i++ {
params.From = tF.Format("2006-01-02 15:04:05")
tF = tF.Add(time.Duration(resolution) * time.Minute)
params.To = tF.Format("2006-01-02 15:04:05")
count = db.Count(params, fieldValue)
w.WriteJson(count)
w.(http.ResponseWriter).Write([]byte("\n"))
// Flush the buffer to client immediately
// (for most cases, this stream will be quick and short - just how we like it. for the more crazy requests, it may take a little while and that's ok too)
w.(http.Flusher).Flush()
}
}
}
// API: Returns the messages (paginated) for a territory with the ability to filter by question or not, etc.
func TerritoryMessages(w rest.ResponseWriter, r *rest.Request) {
res := setTerritoryLinks("territory:messages")
territory := r.PathParam("territory")
queryParams := r.URL.Query()
timeFrom := ""
if len(queryParams["from"]) > 0 {
timeFrom = queryParams["from"][0]
}
timeTo := ""
if len(queryParams["to"]) > 0 {
timeTo = queryParams["to"][0]
}
network := ""
if len(queryParams["network"]) > 0 {
network = queryParams["network"][0]
}
// Limit and Skip
limit := uint64(100)
if len(queryParams["limit"]) > 0 {
l, lErr := strconv.ParseUint(queryParams["limit"][0], 10, 64)
if lErr == nil {
limit = uint64(l)
}
if limit > 100 {
limit = 100
}
if limit < 1 {
limit = 1
}
}
skip := uint64(0)
if len(queryParams["skip"]) > 0 {
sk, skErr := strconv.ParseUint(queryParams["skip"][0], 10, 64)
if skErr == nil {
skip = uint64(sk)
}
if skip < 0 {
skip = 0
}
}
// Build the conditions
var conditions = BasicConditions{}
// Condition for questions
if len(queryParams["questions"]) > 0 {
conditions.IsQuestion = 1
}
// Gender condition
if len(queryParams["gender"]) > 0 {
conditions.Gender = queryParams["gender"][0]
}
// Language condition
if len(queryParams["lang"]) > 0 {
conditions.Lang = queryParams["lang"][0]
}
// Country condition
if len(queryParams["country"]) > 0 {
conditions.Country = queryParams["country"][0]
}
// Geohash condition (nearby)
if len(queryParams["geohash"]) > 0 {
conditions.Geohash = queryParams["geohash"][0]
}
params := CommonQueryParams{
Series: "messages",
Territory: territory,
Network: network,
From: timeFrom,
To: timeTo,
Limit: limit,
Skip: skip,
}
messages, total, skip, limit := db.Messages(params, conditions)
res.Data["messages"] = messages
res.Data["total"] = total
res.Data["limit"] = limit
res.Data["skip"] = skip
//db.Messages(params, conditions)
res.Success()
w.WriteJson(res.End())
}
// Returns all currently configured territories and their settings
func TerritoryList(w rest.ResponseWriter, r *rest.Request) {
res := setTerritoryLinks("territory:list")
res.Data["territories"] = socialHarvest.Config.Harvest.Territories
res.Success()
w.WriteJson(res.End())
}
// Sets the hypermedia response "_links" section with all of the routes we have defined for territories.
func setTerritoryLinks(self string) *config.HypermediaResource {
res := config.NewHypermediaResource()
res.Links["territory:list"] = config.HypermediaLink{
Href: "/territory/list",
}
res.Links["territory:count"] = config.HypermediaLink{
Href: "/territory/count/{territory}/{series}/{field}{?from,to,network,fieldValue}",
}
res.Links["territory:timeseries-count"] = config.HypermediaLink{
Href: "/territory/timeseries/count/{territory}/{series}/{field}{?from,to,network,fieldValue}",
}
res.Links["territory:aggregate"] = config.HypermediaLink{
Href: "/territory/aggregate/{territory}/{series}{?from,to,network,fields}",
}
res.Links["territory:timeseries-aggregate"] = config.HypermediaLink{
Href: "/territory/timeseries/aggregate/{territory}/{series}{?from,to,network,fields,resolution}",
}
res.Links["territory:messages"] = config.HypermediaLink{
Href: "/territory/messages/{territory}{?from,to,limit,skip,network,lang,country,geohash,gender,questions}",
}
res.Links["territory:top-images"] = config.HypermediaLink{
Href: "/territory/top/images/{territory}/{series}{?from,to,network}",
}
res.Links["territory:top-videos"] = config.HypermediaLink{
Href: "/territory/top/videos/{territory}/{series}{?from,to,network}",
}
res.Links["territory:top-audio"] = config.HypermediaLink{
Href: "/territory/top/audio/{territory}/{series}{?from,to,network}",
}
res.Links["territory:top-links"] = config.HypermediaLink{
Href: "/territory/top/links/{territory}/{series}{?from,to,network}",
}
res.Links["territory:top-locations"] = config.HypermediaLink{
Href: "/territory/top/locations/{territory}/{series}{?from,to,network}",
}
res.Links["territory:top-keywords"] = config.HypermediaLink{
Href: "/territory/top/keywords/{territory}/{series}{?from,to,network}",
}
res.Links["territory:top-hashtags"] = config.HypermediaLink{
Href: "/territory/top/hashtags/{territory}/{series}{?from,to,network}",
}
selfedRes := config.NewHypermediaResource()
for link, _ := range res.Links {
if link == self {
selfedRes.Links["self"] = res.Links[link]
} else {
selfedRes.Links[link] = res.Links[link]
}
}
return selfedRes
}
// --------- Utility API end points ---------
// Retrieves information to provide a summary about a give URL, specifically articles/blog posts.
// TODO: Make this more robust (more details, videos, etc.). Some of this may eventually also go into the harvest.
// TODO: Likely fork this package and add in some of the things I did for Virality Score in order to get even more data.
func LinkDetails(w rest.ResponseWriter, r *rest.Request) {
res := config.NewHypermediaResource()
res.Links["self"] = config.HypermediaLink{
Href: "/link/details{?url}",
}
queryParams := r.URL.Query()
if len(queryParams["url"]) > 0 {
g := goose.New()
article := g.ExtractFromUrl(queryParams["url"][0])
log.Println(article)
res.Data["title"] = article.Title
res.Data["published"] = article.PublishDate
res.Data["favicon"] = article.MetaFavicon
res.Data["domain"] = article.Domain
res.Data["description"] = article.MetaDescription
res.Data["keywords"] = article.MetaKeywords
res.Data["content"] = article.CleanedText
res.Data["url"] = article.FinalUrl
res.Data["image"] = article.TopImage
res.Data["movies"] = article.Movies
res.Success()
}
w.WriteJson(res.End())
}
func buildAggregateParams(r *rest.Request) (CommonQueryParams, []string, map[string]string) {
territory := r.PathParam("territory")
series := r.PathParam("series")
queryParams := r.URL.Query()
extraParams := make(map[string]string)
params := CommonQueryParams{}
var err error
// Fields to group by
fields := []string{}
if len(queryParams["fields"]) > 0 {
fields = strings.Split(queryParams["fields"][0], ",")
// trim any white space
for i, val := range fields {
fields[i] = strings.Trim(val, " ")
}
}
timeFrom := ""
if len(queryParams["from"]) > 0 {
timeFrom = queryParams["from"][0]
}
timeTo := ""
if len(queryParams["to"]) > 0 {
timeTo = queryParams["to"][0]
}
network := ""
if len(queryParams["network"]) > 0 {
timeTo = queryParams["network"][0]
}
limit := 0
if len(queryParams["limit"]) > 0 {
parsedLimit, err := strconv.Atoi(queryParams["limit"][0])
if err == nil {
limit = parsedLimit
} else {
log.Println("Error parsing limit param:")
log.Println(err)
}
}
skip := 0
if len(queryParams["skip"]) > 0 {
parsedSkip, skipErr := strconv.Atoi(queryParams["skip"][0])
if skipErr == nil {
skip = parsedSkip
} else {
log.Println("Error parsing skip param:")
log.Println(err)
}
}
params.Series = series
params.Territory = territory
params.Network = network
params.From = timeFrom
params.To = timeTo
params.Limit = uint64(limit)
params.Skip = uint64(skip)
return params, fields, extraParams
}