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
| package matchers
import ( "encoding/xml" "errors" "fmt" "log" "net/http" "regexp"
"search" )
type ( item struct { XMLName xml.Name `xml:"item"` PubDate string `xml:"pubDate"` Title string `xml:"title"` Description string `xml:"description"` Link string `xml:"link"` GUID string `xml:"guid"` GeoRssPoint string `xml:"georss:point"` }
image struct { XMLNAME xml.Name `xml:"image"` URL string `xml:"url"` Title string `xml:"title"` Link string `xml:"link"` }
channel struct { XMLNAME xml.Name `xml:"channel"` Title string `xml:"title"` Description string `xml:"description"` Link string `xml:"link"` PubDate string `xml:"pubDate"` LastBuildDate string `xml:"lastBuildDate"` TTL string `xml:"ttl"` Language string `xml:"language"` ManagingEditor string `xml:"managingEditor"` WebMaster string `xml:"webMaster"` Image image `xml:"image"` Item []item `xml:"item"` } )
type rssMatcher struct {}
func init() { var matcher rssMatcher search.Register("rss", matcher) }
func (m rssMatcher) Search(feed *search.Feed, searchTerm string) ([]*search.Result, error) { var results []*result.Result log.Printf("Search Feed Type[%s] Site[%s] For URI[%s]\n", feed.Type, feed.Name, feed.URI)
document, err := m.retrieve(feed) if err != nil { return nil, err }
for _, channelItem := range document.Channel.Item { matched, err := regexp.MatchString(searchTerm, channelItem.Title) if err != nil { return nil, err }
if matched { results = append(results, &search.Result { Field: "Title", Content: channelItem.Title, }) } } return results, nil }
func (m rssMatcher) retrieve(feed *search.Feed) (*rssDocument, error) { if feed.URI == "" { return nil, errors.New("No rss feed url provided") }
resp, err := http.Get(feed.URI) if err != nil { return nil, err }
defer resp.Body.Close()
if resp.StatusCode != 200 { return nil, fmt.Errorf("HTTP Response Error %d\n", resp.StatusCode) }
var document rssDocument err = xml.NewDecoder(resp.Body).Decode(&document) return &document, err }
|