<file_sep># nu
Go Library for scraping NU.nl RSS feed to JSON output

> XML is crap. Really. There are no excuses. XML is nasty to parse for humans, and it's a disaster to parse even for computers. There's just no reason for that horrible crap to exist. - <NAME>


##Example

```go
package main

import (
	"fmt"
	"github.com/Rivalo/nu"
)

func main() {
	json, err := nu.JSON("http://www.nu.nl/rss/Algemeen")
  	if err != nil {
  		fmt.Println(err)
  		return
  	}
  
	fmt.Println(string(json))
}
```
<file_sep>package nu

import (
	"encoding/json"
	"fmt"
)

//Struct returns the NU RSS feed as struct XML
func Struct(url string) (feed XML, err error) {
	feed, err = parseToFeed(url)
	if err != nil {
		return XML{}, err
	}
	return
}

//JSON returns the NU RSS feed as JSON in []byte
func JSON(url string) (JSON []byte, err error) {
	feed, err := parseToFeed(url)
	if err != nil {
		return nil, err
	}

	fmt.Println(feed)

	JSON, err = json.Marshal(feed)
	if err != nil {
		return nil, err
	}

	return
}
<file_sep>package nu

//XML2 contains link of NU.nl Items (Hack for encoding/xml)
type xML2 struct {
	Channel channel2 `xml:"channel"`
}

//Channel2 contains channel of NUXML2 feed
type channel2 struct {
	Item []article2 `xml:"item"`
}

//Article2 constains link of single Nu.nl article
type article2 struct {
	Link []string `xml:"link" json:"link"`
}
<file_sep>package nu

//XML is a struct containing Nu.nl RSS file
type XML struct {
	Channel Channel `xml:"channel"`
}

//Channel contains channel of NUXML feed
type Channel struct {
	Title         string    `xml:"title" json:"title"`
	Description   string    `xml:"description" json:"description"`
	Language      string    `xml:"language" json:"language"`
	Copyright     string    `xml:"copyright" json:"copyright"`
	LastBuildDate string    `xml:"lastBuildDate" json:"lastbuilddate"`
	Item          []Article `xml:"item" json:"item"`
}

//Article constains information of single Nu.nl article
type Article struct {
	Title       string     `xml:"title" json:"title"`
	Link        string     `json:"link"`
	Description string     `xml:"description" json:"description"`
	PubData     string     `xml:"pubDate" json:"pubdate"`
	GUID        int        `xml:"guid" json:"guid"`
	Categories  []string   `xml:"category" json:"categories"`
	Creator     string     `xml:"creator" json:"creator"`
	Rights      string     `xml:"rights" json:"rights"`
	Image       Enclosure  `xml:"enclosure" json:"image"`
	Related     []AtomLink `xml:"http://www.w3.org/2005/Atom link" json:"related"`
}

//Enclosure contains image of article
type Enclosure struct {
	Link string `xml:"url,attr" json:"url"`
}

//AtomLink contains reference articles
type AtomLink struct {
	Link  string `xml:"href,attr"`
	Title string `xml:"title,attr"`
}
<file_sep>package nu

import (
	"encoding/xml"
	"io"
	"net/http"
)

//ParseToFeed returns JSON in []Byte from NU.nl RSS XML feed as string
func parseToFeed(url string) (Feed XML, err error) {

	file, err := http.Get(url)
	if err != nil {
		return
	}
	defer file.Body.Close()

	PreFeed, err := nuToPreFeed(file.Body)
	if err != nil {
		return
	}

	file, err = http.Get(url)
	if err != nil {
		return
	}
	defer file.Body.Close()

	Feed, err = preFeedToJSON(PreFeed, file.Body)
	if err != nil {
		return
	}

	return
}

func nuToPreFeed(f io.Reader) (NuFeed XML, err error) {

	NuFeed = XML{}
	d := xml.NewDecoder(f)
	err = d.Decode(&NuFeed)
	if err != nil {
		return XML{}, err
	}

	return NuFeed, err
}

func preFeedToJSON(PreFeed XML, f io.Reader) (Feed XML, err error) {

	NuFeed2 := xML2{}
	d := xml.NewDecoder(f)
	err = d.Decode(&NuFeed2)
	if err != nil {
		return XML{}, err
	}

	for i := 0; i < len(PreFeed.Channel.Item); i++ {
		PreFeed.Channel.Item[i].Link = NuFeed2.Channel.Item[i].Link[0]
	}

	return PreFeed, err
}
