go-rss/rss.go

78 lines
1.8 KiB
Go
Raw Normal View History

2012-03-24 20:11:38 +00:00
package rss
import (
"crypto/tls"
2012-03-24 20:11:38 +00:00
"net/http"
"time"
)
const wordpressDateFormat = "Mon, 02 Jan 2006 15:04:05 -0700"
//Fetcher interface
type Fetcher interface {
Get(url string) (resp *http.Response, err error)
}
//Date type
2012-03-28 15:35:35 +00:00
type Date string
//Parse (Date function) and returns Time, error
func (d Date) Parse() (time.Time, error) {
t, err := d.ParseWithFormat(wordpressDateFormat)
2012-03-24 20:11:38 +00:00
if err != nil {
t, err = d.ParseWithFormat(time.RFC822) // RSS 2.0 spec
if err != nil {
t, err = d.ParseWithFormat(time.RFC3339) // Atom
}
2012-03-24 20:11:38 +00:00
}
return t, err
}
//ParseWithFormat (Date function), takes a string and returns Time, error
func (d Date) ParseWithFormat(format string) (time.Time, error) {
return time.Parse(format, string(d))
}
//Format (Date function), takes a string and returns string, error
func (d Date) Format(format string) (string, error) {
t, err := d.Parse()
2012-03-24 20:11:38 +00:00
if err != nil {
return "", err
}
return t.Format(format), nil
}
//MustFormat (Date function), take a string and returns string
func (d Date) MustFormat(format string) string {
s, err := d.Format(format)
2012-03-24 20:11:38 +00:00
if err != nil {
return err.Error()
}
return s
}
//Read a string url and returns a Channel struct, error
func Read(url string) (*http.Response, error) {
2014-02-27 00:57:03 +00:00
return ReadWithClient(url, http.DefaultClient)
}
//InsecureRead reads without certificate check
func InsecureRead(url string) (*http.Response, error) {
2018-07-29 21:41:17 +00:00
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
return ReadWithClient(url, client)
}
//ReadWithClient a string url and custom client that must match the Fetcher interface
//returns a Channel struct, error
func ReadWithClient(url string, client Fetcher) (*http.Response, error) {
2014-02-27 00:57:03 +00:00
response, err := client.Get(url)
2012-03-24 20:11:38 +00:00
if err != nil {
return nil, err
}
return response, nil
2012-03-24 20:11:38 +00:00
}