forked from ardanlabs/gotraining
/
search.go
107 lines (86 loc) · 2.75 KB
/
search.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
// Package search manages the searching of results against different
// news feeds.
package search
import (
"html/template"
"log"
)
// Options provides the search options for performing searches.
type Options struct {
Term string
CNN bool
NYT bool
BBC bool
First bool
}
// Result represents a search result that was found.
type Result struct {
Engine string
Title string
Link string
Content string
}
// TitleHTML fixes encoding issues.
func (r *Result) TitleHTML() template.HTML {
return template.HTML(r.Title)
}
// ContentHTML fixes encoding issues.
func (r *Result) ContentHTML() template.HTML {
return template.HTML(r.Content)
}
// =============================================================================
// Searcher declares an interface used to leverage different
// search engines to find results.
type Searcher interface {
Search(uid string, term string, found chan<- []Result)
}
// =============================================================================
// Submit uses goroutines and channels to perform a search against the
// feeds concurrently.
func Submit(uid string, options Options) []Result {
log.Printf("%s : Submit : Started : %#v\n", uid, options)
searchers := make(map[string]Searcher)
// Create a CNN Searcher if checked.
if options.CNN {
log.Printf("%s : Submit : Info : Adding CNN\n", uid)
searchers["cnn"] = NewCNN()
}
// Create a NYT Searcher if checked.
if options.NYT {
log.Printf("%s : Submit : Info : Adding NYT\n", uid)
searchers["nyt"] = NewNYT()
}
// Create a BBC Searcher if checked.
if options.BBC {
log.Printf("%s : Submit : Info : Adding BBC\n", uid)
searchers["bbc"] = NewBBC()
}
results := make(chan []Result)
// Perform the searches concurrently. Using a map because
// it returns the searchers in a random order every time.
for _, searcher := range searchers {
go searcher.Search(uid, options.Term, results)
}
var final []Result
// Wait for the results to come back.
for search := 0; search < len(searchers); search++ {
// If we just want the first result, don't wait any longer by
// concurrently discarding the remaining results.
// Failing to do so will leave the Searchers blocked forever.
if options.First && (search > 0 && len(final) > 0) {
go func() {
found := <-results
log.Printf("%s : Submit : Info : Results Discarded : Results[%d]\n", uid, len(found))
}()
continue
}
// Wait to recieve results.
log.Printf("%s : Submit : Info : Waiting For Results...\n", uid)
found := <-results
// Save the results to the final slice.
log.Printf("%s : Submit : Info : Results Used : Results[%d]\n", uid, len(found))
final = append(final, found...)
}
log.Printf("%s : Submit : Completed : Found [%d] Results\n", uid, len(final))
return final
}