Created
April 8, 2013 01:59
-
-
Save crosbymichael/5333646 to your computer and use it in GitHub Desktop.
Atom and rss parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/xml" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"time" | |
) | |
type Link struct { | |
Href string `xml:"href,attr"` | |
Rel string `xml:"rel,attr"` | |
} | |
type AtomEntry struct { | |
Title string `xml:"title"` | |
Author string `xml:"author>name"` | |
Summary string `xml:"summary"` | |
Link Link `xml:"link"` | |
} | |
type AtomFeed struct { | |
XMLName xml.Name `xml:"feed"` | |
Title string `xml:"title"` | |
Id string `xml:"id"` | |
Updated time.Time `xml:"updated"` | |
Entrys []AtomEntry `xml:"entry"` | |
Links []Link `xml:"link"` | |
} | |
type RssItem struct { | |
Title string `xml:"title"` | |
Link string `xml:"link"` | |
Description string `xml:"description"` | |
PubDate time.Time `xml:"pubDate"` | |
Guid string `xml:"guid"` | |
} | |
type RssFeed struct { | |
XMLName xml.Name `xml:"rss"` | |
Title string `xml:"title"` | |
Link string `xml:"link"` | |
Description string `xml:"description"` | |
} | |
func getData(url string) ([]byte, error) { | |
request, err := http.NewRequest("GET", url, nil) | |
if err != nil { | |
return nil, err | |
} | |
client := http.Client{} | |
response, err := client.Do(request) | |
if err != nil { | |
return nil, err | |
} | |
defer response.Body.Close() | |
defer response.Close() | |
return ioutil.ReadAll(response.Body) | |
} | |
func main() { | |
url := "http://crosbymichael.com/feeds/all.atom.xml" | |
var feed AtomFeed | |
data, err := getData(url) | |
if err != nil { | |
panic(err) | |
} | |
err = xml.Unmarshal(data, &feed) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(feed.XMLName.Space) | |
fmt.Println(feed.Links) | |
fmt.Println(feed.Entrys[0].Link) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment