Created
March 8, 2019 14:55
-
-
Save aubm/eec8874b6869020895cb96b5da2987e7 to your computer and use it in GitHub Desktop.
Ugly code
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" | |
"flag" | |
"fmt" | |
"net/http" | |
"os" | |
"text/template" | |
) | |
var ( | |
rssUrl string | |
o string | |
) | |
func init() { | |
flag.StringVar(&rssUrl, "rss-url", "", "") | |
flag.StringVar(&o, "output-file", "", "") | |
flag.Parse() | |
} | |
func main() { | |
if err := run(); err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
} | |
func run() error { | |
// will get rss feed | |
v, err := get_rssFeed() | |
if err == nil { | |
f, err := os.Create(o) | |
if err != nil { | |
return err | |
} | |
defer f.Close() | |
tmpl, _ := template.New("").Parse(`{{range .Channel.Item}} | |
## {{.Title}} | |
{{.Description}} | |
{{.Link}} | |
{{end}}`) | |
if err := tmpl.Execute(f, v); err != nil { | |
return err | |
} | |
return nil | |
} else { | |
return err | |
} | |
} | |
// get_rssFeed downloads the rss feed and returns | |
// the parsed rss feed | |
// error occurs either when: | |
// - the http request fails | |
// - the status code not 200 | |
// - it fails to parse the xml content | |
func get_rssFeed() (*RssFeed, error) { | |
resp, err := http.Get(rssUrl) | |
if err != nil { | |
return nil, err | |
} | |
defer resp.Body.Close() | |
if resp.StatusCode < 200 || resp.StatusCode > 299 { | |
return nil, fmt.Errorf("unexpected status code %v for url %v", resp.StatusCode, rssUrl) | |
} | |
feed := RssFeed{} | |
if err := xml.NewDecoder(resp.Body).Decode(&feed); err != nil { | |
return nil, err | |
} | |
return &feed, nil | |
} | |
type RssFeed struct { | |
XMLName xml.Name `xml:"rss"` | |
Text string `xml:",chardata"` | |
Version string `xml:"version,attr"` | |
Atom string `xml:"atom,attr"` | |
Channel struct { | |
Text string `xml:",chardata"` | |
Title string `xml:"title"` | |
Description string `xml:"description"` | |
Copyright string `xml:"copyright"` | |
Link struct { | |
Text string `xml:",chardata"` | |
Href string `xml:"href,attr"` | |
Rel string `xml:"rel,attr"` | |
Type string `xml:"type,attr"` | |
} `xml:"link"` | |
PubDate string `xml:"pubDate"` | |
Image struct { | |
Text string `xml:",chardata"` | |
URL string `xml:"url"` | |
Title string `xml:"title"` | |
Link string `xml:"link"` | |
} `xml:"image"` | |
Item []struct { | |
Text string `xml:",chardata"` | |
Link string `xml:"link"` | |
Title string `xml:"title"` | |
Description string `xml:"description"` | |
PubDate string `xml:"pubDate"` | |
Guid struct { | |
Text string `xml:",chardata"` | |
IsPermaLink string `xml:"isPermaLink,attr"` | |
} `xml:"guid"` | |
Enclosure struct { | |
Text string `xml:",chardata"` | |
URL string `xml:"url,attr"` | |
Type string `xml:"type,attr"` | |
Length string `xml:"length,attr"` | |
} `xml:"enclosure"` | |
} `xml:"item"` | |
} `xml:"channel"` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment