From cdff69abdc27e1996c3c3cab50e95cb268ced7ae Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Mon, 15 Jul 2019 12:46:24 -0400 Subject: [PATCH] bid: Add a skeleton of an n-gate bidding game * webshit module will house any logic needed for the game * newsbid is the top-level plugin to be used for the bot interface --- go.mod | 5 +- go.sum | 6 +- plugins/newsbid/newsbid.go | 25 ++++++++ plugins/newsbid/webshit/webshit.go | 76 +++++++++++++++++++++++++ plugins/newsbid/webshit/webshit_test.go | 30 ++++++++++ 5 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 plugins/newsbid/newsbid.go create mode 100644 plugins/newsbid/webshit/webshit.go create mode 100644 plugins/newsbid/webshit/webshit_test.go diff --git a/go.mod b/go.mod index 6d3988c..fa4a86a 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,8 @@ module github.com/velour/catbase require ( - github.com/PuerkitoBio/goquery v1.5.0 // indirect + github.com/PaulRosset/go-hacknews v0.0.0-20170815075127-4aad99273a3c + github.com/PuerkitoBio/goquery v1.5.0 github.com/armon/go-radix v1.0.0 // indirect github.com/chrissexton/leftpad v0.0.0-20181207133115-1e93189d2fff github.com/go-sql-driver/mysql v1.4.1 // indirect @@ -26,7 +27,7 @@ require ( github.com/velour/chat v0.0.0-20180713122344-fd1d1606cb89 github.com/velour/velour v0.0.0-20160303155839-8e090e68d158 golang.org/x/exp v0.0.0-20190321205749-f0864edee7f3 // indirect - golang.org/x/net v0.0.0-20190326090315-15845e8f865b // indirect + golang.org/x/net v0.0.0-20190606173856-1492cefac77f // indirect gonum.org/v1/gonum v0.0.0-20190325211145-e42c1265cdd5 // indirect google.golang.org/appengine v1.4.0 // indirect gopkg.in/sourcemap.v1 v1.0.5 // indirect diff --git a/go.sum b/go.sum index a5f9830..646cbc7 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/AlekSi/pointer v1.0.0 h1:KWCWzsvFxNLcmM5XmiqHsGTTsuwZMsLFwWF9Y+//bNE= github.com/AlekSi/pointer v1.0.0/go.mod h1:1kjywbfcPFCmncIxtk6fIEub6LKrfMz3gc5QKVOSOA8= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/PaulRosset/go-hacknews v0.0.0-20170815075127-4aad99273a3c h1:bJ0HbTMaInVjakxM76G+2gsmbKTdHzpTUGyLGYxdMO0= +github.com/PaulRosset/go-hacknews v0.0.0-20170815075127-4aad99273a3c/go.mod h1:8+24kIp7vJsYy0GmQDDNnPwAYEWkl3OcaPxJSDAfe1U= github.com/PuerkitoBio/goquery v1.5.0 h1:uGvmFXOA73IKluu/F84Xd1tt/z07GYm8X49XKHP7EJk= github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg= github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o= @@ -74,8 +76,8 @@ golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190326090315-15845e8f865b h1:LlDMQZ0I/u8J45sbt31TecpsFNErRGwDgS4WvT9hKzE= -golang.org/x/net v0.0.0-20190326090315-15845e8f865b/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190606173856-1492cefac77f h1:IWHgpgFqnL5AhBUBZSgBdjl2vkQUEzcY+JNKWfcgAU0= +golang.org/x/net v0.0.0-20190606173856-1492cefac77f/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= diff --git a/plugins/newsbid/newsbid.go b/plugins/newsbid/newsbid.go new file mode 100644 index 0000000..c2355a1 --- /dev/null +++ b/plugins/newsbid/newsbid.go @@ -0,0 +1,25 @@ +package newsbid + +import ( + "github.com/jmoiron/sqlx" + "github.com/velour/catbase/bot" + "github.com/velour/catbase/bot/msg" +) + +type NewsBid struct { + bot bot.Bot + db *sqlx.DB +} + +func New(b bot.Bot) *NewsBid { + p := &NewsBid{ + bot: b, + db: b.DB(), + } + p.bot.Register(p, bot.Message, p.message) + return p +} + +func (p *NewsBid) message(conn bot.Connector, k bot.Kind, message msg.Message, args ...interface{}) bool { + return false +} diff --git a/plugins/newsbid/webshit/webshit.go b/plugins/newsbid/webshit/webshit.go new file mode 100644 index 0000000..a06b6eb --- /dev/null +++ b/plugins/newsbid/webshit/webshit.go @@ -0,0 +1,76 @@ +package webshit + +import ( + "bytes" + "fmt" + "github.com/PaulRosset/go-hacknews" + "github.com/PuerkitoBio/goquery" + "github.com/jmoiron/sqlx" + "github.com/mmcdole/gofeed" + "net/url" +) + +type Webshit struct { + db *sqlx.DB +} + +func New(db *sqlx.DB) *Webshit { + w := &Webshit{db} + w.setup() + return w +} + +// setup will create any necessary SQL tables and populate them with minimal data +func (w *Webshit) setup() { +} + +// GetHeadlines will return the current possible news headlines for bidding +func (w *Webshit) GetHeadlines() ([]hacknews.Post, error) { + news := hacknews.Initializer{Story: "topstories", NbPosts: 10} + ids, err := news.GetCodesStory() + if err != nil { + return nil, err + } + posts, err := news.GetPostStory(ids) + if err != nil { + return nil, err + } + return posts, nil +} + +type Weekly []string + +// GetWeekly will return the headlines in the last webshit weekly report +func (w *Webshit) GetWeekly() (Weekly, error) { + fp := gofeed.NewParser() + feed, err := fp.ParseURL("http://n-gate.com/hackernews/index.rss") + if err != nil { + return nil, err + } + if len(feed.Items) <= 0 { + return nil, fmt.Errorf("no webshit weekly found") + } + + buf := bytes.NewBufferString(feed.Items[0].Description) + doc, err := goquery.NewDocumentFromReader(buf) + if err != nil { + return nil, err + } + + var items []string + doc.Find(".storylink").Each(func(i int, s *goquery.Selection) { + items = append(items, s.Find("a").Text()) + }) + + return items, nil +} + +// GetBalances returns the current balance for all known users +// Any unknown user has a default balance on their first bid +func (w *Webshit) GetBalances() { +} + +// Bid allows a user to place a bid on a particular story +func (w *Webshit) Bid(user string, amount int, URL url.URL) error { + return nil +} diff --git a/plugins/newsbid/webshit/webshit_test.go b/plugins/newsbid/webshit/webshit_test.go new file mode 100644 index 0000000..4d0c278 --- /dev/null +++ b/plugins/newsbid/webshit/webshit_test.go @@ -0,0 +1,30 @@ +package webshit + +import ( + "github.com/jmoiron/sqlx" + "testing" + + _ "github.com/mattn/go-sqlite3" +) + +func make(t *testing.T) *Webshit { + db := sqlx.MustOpen("sqlite3", "file::memory:?mode=memory&cache=shared") + w := New(db) + if w.db != db { + t.Fail() + } + return w +} + +func TestWebshit_GetWeekly(t *testing.T) { + w := make(t) + weekly, err := w.GetWeekly() + if err != nil { + t.Errorf("Could not get weekly: %s", err) + t.Fail() + } + if len(weekly) < 5 { + t.Errorf("Weekly content:\n%+v", weekly) + t.Fail() + } +}