diff --git a/ISO-8859-1.go b/ISO-8859-1.go index 68a940f..dec0c20 100644 --- a/ISO-8859-1.go +++ b/ISO-8859-1.go @@ -11,81 +11,81 @@ import ( // ISO-8859-1 support type charsetISO88591er struct { - r io.ByteReader - buf *bytes.Buffer + r io.ByteReader + buf *bytes.Buffer } func newCharsetISO88591(r io.Reader) *charsetISO88591er { - buf := bytes.NewBuffer(make([]byte, 0, utf8.UTFMax)) - return &charsetISO88591er{r.(io.ByteReader), buf} + buf := bytes.NewBuffer(make([]byte, 0, utf8.UTFMax)) + return &charsetISO88591er{r.(io.ByteReader), buf} } func (cs *charsetISO88591er) ReadByte() (b byte, err error) { - // http://unicode.org/Public/MAPPINGS/ISO8859/8859-1.TXT - // Date: 1999 July 27; Last modified: 27-Feb-2001 05:08 - if cs.buf.Len() <= 0 { - r, err := cs.r.ReadByte() - if err != nil { - return 0, err - } - if r < utf8.RuneSelf { - return r, nil - } - cs.buf.WriteRune(rune(r)) - } - return cs.buf.ReadByte() + // http://unicode.org/Public/MAPPINGS/ISO8859/8859-1.TXT + // Date: 1999 July 27; Last modified: 27-Feb-2001 05:08 + if cs.buf.Len() <= 0 { + r, err := cs.r.ReadByte() + if err != nil { + return 0, err + } + if r < utf8.RuneSelf { + return r, nil + } + cs.buf.WriteRune(rune(r)) + } + return cs.buf.ReadByte() } func (cs *charsetISO88591er) Read(p []byte) (int, error) { - // Use ReadByte method. - return 0, errors.New("Use ReadByte()") + // Use ReadByte method. + return 0, errors.New("Use ReadByte()") } func isCharset(charset string, names []string) bool { - charset = strings.ToLower(charset) - for _, n := range names { - if charset == strings.ToLower(n) { - return true - } - } - return false + charset = strings.ToLower(charset) + for _, n := range names { + if charset == strings.ToLower(n) { + return true + } + } + return false } func isCharsetISO88591(charset string) bool { - // http://www.iana.org/assignments/character-sets - // (last updated 2010-11-04) - names := []string{ - // Name - "ISO_8859-1:1987", - // Alias (preferred MIME name) - "ISO-8859-1", - // Aliases - "iso-ir-100", - "ISO_8859-1", - "latin1", - "l1", - "IBM819", - "CP819", - "csISOLatin1", - } - return isCharset(charset, names) + // http://www.iana.org/assignments/character-sets + // (last updated 2010-11-04) + names := []string{ + // Name + "ISO_8859-1:1987", + // Alias (preferred MIME name) + "ISO-8859-1", + // Aliases + "iso-ir-100", + "ISO_8859-1", + "latin1", + "l1", + "IBM819", + "CP819", + "csISOLatin1", + } + return isCharset(charset, names) } func isCharsetUTF8(charset string) bool { - names := []string{ - "UTF-8", - // Default - "", - } - return isCharset(charset, names) + names := []string{ + "UTF-8", + // Default + "", + } + return isCharset(charset, names) } func charsetReader(charset string, input io.Reader) (io.Reader, error) { - switch { - case isCharsetUTF8(charset): - return input, nil - case isCharsetISO88591(charset): - return newCharsetISO88591(input), nil - } - return nil, errors.New("CharsetReader: unexpected charset: " + charset) -} \ No newline at end of file + switch { + case isCharsetUTF8(charset): + return input, nil + case isCharsetISO88591(charset): + return newCharsetISO88591(input), nil + } + return nil, errors.New("CharsetReader: unexpected charset: " + charset) +} diff --git a/atom.go b/atom.go index f531bac..8938e67 100644 --- a/atom.go +++ b/atom.go @@ -15,29 +15,29 @@ func parseAtom(data []byte, read *db) (*Feed, error) { if err != nil { return nil, err } - + out := new(Feed) out.Title = feed.Title out.Description = feed.Description out.Link = feed.Link.Href out.Image = feed.Image.Image() out.Refresh = time.Now().Add(10 * time.Minute) - + if feed.Items == nil { return nil, fmt.Errorf("Error: no feeds found in %q.", string(data)) } - + out.Items = make([]*Item, 0, len(feed.Items)) out.ItemMap = make(map[string]struct{}) - + // Process items. for _, item := range feed.Items { - + // Skip items already known. - if read.req <- item.ID; <- read.res { + if read.req <- item.ID; <-read.res { continue } - + next := new(Item) next.Title = item.Title next.Content = item.Content @@ -50,22 +50,22 @@ func parseAtom(data []byte, read *db) (*Feed, error) { } next.ID = item.ID next.Read = false - + if next.ID == "" { fmt.Printf("Warning: Item %q has no ID and will be ignored.\n", next.Title) continue } - + if _, ok := out.ItemMap[next.ID]; ok { fmt.Printf("Warning: Item %q has duplicate ID.\n", next.Title) continue } - + out.Items = append(out.Items, next) out.ItemMap[next.ID] = struct{}{} out.Unread++ } - + return out, nil } @@ -97,7 +97,7 @@ type atomImage struct { } type atomLink struct { - Href string `xml:"href,attr"` + Href string `xml:"href,attr"` } func (a *atomImage) Image() *Image { diff --git a/database.go b/database.go index f56d7ff..b2bd8c4 100644 --- a/database.go +++ b/database.go @@ -8,17 +8,17 @@ func init() { } type db struct { - req chan string - res chan bool + req chan string + res chan bool known map[string]struct{} } func (d *db) Run() { d.known = make(map[string]struct{}) var s string - + for { - s = <- d.req + s = <-d.req if _, ok := d.known[s]; ok { d.res <- true } else { diff --git a/doc.go b/doc.go index d43282b..0bfc08f 100644 --- a/doc.go +++ b/doc.go @@ -19,9 +19,9 @@ func main() { if err != nil { // handle error. } - + // ... Some time later ... - + err = feed.Update() if err != nil { // handle error. diff --git a/rss 1.0.go b/rss 1.0.go index 2748ae7..5d6a65f 100644 --- a/rss 1.0.go +++ b/rss 1.0.go @@ -20,9 +20,9 @@ func parseRSS1(data []byte, read *db) (*Feed, error) { if feed.Channel == nil { return nil, fmt.Errorf("Error: no channel found in %q.", string(data)) } - + channel := feed.Channel - + out := new(Feed) out.Title = channel.Title out.Description = channel.Description @@ -33,7 +33,7 @@ func parseRSS1(data []byte, read *db) (*Feed, error) { next := time.Now().Add(time.Duration(channel.MinsToLive) * time.Minute) for _, hour := range channel.SkipHours { if hour == next.Hour() { - next.Add(time.Duration(60 - next.Minute()) * time.Minute) + next.Add(time.Duration(60-next.Minute()) * time.Minute) } } trying := true @@ -41,35 +41,35 @@ func parseRSS1(data []byte, read *db) (*Feed, error) { trying = false for _, day := range channel.SkipDays { if strings.Title(day) == next.Weekday().String() { - next.Add(time.Duration(24 - next.Hour()) * time.Hour) + next.Add(time.Duration(24-next.Hour()) * time.Hour) trying = true break } } } - + out.Refresh = next } - + if out.Refresh.IsZero() { out.Refresh = time.Now().Add(10 * time.Minute) } - + if feed.Items == nil { return nil, fmt.Errorf("Error: no feeds found in %q.", string(data)) } - + out.Items = make([]*Item, 0, len(feed.Items)) out.ItemMap = make(map[string]struct{}) - + // Process items. for _, item := range feed.Items { - + // Skip items already known. - if read.req <- item.ID; <- read.res { + if read.req <- item.ID; <-read.res { continue } - + next := new(Item) next.Title = item.Title next.Content = item.Content @@ -82,7 +82,7 @@ func parseRSS1(data []byte, read *db) (*Feed, error) { } next.ID = item.ID next.Read = false - + if next.ID == "" { if next.Link == "" { fmt.Printf("Warning: Item %q has no ID or link and will be ignored.\n", next.Title) @@ -90,35 +90,35 @@ func parseRSS1(data []byte, read *db) (*Feed, error) { } next.ID = next.Link } - + if _, ok := out.ItemMap[next.ID]; ok { fmt.Printf("Warning: Item %q has duplicate ID.\n", next.Title) continue } - + out.Items = append(out.Items, next) out.ItemMap[next.ID] = struct{}{} out.Unread++ } - + return out, nil } type rss1_0Feed struct { - XMLName xml.Name `xml:"RDF"` - Channel *rss1_0Channel `xml:"channel"` - Items []rss1_0Item `xml:"item"` + XMLName xml.Name `xml:"RDF"` + Channel *rss1_0Channel `xml:"channel"` + Items []rss1_0Item `xml:"item"` } type rss1_0Channel struct { - XMLName xml.Name `xml:"channel"` - Title string `xml:"title"` - Description string `xml:"description"` - Link string `xml:"link"` - Image rss1_0Image `xml:"image"` - MinsToLive int `xml:"ttl"` - SkipHours []int `xml:"skipHours>hour"` - SkipDays []string `xml:"skipDays>day"` + XMLName xml.Name `xml:"channel"` + Title string `xml:"title"` + Description string `xml:"description"` + Link string `xml:"link"` + Image rss1_0Image `xml:"image"` + MinsToLive int `xml:"ttl"` + SkipHours []int `xml:"skipHours>hour"` + SkipDays []string `xml:"skipDays>day"` } type rss1_0Item struct { diff --git a/rss 2.0.go b/rss 2.0.go index c53d8de..59b7a46 100644 --- a/rss 2.0.go +++ b/rss 2.0.go @@ -20,9 +20,9 @@ func parseRSS2(data []byte, read *db) (*Feed, error) { if feed.Channel == nil { return nil, fmt.Errorf("Error: no channel found in %q.", string(data)) } - + channel := feed.Channel - + out := new(Feed) out.Title = channel.Title out.Description = channel.Description @@ -33,7 +33,7 @@ func parseRSS2(data []byte, read *db) (*Feed, error) { next := time.Now().Add(time.Duration(channel.MinsToLive) * time.Minute) for _, hour := range channel.SkipHours { if hour == next.Hour() { - next.Add(time.Duration(60 - next.Minute()) * time.Minute) + next.Add(time.Duration(60-next.Minute()) * time.Minute) } } trying := true @@ -41,35 +41,35 @@ func parseRSS2(data []byte, read *db) (*Feed, error) { trying = false for _, day := range channel.SkipDays { if strings.Title(day) == next.Weekday().String() { - next.Add(time.Duration(24 - next.Hour()) * time.Hour) + next.Add(time.Duration(24-next.Hour()) * time.Hour) trying = true break } } } - + out.Refresh = next } - + if out.Refresh.IsZero() { out.Refresh = time.Now().Add(10 * time.Minute) } - + if channel.Items == nil { return nil, fmt.Errorf("Error: no feeds found in %q.", string(data)) } - + out.Items = make([]*Item, 0, len(channel.Items)) out.ItemMap = make(map[string]struct{}) - + // Process items. for _, item := range channel.Items { - + // Skip items already known. - if read.req <- item.ID; <- read.res { + if read.req <- item.ID; <-read.res { continue } - + next := new(Item) next.Title = item.Title next.Content = item.Content @@ -82,7 +82,7 @@ func parseRSS2(data []byte, read *db) (*Feed, error) { } next.ID = item.ID next.Read = false - + if next.ID == "" { if next.Link == "" { fmt.Printf("Warning: Item %q has no ID or link and will be ignored.\n", next.Title) @@ -90,23 +90,23 @@ func parseRSS2(data []byte, read *db) (*Feed, error) { } next.ID = next.Link } - + if _, ok := out.ItemMap[next.ID]; ok { fmt.Printf("Warning: Item %q has duplicate ID.\n", next.Title) continue } - + out.Items = append(out.Items, next) out.ItemMap[next.ID] = struct{}{} out.Unread++ } - + return out, nil } type rss2_0Feed struct { - XMLName xml.Name `xml:"rss"` - Channel *rss2_0Channel `xml:"channel"` + XMLName xml.Name `xml:"rss"` + Channel *rss2_0Channel `xml:"channel"` } type rss2_0Channel struct { diff --git a/rss.go b/rss.go index 2d6f932..2d931df 100644 --- a/rss.go +++ b/rss.go @@ -1,147 +1,147 @@ package rss import ( - "bytes" - "errors" - "fmt" - "io/ioutil" - "net/http" - "strings" - "time" + "bytes" + "errors" + "fmt" + "io/ioutil" + "net/http" + "strings" + "time" ) // Parse RSS or Atom data. func Parse(data []byte) (*Feed, error) { - if strings.Contains(string(data), "