diff --git a/bots/bybot/bybot.go b/bots/bybot/main.go similarity index 91% rename from bots/bybot/bybot.go rename to bots/bybot/main.go index e1e2503..150a113 100644 --- a/bots/bybot/bybot.go +++ b/bots/bybot/main.go @@ -1,6 +1,7 @@ -package bybot +package main import ( + "flag" "fmt" "log" "math/rand" @@ -10,6 +11,20 @@ import ( irc "github.com/fluffle/goirc/client" ) +var host *string = flag.String("host", "localhost:6668", "irc server hostname") +var channel *string = flag.String("channel", "#exmormon", "irc channel") + +func main() { + rand.Seed(time.Now().UnixNano()) + flag.Parse() + log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) + + done := make(chan bool) + + Register(*host, *channel, done) + <-done +} + const name = "BYbot" type BY struct { diff --git a/bots/reddit/reddit.go b/bots/reddit/main.go similarity index 78% rename from bots/reddit/reddit.go rename to bots/reddit/main.go index 88a85d3..92653b3 100644 --- a/bots/reddit/reddit.go +++ b/bots/reddit/main.go @@ -1,17 +1,33 @@ -package reddit +package main import ( "encoding/json" + "flag" "fmt" "log" + "math/rand" "net/http" "regexp" - "sort" "time" irc "github.com/fluffle/goirc/client" ) +var host *string = flag.String("host", "localhost:6668", "irc server hostname") +var channel *string = flag.String("channel", "#exmormon", "irc channel") +var top *int = flag.Int("top", 5, "number of top posts to use") + +func main() { + rand.Seed(time.Now().UnixNano()) + flag.Parse() + log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) + + done := make(chan bool) + + Register(*host, *channel, done) + <-done +} + const name = "reddit" func Register(host, channel string, done chan<- bool) { @@ -57,9 +73,7 @@ func Register(host, channel string, done chan<- bool) { } for { - log.Printf("reddit ...") latest, err := items() - log.Printf("got %+v items", len(latest)) if err != nil { log.Printf("%+v", err) } @@ -67,7 +81,7 @@ func Register(host, channel string, done chan<- bool) { if _, ok := seen[item.ID]; ok { continue } - log.Printf("%+v", item) + log.Printf("decided to talk about this: %+v", item) seen[item.ID] = item.Score c.Privmsgf(channel, "%v", item) } @@ -101,7 +115,7 @@ func (i Item) String() string { com = fmt.Sprintf(" (%d comments)", i.Comments) } - return fmt.Sprintf("(%d: %s) %s%s\n%s", i.Score, i.ID, i.Title, com, i.URL) + return fmt.Sprintf("https://redd.it/%s : %s %v", i.ID, i.Title, com) } type response struct { @@ -135,12 +149,9 @@ func items() ([]Item, error) { for _, child := range rresp.Data.Children { items = append(items, child.Data) } - sort.Sort(ByScore(items)) - return items, nil + + if len(items) < *top { + return items, nil + } + return items[:*top], nil } - -type ByScore []Item - -func (a ByScore) Len() int { return len(a) } -func (a ByScore) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a ByScore) Less(i, j int) bool { return a[i].Score > a[j].Score } diff --git a/main.go b/main.go deleted file mode 100644 index 029a174..0000000 --- a/main.go +++ /dev/null @@ -1,29 +0,0 @@ -package main - -import ( - "flag" - "log" - "math/rand" - "time" - - "mcquay.me/exmo/bots/bybot" - "mcquay.me/exmo/bots/reddit" -) - -var host *string = flag.String("host", "localhost:6668", "irc server hostname") -var channel *string = flag.String("channel", "#exmormon", "irc channel") - -func main() { - rand.Seed(time.Now().UnixNano()) - flag.Parse() - log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) - - done := make(chan bool) - - bybot.Register(*host, *channel, done) - reddit.Register(*host, *channel, done) - - for i := 0; i < 2; i++ { - <-done - } -}