示例#1
0
// retrieveFeed makes the HTTP GET request to the feed url then parses the xml
// into the models we created
func retrieveFeed(args ...interface{}) interface{} {
	res, err := http.Get(args[0].(string))
	if err != nil {
		log.Fatal("error sending GET to url: ", err)
		return feedFetchResult{nil, &_Error{500, "Failed when sending GET request using the url you provided..."}}
	}
	raw, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		log.Fatal("error reading the response of GET: ", err)
		return feedFetchResult{nil, &_Error{500, "Failed when reading the response of GET request using the url you provided..."}}
	}
	var rssStruct models.RSS
	d := xml.NewDecoder(bytes.NewReader(raw))
	// using xml.Unmarshal(raw, &rssStruct) for some reason does not parse channel link
	// but setting the default name space does the trick http://stackoverflow.com/questions/25332175/golang-rss-xml-parsing-atom10link-overrides-link
	d.DefaultSpace = "RssDefault"
	e := d.Decode(&rssStruct)
	if e != nil {
		log.Fatal("error parsing xml: ", e)
		return feedFetchResult{nil, &_Error{500, "Failed when processing the xml..."}}
	}

	feed := models.Feed{}
	feed.Title = rssStruct.Channel.Title
	feed.Description = rssStruct.Channel.Description
	feed.URL = args[0].(string)
	feed.LastPubDate = rssStruct.Channel.PubDate
	feed.FeedURL = rssStruct.Channel.LinkURL
	feed.ImageURL = rssStruct.Channel.Image.URL
	log.Print("itunes image url: ", rssStruct.Channel.ITunesImage)

	feed.Items = make([]*models.FeedItem, len(rssStruct.Channel.Items))
	for i, item := range rssStruct.Channel.Items {

		var feedItem models.FeedItem
		feedItem.Title = item.Title
		feedItem.Description = item.Description
		feedItem.URL = item.Link
		feedItem.MediaURL = item.Enclosure.URL
		feedItem.PubDate = item.PubDate

		feed.Items[i] = &feedItem
	}

	return feedFetchResult{&feed, nil}
}