raven/feed.go

158 lines
2.9 KiB
Go
Raw Normal View History

2023-01-10 05:23:25 +00:00
package main
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"sort"
"strings"
"time"
)
func appendToFile(filename, data string) error {
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
f.WriteString(data)
f.Close()
return nil
}
func prependToFile(filename, data string) error {
ogData, err := os.ReadFile(filename)
if err != nil {
return err
}
fileData := string(ogData[:])
f, err := os.Create(filename)
if err != nil {
return err
}
f.WriteString(data + fileData)
f.Close()
return nil
}
func Post(twt, feedFile string, ascend bool) error {
fileInfo, err := os.Stat(feedFile)
if err != nil {
f, err := os.Create(feedFile)
if err != nil {
return err
}
f.Close()
2023-01-10 05:23:25 +00:00
} else if fileInfo.IsDir() {
return errors.New("Feed file is a directory")
}
if ascend {
return appendToFile(feedFile, time.Now().Format(time.RFC3339)+"\t"+twt+"\n")
}
return prependToFile(feedFile, time.Now().Format(time.RFC3339)+"\t"+twt+"\n")
}
2023-01-10 15:47:05 +00:00
func buildFeedFromUrl(feed *Feed, nick, url string) error {
2023-01-10 05:23:25 +00:00
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
2023-01-10 15:47:05 +00:00
return buildFeed(feed, nick, string(body[:]))
2023-01-10 05:23:25 +00:00
}
2023-01-10 15:47:05 +00:00
func buildFeedFromPath(feed *Feed, nick, filename string) error {
2023-01-10 05:23:25 +00:00
f, err := os.ReadFile(filename)
if err != nil {
return err
}
2023-01-10 15:47:05 +00:00
return buildFeed(feed, nick, string(f[:]))
2023-01-10 05:23:25 +00:00
}
2023-01-10 15:47:05 +00:00
func buildFeed(feed *Feed, nick, data string) error {
2023-01-10 05:23:25 +00:00
lines := strings.Split(data, "\n")
2023-01-10 15:47:05 +00:00
for _, l := range lines {
2023-01-10 05:23:25 +00:00
parts := strings.Split(l, "\t")
t, err := time.Parse(time.RFC3339, parts[0])
if err != nil {
continue
2023-01-10 05:23:25 +00:00
}
*feed = append(*feed, FeedEntry{
Nick: nick,
Timestamp: t,
Post: parts[1],
})
}
return nil
}
2023-01-10 15:47:05 +00:00
func printFeed(feed *Feed, max int64) {
2023-01-10 05:23:25 +00:00
sort.Sort(*feed)
2023-01-10 15:47:05 +00:00
for i, entry := range *feed {
2023-01-10 15:51:25 +00:00
if int64(i) >= max {
return
}
2023-01-10 05:23:25 +00:00
fmt.Printf("[%s] <%s> %s\n", entry.Timestamp.Format(time.Stamp), entry.Nick, entry.Post)
}
}
func GetFeed(friend, friendsFile string, max int64) error {
feed := &Feed{}
f, err := os.ReadFile(friendsFile)
if err != nil {
return err
}
fileData := string(f[:])
lines := strings.Split(fileData, "\n")
for _, l := range lines {
if len(l) == 0 {
continue
}
if !strings.Contains(l, " ") {
return errors.New("Malformed friends file not in the format 'FRIEND_NAME https://friend.site/path/to/feed.txt'")
}
kvp := strings.Split(l, " ")
k := kvp[0]
v := kvp[1]
if k == friend || len(friend) == 0 {
2023-01-10 15:47:05 +00:00
err = buildFeedFromUrl(feed, k, v)
2023-01-10 05:23:25 +00:00
if err != nil {
return err
}
if len(friend) > 0 {
break
2023-01-10 05:23:25 +00:00
}
}
}
2023-01-10 15:47:05 +00:00
printFeed(feed, max)
2023-01-10 05:23:25 +00:00
return nil
}
func GetOwnFeed(nick, feedFile string, max int64) error {
feed := &Feed{}
2023-01-10 15:47:05 +00:00
err := buildFeedFromPath(feed, nick, feedFile)
2023-01-10 05:23:25 +00:00
if err != nil {
return err
}
2023-01-10 15:47:05 +00:00
printFeed(feed, max)
2023-01-10 05:23:25 +00:00
return nil
}