Last active
August 29, 2015 14:06
-
-
Save bloopletech/b9b0ebd41a51b4fe240c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
"fmt" | |
//"os" | |
//"regexp" | |
"strings" | |
"encoding/json" | |
"github.com/SlyMarbo/rss" | |
"github.com/moovweb/gokogiri" | |
) | |
const MAX_ITEM_COUNT = 20 | |
type ItemSubset struct { | |
Title string | |
Content string | |
URL string | |
} | |
func formatItemContent(text string) string { | |
doc, err := gokogiri.ParseHtml([]byte(text)) | |
if err != nil { | |
fmt.Printf("%s", err) | |
// handle error | |
} | |
defer doc.Free() | |
return strings.TrimSpace(doc.String()) | |
} | |
// | |
// | |
// re := regexp.MustCompile("\\s+") | |
// return strings.TrimSpace(re.ReplaceAllString(text, " ")) | |
//} | |
func main() { | |
//download the rss feed | |
//pull out the content | |
//dump it out | |
//url := os.Args[1] | |
url := "http://www.abc.net.au/news/feed/51120/rss.xml" | |
feed, err := rss.Fetch(url) | |
if err != nil { | |
fmt.Printf("%s", err) | |
// handle error | |
} | |
feedSubset := make(map[string]interface{}) | |
feedSubset["title"] = feed.Title | |
feedSubset["url"] = feed.Link | |
itemsSubset := make([]map[string]interface{}, 0) | |
for _, item := range feed.Items[:MAX_ITEM_COUNT] { | |
itemSubset := make(map[string]interface{}) | |
itemSubset["title"] = item.Title | |
itemSubset["content"] = formatItemContent(item.Content) | |
itemSubset["url"] = item.Link | |
itemsSubset = append(itemsSubset, itemSubset) | |
} | |
feedSubset["items"] = itemsSubset | |
output, err := json.Marshal(feedSubset) | |
if err != nil { | |
fmt.Printf("%s", err) | |
// handle error | |
} | |
fmt.Printf("%s", output) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment