Skip to content

Commit 3609311

Browse files
Added interface
1 parent 278e69f commit 3609311

5 files changed

Lines changed: 183 additions & 111 deletions

File tree

‎src/chapter2/search/feeds/feed.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type (
1515
Site struct {
1616
Name string `json:"site"`
1717
Uri string `json:"link"`
18+
Type string `json:"type"`
1819
}
1920
)
2021

Lines changed: 74 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,187 @@
11
[
22
{
33
"site" : "npr",
4-
"link" : "http://www.npr.org/rss/rss.php?id=1001"
4+
"link" : "http://www.npr.org/rss/rss.php?id=1001",
5+
"type" : "rss"
56
},
67
{
78
"site" : "npr",
8-
"link" : "http://www.npr.org/rss/rss.php?id=1008"
9+
"link" : "http://www.npr.org/rss/rss.php?id=1008",
10+
"type" : "rss"
911
},
1012
{
1113
"site" : "npr",
12-
"link" : "http://www.npr.org/rss/rss.php?id=1006"
14+
"link" : "http://www.npr.org/rss/rss.php?id=1006",
15+
"type" : "rss"
1316
},
1417
{
1518
"site" : "npr",
16-
"link" : "http://www.npr.org/rss/rss.php?id=1007"
19+
"link" : "http://www.npr.org/rss/rss.php?id=1007",
20+
"type" : "rss"
1721
},
1822
{
1923
"site" : "npr",
20-
"link" : "http://www.npr.org/rss/rss.php?id=1057"
24+
"link" : "http://www.npr.org/rss/rss.php?id=1057",
25+
"type" : "rss"
2126
},
2227
{
2328
"site" : "npr",
24-
"link" : "http://www.npr.org/rss/rss.php?id=1021"
29+
"link" : "http://www.npr.org/rss/rss.php?id=1021",
30+
"type" : "rss"
2531
},
2632
{
2733
"site" : "npr",
28-
"link" : "http://www.npr.org/rss/rss.php?id=1012"
34+
"link" : "http://www.npr.org/rss/rss.php?id=1012",
35+
"type" : "rss"
2936
},
3037
{
3138
"site" : "npr",
32-
"link" : "http://www.npr.org/rss/rss.php?id=1003"
39+
"link" : "http://www.npr.org/rss/rss.php?id=1003",
40+
"type" : "rss"
3341
},
3442
{
3543
"site" : "npr",
36-
"link" : "http://www.npr.org/rss/rss.php?id=2"
44+
"link" : "http://www.npr.org/rss/rss.php?id=2",
45+
"type" : "rss"
3746
},
3847
{
3948
"site" : "npr",
40-
"link" : "http://www.npr.org/rss/rss.php?id=3"
49+
"link" : "http://www.npr.org/rss/rss.php?id=3",
50+
"type" : "rss"
4151
},
4252
{
4353
"site" : "npr",
44-
"link" : "http://www.npr.org/rss/rss.php?id=5"
54+
"link" : "http://www.npr.org/rss/rss.php?id=5",
55+
"type" : "rss"
4556
},
4657
{
4758
"site" : "npr",
48-
"link" : "http://www.npr.org/rss/rss.php?id=13"
59+
"link" : "http://www.npr.org/rss/rss.php?id=13",
60+
"type" : "rss"
4961
},
5062
{
5163
"site" : "npr",
52-
"link" : "http://www.npr.org/rss/rss.php?id=46"
64+
"link" : "http://www.npr.org/rss/rss.php?id=46",
65+
"type" : "rss"
5366
},
5467
{
5568
"site" : "npr",
56-
"link" : "http://www.npr.org/rss/rss.php?id=7"
69+
"link" : "http://www.npr.org/rss/rss.php?id=7",
70+
"type" : "rss"
5771
},
5872
{
5973
"site" : "npr",
60-
"link" : "http://www.npr.org/rss/rss.php?id=10"
74+
"link" : "http://www.npr.org/rss/rss.php?id=10",
75+
"type" : "rss"
6176
},
6277
{
6378
"site" : "npr",
64-
"link" : "http://www.npr.org/rss/rss.php?id=39"
79+
"link" : "http://www.npr.org/rss/rss.php?id=39",
80+
"type" : "rss"
6581
},
6682
{
6783
"site" : "npr",
68-
"link" : "http://www.npr.org/rss/rss.php?id=43"
84+
"link" : "http://www.npr.org/rss/rss.php?id=43",
85+
"type" : "rss"
6986
},
7087
{
7188
"site" : "bbci",
72-
"link" : "http://feeds.bbci.co.uk/news/rss.xml"
89+
"link" : "http://feeds.bbci.co.uk/news/rss.xml",
90+
"type" : "rss"
7391
},
7492
{
7593
"site" : "bbci",
76-
"link" : "http://feeds.bbci.co.uk/news/business/rss.xml"
94+
"link" : "http://feeds.bbci.co.uk/news/business/rss.xml",
95+
"type" : "rss"
7796
},
7897
{
7998
"site" : "bbci",
80-
"link" : "http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml"
99+
"link" : "http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml",
100+
"type" : "rss"
81101
},
82102
{
83103
"site" : "cnn",
84-
"link" : "http://rss.cnn.com/rss/cnn_topstories.rss"
104+
"link" : "http://rss.cnn.com/rss/cnn_topstories.rss",
105+
"type" : "rss"
85106
},
86107
{
87108
"site" : "cnn",
88-
"link" : "http://rss.cnn.com/rss/cnn_world.rss"
109+
"link" : "http://rss.cnn.com/rss/cnn_world.rss",
110+
"type" : "rss"
89111
},
90112
{
91113
"site" : "cnn",
92-
"link" : "http://rss.cnn.com/rss/cnn_us.rss"
114+
"link" : "http://rss.cnn.com/rss/cnn_us.rss",
115+
"type" : "rss"
93116
},
94117
{
95118
"site" : "cnn",
96-
"link" : "http://rss.cnn.com/rss/cnn_allpolitics.rss"
119+
"link" : "http://rss.cnn.com/rss/cnn_allpolitics.rss",
120+
"type" : "rss"
97121
},
98122
{
99123
"site" : "cnn",
100-
"link" : "http://rss.cnn.com/rss/cnn_crime.rss"
124+
"link" : "http://rss.cnn.com/rss/cnn_crime.rss",
125+
"type" : "rss"
101126
},
102127
{
103128
"site" : "cnn",
104-
"link" : "http://rss.cnn.com/rss/cnn_tech.rss"
129+
"link" : "http://rss.cnn.com/rss/cnn_tech.rss",
130+
"type" : "rss"
105131
},
106132
{
107133
"site" : "cnn",
108-
"link" : "http://rss.cnn.com/rss/cnn_health.rss"
134+
"link" : "http://rss.cnn.com/rss/cnn_health.rss",
135+
"type" : "rss"
109136
},
110137
{
111138
"site" : "foxnews",
112-
"link" : "http://rss.cnn.com/rss/cnn_topstories.rss"
139+
"link" : "http://rss.cnn.com/rss/cnn_topstories.rss",
140+
"type" : "rss"
113141
},
114142
{
115143
"site" : "foxnews",
116-
"link" : "http://feeds.foxnews.com/foxnews/opinion?format=xml"
144+
"link" : "http://feeds.foxnews.com/foxnews/opinion?format=xml",
145+
"type" : "rss"
117146
},
118147
{
119148
"site" : "foxnews",
120-
"link" : "http://feeds.foxnews.com/foxnews/politics?format=xml"
149+
"link" : "http://feeds.foxnews.com/foxnews/politics?format=xml",
150+
"type" : "rss"
121151
},
122152
{
123153
"site" : "foxnews",
124-
"link" : "http://feeds.foxnews.com/foxnews/national?format=xml"
154+
"link" : "http://feeds.foxnews.com/foxnews/national?format=xml",
155+
"type" : "rss"
125156
},
126157
{
127158
"site" : "foxnews",
128-
"link" : "http://feeds.foxnews.com/foxnews/world?format=xml"
159+
"link" : "http://feeds.foxnews.com/foxnews/world?format=xml",
160+
"type" : "rss"
129161
},
130162
{
131163
"site" : "nbcnews",
132-
"link" : "http://feeds.nbcnews.com/feeds/topstories"
164+
"link" : "http://feeds.nbcnews.com/feeds/topstories",
165+
"type" : "rss"
133166
},
134167
{
135168
"site" : "nbcnews",
136-
"link" : "http://feeds.nbcnews.com/feeds/usnews"
169+
"link" : "http://feeds.nbcnews.com/feeds/usnews",
170+
"type" : "rss"
137171
},
138172
{
139173
"site" : "nbcnews",
140-
"link" : "http://rss.msnbc.msn.com/id/21491043/device/rss/rss.xml"
174+
"link" : "http://rss.msnbc.msn.com/id/21491043/device/rss/rss.xml",
175+
"type" : "rss"
141176
},
142177
{
143178
"site" : "nbcnews",
144-
"link" : "http://rss.msnbc.msn.com/id/21491571/device/rss/rss.xml"
179+
"link" : "http://rss.msnbc.msn.com/id/21491571/device/rss/rss.xml",
180+
"type" : "rss"
145181
},
146182
{
147183
"site" : "nbcnews",
148-
"link" : "http://rss.msnbc.msn.com/id/28180066/device/rss/rss.xml"
184+
"link" : "http://rss.msnbc.msn.com/id/28180066/device/rss/rss.xml",
185+
"type" : "rss"
149186
}
150187
]

‎src/chapter2/search/find/find.go‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package find
2+
3+
import (
4+
"log"
5+
6+
"github.com/goinaction/code/src/chapter2/search/feeds"
7+
)
8+
9+
type (
10+
// SearchData contains the data we can search on.
11+
SearchData struct {
12+
Title string
13+
Description string
14+
}
15+
16+
// Result contains the result of a search.
17+
Result struct {
18+
Field string
19+
Content string
20+
}
21+
)
22+
23+
type (
24+
// Matcher defines the behavior required by the Search function.
25+
Matcher interface {
26+
Retrieve(site feeds.Site) ([]SearchData, error)
27+
Match(document []SearchData, searchTerm string) ([]Result, error)
28+
}
29+
)
30+
31+
// Search pulls down each feed looking for the search term.
32+
func Search(matcher Matcher, searchTerm string, site feeds.Site, captureResults chan []Result) {
33+
// Make sure each find returns a result.
34+
var err error
35+
defer func() {
36+
if err != nil {
37+
captureResults <- nil
38+
}
39+
}()
40+
41+
log.Printf("Search Feed Type[%s] Site[%s] For Uri[%s]\n", site.Type, site.Name, site.Uri)
42+
43+
// Retrieve the search data.
44+
searchData, err := matcher.Retrieve(site)
45+
if err != nil {
46+
log.Printf("%s : %s", site.Uri, err)
47+
return
48+
}
49+
50+
// Search the data for the search term.
51+
searchResults, err := matcher.Match(searchData, searchTerm)
52+
if err != nil {
53+
log.Printf("%s : %s", site.Uri, err)
54+
return
55+
}
56+
57+
// Write the results to the channel.
58+
captureResults <- searchResults
59+
}

‎src/chapter2/search/main.go‎

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55

66
"github.com/goinaction/code/src/chapter2/search/feeds"
7+
"github.com/goinaction/code/src/chapter2/search/find"
78
"github.com/goinaction/code/src/chapter2/search/rss"
89
)
910

@@ -20,60 +21,29 @@ func main() {
2021

2122
// Perpare the slice of results and the channel to
2223
// retrieve the results on.
23-
captureResults := make(chan []rss.SearchResult)
24+
results := make(chan []find.Result)
2425

25-
// Launch goroutines to find us our results.
26+
// Launch a goroutine for each feed to find the results.
2627
for _, site := range sites {
27-
go find(searchTerm, site, captureResults)
28+
var matcher find.Matcher
29+
30+
// Create the right type of matcher for this search.
31+
switch site.Type {
32+
case "rss":
33+
matcher = new(rss.Search)
34+
}
35+
36+
// Launch the goroutine to perform the search.
37+
go find.Search(matcher, searchTerm, site, results)
2838
}
2939

3040
// Wait for the result from each goroutine
3141
for site := 0; site < len(sites); site++ {
3242
select {
33-
case findResults := <-captureResults:
34-
for _, result := range findResults {
35-
display(&result)
43+
case found := <-results:
44+
for _, result := range found {
45+
log.Printf("%s:\n%s\n\n", result.Field, result.Content)
3646
}
3747
}
3848
}
3949
}
40-
41-
// find pulls down each feed and searches for the results.
42-
func find(searchTerm string, site feeds.Site, captureResults chan []rss.SearchResult) {
43-
// Make sure each find returns a result.
44-
var err error
45-
defer func() {
46-
if err != nil {
47-
captureResults <- nil
48-
}
49-
}()
50-
51-
log.Printf("Search Feed Site[%s] For Uri[%s]\n", site.Name, site.Uri)
52-
53-
// Retrieve the RSS feed document.
54-
document, err := rss.Retrieve(site.Uri)
55-
if err != nil {
56-
log.Printf("%s : %s", site.Uri, err)
57-
return
58-
}
59-
60-
// Search the document for the search term.
61-
searchResults, err := rss.Search(document, searchTerm)
62-
if err != nil {
63-
log.Printf("%s : %s", site.Uri, err)
64-
return
65-
}
66-
67-
// Write the results to the channel.
68-
captureResults <- searchResults
69-
}
70-
71-
// display logs the results of the serach to the console.
72-
func display(result *rss.SearchResult) {
73-
switch result.Field {
74-
case "Title":
75-
log.Printf("Title:\n%s\n\n", result.Document.Title)
76-
case "Description":
77-
log.Printf("Description:\n%s\n\n", result.Document.Description)
78-
}
79-
}

0 commit comments

Comments
 (0)