feed.go: fix max posts logic

This commit is contained in:
Iris Lightshard 2023-01-10 08:47:05 -07:00
parent c274e63e82
commit fb43dd02b6
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398

30
feed.go
View file

@ -52,7 +52,7 @@ func Post(twt, feedFile string, ascend bool) error {
return prependToFile(feedFile, time.Now().Format(time.RFC3339)+"\t"+twt+"\n") return prependToFile(feedFile, time.Now().Format(time.RFC3339)+"\t"+twt+"\n")
} }
func buildFeedFromUrl(feed *Feed, nick, url string, max int64) error { func buildFeedFromUrl(feed *Feed, nick, url string) error {
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
return err return err
@ -64,25 +64,22 @@ func buildFeedFromUrl(feed *Feed, nick, url string, max int64) error {
return err return err
} }
return buildFeed(feed, nick, string(body[:]), max) return buildFeed(feed, nick, string(body[:]))
} }
func buildFeedFromPath(feed *Feed, nick, filename string, max int64) error { func buildFeedFromPath(feed *Feed, nick, filename string) error {
f, err := os.ReadFile(filename) f, err := os.ReadFile(filename)
if err != nil { if err != nil {
return err return err
} }
return buildFeed(feed, nick, string(f[:]), max) return buildFeed(feed, nick, string(f[:]))
} }
func buildFeed(feed *Feed, nick, data string, max int64) error { func buildFeed(feed *Feed, nick, data string) error {
lines := strings.Split(data, "\n") lines := strings.Split(data, "\n")
for i, l := range lines { for _, l := range lines {
if int64(i) > max {
return nil
}
parts := strings.Split(l, "\t") parts := strings.Split(l, "\t")
t, err := time.Parse(time.RFC3339, parts[0]) t, err := time.Parse(time.RFC3339, parts[0])
if err != nil { if err != nil {
@ -97,9 +94,12 @@ func buildFeed(feed *Feed, nick, data string, max int64) error {
return nil return nil
} }
func printFeed(feed *Feed) { func printFeed(feed *Feed, max int64) {
sort.Sort(*feed) sort.Sort(*feed)
for _, entry := range *feed { for i, entry := range *feed {
if int64(i) >= max {
return
}
fmt.Printf("[%s] <%s> %s\n", entry.Timestamp.Format(time.Stamp), entry.Nick, entry.Post) fmt.Printf("[%s] <%s> %s\n", entry.Timestamp.Format(time.Stamp), entry.Nick, entry.Post)
} }
} }
@ -129,7 +129,7 @@ func GetFeed(friend, friendsFile string, max int64) error {
v := kvp[1] v := kvp[1]
if k == friend || len(friend) == 0 { if k == friend || len(friend) == 0 {
err = buildFeedFromUrl(feed, k, v, max) err = buildFeedFromUrl(feed, k, v)
if err != nil { if err != nil {
return err return err
} }
@ -138,16 +138,16 @@ func GetFeed(friend, friendsFile string, max int64) error {
} }
} }
} }
printFeed(feed) printFeed(feed, max)
return nil return nil
} }
func GetOwnFeed(nick, feedFile string, max int64) error { func GetOwnFeed(nick, feedFile string, max int64) error {
feed := &Feed{} feed := &Feed{}
err := buildFeedFromPath(feed, nick, feedFile, max) err := buildFeedFromPath(feed, nick, feedFile)
if err != nil { if err != nil {
return err return err
} }
printFeed(feed) printFeed(feed, max)
return nil return nil
} }