made separate binaries; added -top to reddit

This commit is contained in:
Stephen McQuay 2016-05-08 09:49:00 -07:00
parent 473f9212b9
commit 4055810f22
No known key found for this signature in database
GPG Key ID: 1ABF428F71BAFC3D
3 changed files with 41 additions and 44 deletions

View File

@ -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 {

View File

@ -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 }

29
main.go
View File

@ -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
}
}