Exemplo n.º 1
0
func formatAtom(sub *Subscription, doc *xml.XmlDocument, articles *[]Article) {
	entries, err := doc.Search("//entry")
	if err != nil {
		log.Println("Error parsing Atom entries on feed: " + err.Error())
		return
	}
	for _, v := range entries {
		var article Article
		// Subscription ID
		article.SubscriptionId = sub.Id
		// Title
		titleNodes, _ := v.Search(v.Path() + "/title")
		article.Title = titleNodes[0].Content()
		// Link
		linkNodes, _ := v.Search(v.Path() + "/link")
		article.Url = linkNodes[0].Attr("href")
		// Published
		dateNodes, _ := v.Search(v.Path() + "/published")
		pub, _ := time.Parse(time.RFC3339, dateNodes[0].Content())
		article.Published = pub.UTC()
		// Author
		authorNodes, _ := v.Search(v.Path() + "/author/name")
		if len(authorNodes) > 0 {
			article.Author = authorNodes[0].Content()
		}
		// Summary
		summaryNodes, _ := v.Search(v.Path() + "/summary")
		if len(summaryNodes) > 0 {
			article.Summary.Content = summaryNodes[0].Content()
			article.Summary.Type = summaryNodes[0].Attr("type")
		}
		// Body
		bodyNodes, _ := v.Search(v.Path() + "/content")
		if len(bodyNodes) > 0 {
			article.Body.Content = bodyNodes[0].Content()
			article.Body.Type = bodyNodes[0].Attr("type")
		}
		// Read
		article.Read = false
		*articles = append(*articles, Article{})
		copy((*articles)[0+1:], (*articles)[0:])
		(*articles)[0] = article
	}
	// rfc3339
}
Exemplo n.º 2
0
func formatRSS(sub *Subscription, doc *xml.XmlDocument, articles *[]Article) {
	items, err := doc.Search("//item")
	if err != nil {
		log.Println("Error parsing RSS entries on feed: " + err.Error())
		return
	}
	for _, v := range items {
		var article Article
		// Subscription ID
		article.SubscriptionId = sub.Id
		// Title
		titleNodes, _ := v.Search(v.Path() + "/title")
		article.Title = titleNodes[0].Content()
		// Link
		linkNodes, _ := v.Search(v.Path() + "/link")
		article.Url = linkNodes[0].Content()
		// Published
		dateNodes, _ := v.Search(v.Path() + "/pubDate")
		pub, err := time.Parse(time.RFC1123Z, dateNodes[0].Content())
		if err != nil {
			pub, _ = time.Parse(time.RFC1123, dateNodes[0].Content())
		}
		article.Published = pub.UTC()
		// Author
		authorNodes, _ := v.Search(v.Path() + "/author")
		if len(authorNodes) > 0 {
			article.Author = authorNodes[0].Content()
		}
		// Summary
		summaryNodes, _ := v.Search(v.Path() + "/description")
		article.Summary.Content = summaryNodes[0].Content()
		article.Summary.Type = "html"
		// Body
		// No body? Body = Summary? No summary and body contains entry description?
		// Read
		article.Read = false
		*articles = append(*articles, Article{})
		copy((*articles)[0+1:], (*articles)[0:])
		(*articles)[0] = article
	}
	// rfc1123 or rfc1123z
}