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 getEposNodes(doc *xml.XmlDocument) (retnodes []xml.Node, err error) {
	// grab the 'Body' element
	path := xpath.Compile("*[local-name()='Body']")
	nodes, e := doc.Root().Search(path)
	if e != nil {
		err = e
		return
	}

	// check that the data is present
	if len(nodes) < 1 || nodes[0].CountChildren() < 1 {
		err = errors.New("bad data")
		return
	}

	// get epos data
	return nodes[0].FirstChild().Search("./*")
}
Exemplo n.º 3
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
}
Exemplo n.º 4
0
func (r *RssCloudRequest) Unpack(doc *xml.XmlDocument) error {
	root := doc.Root()

	requestMethods, err := root.Search("methodName/text()")
	if err != nil {
		return err
	}
	if len(requestMethods) != 1 {
		return fmt.Errorf("Could not find cloud request's request method")
	}
	r.RequestMethodName = requestMethods[0].Content()

	// Would be nice to get the child nodes but xpath will give elements only, not text nodes.
	params, err := root.Search("params/param/value")
	if err != nil {
		return err
	}
	if len(params) != 5 {
		return fmt.Errorf("Could not unpack cloud request with %d params", len(params))
	}

	node := params[0].FirstChild()
	if node.NodeType() != xml.XML_TEXT_NODE {
		return fmt.Errorf("Could not unpack cloud request with first param not text")
	}
	r.MethodName = node.Content()

	node = params[1].FirstChild()
	if node.NodeType() != xml.XML_ELEMENT_NODE {
		return fmt.Errorf("Could not unpack cloud request with second param not an element")
	}
	if !(node.Name() == "i4" || node.Name() == "int") {
		return fmt.Errorf("Could not unpack cloud request with second param a %s element, not int or i4", node.Name())
	}
	node = node.FirstChild()
	if node.NodeType() != xml.XML_TEXT_NODE {
		return fmt.Errorf("Could not unpack cloud request with second param not containing text")
	}
	port, err := strconv.Atoi(node.Content())
	if err != nil {
		return err
	}
	r.Port = uint16(port)

	node = params[2].FirstChild()
	if node.NodeType() != xml.XML_TEXT_NODE {
		return fmt.Errorf("Could not unpack cloud request with third param not text")
	}
	r.Path = node.Content()

	node = params[3].FirstChild()
	if node.NodeType() != xml.XML_TEXT_NODE {
		return fmt.Errorf("Could not unpack cloud request with fourth param not text")
	}
	r.IsXmlRpc = strings.TrimSpace(node.Content()) == "xml-rpc"

	node = params[4].FirstChild()
	if node.NodeType() != xml.XML_ELEMENT_NODE {
		return fmt.Errorf("Could not unpack cloud request with fifth param not an element")
	}
	if node.Name() != "array" {
		return fmt.Errorf("Could not unpack cloud request with fifth param a %s element, not array", node.Name())
	}
	params, err = node.Search("data/value/text()")
	if err != nil {
		return err
	}
	if len(params) < 1 {
		return fmt.Errorf("Could not unpack cloud request with fifth param containing no data values")
	}
	r.FeedURL = params[0].Content()

	logr.Debugln("Unpacked cloud request!")
	return nil
}