예제 #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}
}
예제 #2
0
func (pg *PostgresDBManager) SaveFeed(feed *models.Feed) bool {
	var feedStmt, itemStmt *sql.Stmt
	var err error
	var randID uuid.UUID
	// save feed to db
	feedStmt, err = pg.db.Prepare("INSERT INTO feeds VALUES($1, $2, $3, $4, $5, $6, $7)")
	if err != nil {
		log.Fatal(err)
		return false
	}

	// add id
	randID, err = uuid.V4()
	if err != nil {
		log.Fatal(err)
		return false
	}

	feed.ID = randID.String()
	_, err = feedStmt.Exec(feed.ID, feed.Title, feed.Description, feed.URL, feed.FeedURL, feed.LastPubDate, feed.ImageURL)
	if err != nil {
		log.Fatal(err)
		return false
	}

	// save feed Items
	itemStmt, err = pg.db.Prepare("INSERT INTO feed_items VALUES($1, $2, $3, $4, $5, $6, $7, $8)")
	if err != nil {
		log.Fatal(err)
		return false
	}

	for _, item := range feed.Items {
		randID, err = uuid.V4()
		if err != nil {
			log.Fatal(err)
			return false
		}
		item.ID = randID.String()
		_, err = itemStmt.Exec(item.ID, item.Title, item.Description, item.URL, item.MediaURL, item.PubDate, item.ImageURL, feed.ID)
		if err != nil {
			log.Fatal(err)
			return false
		}
	}

	return true
}