made separate binaries; added -top to reddit
This commit is contained in:
parent
473f9212b9
commit
4055810f22
@ -1,6 +1,7 @@
|
|||||||
package bybot
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
@ -10,6 +11,20 @@ import (
|
|||||||
irc "github.com/fluffle/goirc/client"
|
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"
|
const name = "BYbot"
|
||||||
|
|
||||||
type BY struct {
|
type BY struct {
|
@ -1,17 +1,33 @@
|
|||||||
package reddit
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
irc "github.com/fluffle/goirc/client"
|
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"
|
const name = "reddit"
|
||||||
|
|
||||||
func Register(host, channel string, done chan<- bool) {
|
func Register(host, channel string, done chan<- bool) {
|
||||||
@ -57,9 +73,7 @@ func Register(host, channel string, done chan<- bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
log.Printf("reddit ...")
|
|
||||||
latest, err := items()
|
latest, err := items()
|
||||||
log.Printf("got %+v items", len(latest))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("%+v", err)
|
log.Printf("%+v", err)
|
||||||
}
|
}
|
||||||
@ -67,7 +81,7 @@ func Register(host, channel string, done chan<- bool) {
|
|||||||
if _, ok := seen[item.ID]; ok {
|
if _, ok := seen[item.ID]; ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Printf("%+v", item)
|
log.Printf("decided to talk about this: %+v", item)
|
||||||
seen[item.ID] = item.Score
|
seen[item.ID] = item.Score
|
||||||
c.Privmsgf(channel, "%v", item)
|
c.Privmsgf(channel, "%v", item)
|
||||||
}
|
}
|
||||||
@ -101,7 +115,7 @@ func (i Item) String() string {
|
|||||||
com = fmt.Sprintf(" (%d comments)", i.Comments)
|
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 {
|
type response struct {
|
||||||
@ -135,12 +149,9 @@ func items() ([]Item, error) {
|
|||||||
for _, child := range rresp.Data.Children {
|
for _, child := range rresp.Data.Children {
|
||||||
items = append(items, child.Data)
|
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
29
main.go
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user