diff --git a/rss.go b/rss.go index 7e0d3e6..868e793 100644 --- a/rss.go +++ b/rss.go @@ -1,90 +1,133 @@ package rss import ( - "bytes" - "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), "= len(f.Items) { + f.Items = append(f.Items, item) + f.Unread++ + } else if f.Items[i+offset].ID != item.ID { + return errors.New("Error: offsets don't match.") + } + } + + return nil } func (f *Feed) String() string { - buf := new(bytes.Buffer) - buf.WriteString(fmt.Sprintf("Feed %q\n\t%q\n\t%q\n\t%s\n\tRefresh at %s\n\tUnread: %d\n\tItems:\n", - f.Title, f.Description, f.Link, f.Image, f.Refresh.Format("Mon 2 Jan 2006 15:04:05 MST"), f.Unread)) - for _, item := range f.Items { - buf.WriteString(fmt.Sprintf("\t%s\n", item.Format("\t\t"))) - } - return buf.String() + buf := new(bytes.Buffer) + buf.WriteString(fmt.Sprintf("Feed %q\n\t%q\n\t%q\n\t%s\n\tRefresh at %s\n\tUnread: %d\n\tItems:\n", + f.Title, f.Description, f.Link, f.Image, f.Refresh.Format("Mon 2 Jan 2006 15:04:05 MST"), f.Unread)) + for _, item := range f.Items { + buf.WriteString(fmt.Sprintf("\t%s\n", item.Format("\t\t"))) + } + return buf.String() } // Item represents a single story. type Item struct { - Title string - Content string - Link string - Date time.Time - ID string - Read bool + Title string + Content string + Link string + Date time.Time + ID string + Read bool } func (i *Item) String() string { - return i.Format("") + return i.Format("") } func (i *Item) Format(s string) string { - return fmt.Sprintf("Item %q\n\t%s%q\n\t%s%s\n\t%s%q\n\t%sRead: %v\n\t%s%q", i.Title, s, i.Link, s, - i.Date.Format("Mon 2 Jan 2006 15:04:05 MST"), s, i.ID, s, i.Read, s, i.Content) + return fmt.Sprintf("Item %q\n\t%s%q\n\t%s%s\n\t%s%q\n\t%sRead: %v\n\t%s%q", i.Title, s, i.Link, s, + i.Date.Format("Mon 2 Jan 2006 15:04:05 MST"), s, i.ID, s, i.Read, s, i.Content) } type Image struct { - Title string - Url string - Height uint32 - Width uint32 + Title string + Url string + Height uint32 + Width uint32 } func (i *Image) String() string { - return fmt.Sprintf("Image %q", i.Title) + return fmt.Sprintf("Image %q", i.Title) }