153 lines
2.8 KiB
Go
153 lines
2.8 KiB
Go
|
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 {
|
||
|
os.Create(feedFile)
|
||
|
} 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")
|
||
|
}
|
||
|
|
||
|
func buildFeedFromUrl(feed *Feed, nick, url string, max int64) error {
|
||
|
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
|
||
|
}
|
||
|
|
||
|
return buildFeed(feed, nick, string(body[:]), max)
|
||
|
}
|
||
|
|
||
|
func buildFeedFromPath(feed *Feed, nick, filename string, max int64) error {
|
||
|
f, err := os.ReadFile(filename)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return buildFeed(feed, nick, string(f[:]), max)
|
||
|
}
|
||
|
|
||
|
func buildFeed(feed *Feed, nick, data string, max int64) error {
|
||
|
lines := strings.Split(data, "\n")
|
||
|
|
||
|
for i, l := range lines {
|
||
|
if int64(i) > max {
|
||
|
return nil
|
||
|
}
|
||
|
parts := strings.Split(l, "\t")
|
||
|
t, err := time.Parse(time.RFC3339, parts[0])
|
||
|
if err != nil {
|
||
|
continue
|
||
|
}
|
||
|
*feed = append(*feed, FeedEntry{
|
||
|
Nick: nick,
|
||
|
Timestamp: t,
|
||
|
Post: parts[1],
|
||
|
})
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func printFeed(feed *Feed) {
|
||
|
sort.Sort(*feed)
|
||
|
for _, entry := range *feed {
|
||
|
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 {
|
||
|
err = buildFeedFromUrl(feed, k, v, max)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if len(friend) > 0 {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
printFeed(feed)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func GetOwnFeed(nick, feedFile string, max int64) error {
|
||
|
feed := &Feed{}
|
||
|
err := buildFeedFromPath(feed, nick, feedFile, max)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
printFeed(feed)
|
||
|
return nil
|
||
|
}
|