Last active
August 29, 2015 14:13
-
-
Save sdemingo/bc9590f413f73b7a5edd to your computer and use it in GitHub Desktop.
How convert a RSS source to org-mode calendar file
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 ( | |
"time" | |
"bytes" | |
"encoding/xml" | |
"os" | |
"fmt" | |
"net/http" | |
"io/ioutil" | |
) | |
type RssFeed struct { | |
XMLName xml.Name `xml:"rss"` | |
Channel *RssChannel `xml:"channel"` | |
} | |
type RssChannel struct { | |
XMLName xml.Name `xml:"channel"` | |
Title string `xml:"title"` | |
Description string `xml:"description"` | |
Link string `xml:"link"` | |
Language string `xml:"language"` | |
PubDate string `xml:"pubDate"` | |
LastBuildDate string `xml:"lastBuildDate"` | |
Items []RssItem `xml:"item"` | |
} | |
type RssItem struct{ | |
Title string `xml:"title"` | |
Description string `xml:"description"` | |
Author string `xml:"author"` | |
PubDate string `xml:"pubDate"` | |
} | |
func main(){ | |
if len(os.Args)<2{ | |
fmt.Println("RSS url not found") | |
os.Exit(0) | |
} | |
url:=os.Args[1] | |
xml := fetchRss(url) | |
rss := parseXml(xml) | |
printRSS(rss.Channel) | |
} | |
func fetchRss(url string) []byte { | |
resp, err := http.Get(url) | |
content,err:=ioutil.ReadAll(resp.Body) | |
if err!=nil{ | |
fmt.Println("Fetching failed") | |
os.Exit(0) | |
} | |
f, err:=ioutil.TempFile("","rssfile") | |
_,err=f.Write(content) | |
rssContent, err := ioutil.ReadFile(f.Name()) | |
if err != nil { | |
fmt.Println("Unable to read src xml file.", err) | |
os.Exit(0) | |
} | |
return rssContent | |
} | |
func parseXml(x []byte) *RssFeed { | |
feed := RssFeed{} | |
d := xml.NewDecoder(bytes.NewReader(x)) | |
d.DefaultSpace = "RssDefault" | |
err := d.Decode(&feed) | |
if err != nil { | |
fmt.Println("Failed decoding xml") | |
os.Exit(0) | |
} | |
return &feed | |
} | |
func printRSS(rss *RssChannel) { | |
fmt.Printf("\n\n* %s\n",rss.Title) | |
fmt.Printf("%s\n",rss.Description) | |
if (rss.Link!=""){ | |
fmt.Printf("From %s\n",rss.Link) | |
} | |
fmt.Printf("\n") | |
for i:=range rss.Items{ | |
t,_:=time.Parse("Mon, 2 Jan 2006 15:04:05 -0700",rss.Items[i].PubDate) | |
fmt.Println("** ",rss.Items[i].Title) | |
fmt.Println("\t",rss.Items[i].Description) | |
fmt.Println("\t<"+t.Format("2006-01-02 Mon 15:04")+">") | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment