newsbid: improve display of news items

This commit is contained in:
Chris Sexton 2019-12-20 13:31:05 -05:00 committed by Chris Sexton
parent 7b8f37d67d
commit 408794fe58
5 changed files with 45 additions and 25 deletions

View File

@ -82,7 +82,7 @@ func (p *NewsBid) message(conn bot.Connector, k bot.Kind, message msg.Message, a
}
amount, _ := strconv.Atoi(parts[1])
url := parts[2]
if bid, err := p.ws.Bid(message.User.Name, amount, url); err != nil {
if bid, err := p.ws.Bid(message.User.Name, amount, parts[1], url); err != nil {
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Error placing bid: %s", err))
} else {
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Your bid has been placed on %s", bid.Title))
@ -128,10 +128,10 @@ func (p *NewsBid) check(conn bot.Connector, ch string) {
msg := fmt.Sprintf("%s%s won %d for a score of %d",
icon, res.User, res.Won, res.Score)
if len(res.WinningArticles) > 0 {
msg += "\nWinning articles: " + res.WinningArticles.Titles()
msg += "\nWinning articles: \n" + res.WinningArticles.Titles()
}
if len(res.LosingArticles) > 0 {
msg += "\nLosing articles: " + res.LosingArticles.Titles()
msg += "\nLosing articles: \n" + res.LosingArticles.Titles()
}
p.bot.Send(conn, bot.Message, ch, msg)
}

View File

@ -33,11 +33,9 @@ type Items []Item
func (is Items) Titles() string {
out := ""
for i, v := range is {
if i > 0 {
out += ", "
}
out += fmt.Sprintf("<%s|%s>", v.URL, v.Title)
for _, v := range is {
hnURL := fmt.Sprintf("https://news.ycombinator.com/item?id=%d", v.ID)
out += fmt.Sprintf("• %s <%s|%s> (<%s|Comments>)\n", v.Bid, v.URL, v.Title, hnURL)
}
return out
}

View File

@ -16,4 +16,9 @@ type Item struct {
Title string `json:"title"`
Parts []int `json:"parts"`
Descendants int `json:"descendants"`
// This is not in the API but it's
// easier to hack it in here than
// fix my code.
Bid string `json:"bid"`
}

View File

@ -38,7 +38,9 @@ type Bid struct {
User string
Title string
URL string
HNID int `db:"hnid"`
Bid int
BidStr string
PlacedScore int `db:"placed_score"`
ProcessedScore int `db:"processed_score"`
Placed int64
@ -80,7 +82,9 @@ func (w *Webshit) setup() {
user string,
title string,
url string,
hnid string,
bid integer,
bidstr string,
placed_score integer,
processed_score integer,
placed integer,
@ -100,14 +104,21 @@ func (w *Webshit) Check(last int64) ([]WeeklyResult, int64, error) {
}
if published.Unix() <= last {
log.Debug().Msgf("No new ngate: %v vs %v", published.Unix(), last)
return nil, 0, fmt.Errorf("no new ngate")
}
var bids []Bid
if err = w.db.Select(&bids, `select user,title,url,bid from webshit_bids where processed=0`); err != nil {
if err = w.db.Select(&bids, `select user,title,url,hnid,bid,bidstr from webshit_bids where processed=0`); err != nil {
return nil, 0, err
}
log.Debug().
Interface("bids", bids).
Interface("ngate", stories).
Interface("published", published).
Msg("checking ngate")
// Assuming no bids earlier than the weekly means there hasn't been a new weekly
if len(bids) == 0 {
return nil, 0, fmt.Errorf("there are no bids against the current ngate post")
@ -115,8 +126,7 @@ func (w *Webshit) Check(last int64) ([]WeeklyResult, int64, error) {
storyMap := map[string]hn.Item{}
for _, s := range stories {
id := strconv.Itoa(s.ID)
storyMap[id] = s
storyMap[s.URL] = s
}
wr := w.checkBids(bids, storyMap)
@ -127,8 +137,8 @@ func (w *Webshit) Check(last int64) ([]WeeklyResult, int64, error) {
}
// Delete all those bids
if _, err = w.db.Exec(`update webshit_bids set processed=? where placed < ?`,
time.Now().Unix(), published.Unix()); err != nil {
if _, err = w.db.Exec(`update webshit_bids set processed=? where processed=0`,
time.Now().Unix()); err != nil {
return nil, 0, err
}
@ -157,19 +167,19 @@ func (w *Webshit) checkBids(bids []Bid, storyMap map[string]hn.Item) []WeeklyRes
}
rec := wr[b.User]
u, err := url.Parse(b.URL)
if err != nil {
log.Error().Err(err).Msg("couldn't parse URL")
continue
}
id := u.Query().Get("id")
if s, ok := storyMap[id]; ok {
if s, ok := storyMap[b.URL]; ok {
wins = append(wins, b)
s.Bid = b.BidStr
rec.WinningArticles = append(rec.WinningArticles, s)
totalWinning += float64(b.Bid)
} else {
rec.LosingArticles = append(rec.LosingArticles, hn.Item{Title: b.Title, URL: b.URL})
bid := hn.Item{
ID: b.HNID,
URL: b.URL,
Title: b.Title,
Bid: b.BidStr,
}
rec.LosingArticles = append(rec.LosingArticles, bid)
}
total += float64(b.Bid)
wr[b.User] = rec
@ -274,7 +284,7 @@ func (w *Webshit) GetAllBalances() ([]Balance, error) {
}
// Bid allows a user to place a bid on a particular story
func (w *Webshit) Bid(user string, amount int, URL string) (Bid, error) {
func (w *Webshit) Bid(user string, amount int, bidStr, URL string) (Bid, error) {
bal := w.GetBalance(user)
if amount < 0 {
return Bid{}, fmt.Errorf("cannot bid less than 0")
@ -290,8 +300,8 @@ func (w *Webshit) Bid(user string, amount int, URL string) (Bid, error) {
ts := time.Now().Unix()
tx := w.db.MustBegin()
_, err = tx.Exec(`insert into webshit_bids (user,title,url,bid,placed,processed,placed_score,processed_score) values (?,?,?,?,?,0,?,0)`,
user, story.Title, story.URL, amount, ts, story.Score)
_, err = tx.Exec(`insert into webshit_bids (user,title,url,hnid,bid,bidstr,placed,processed,placed_score,processed_score) values (?,?,?,?,?,?,?,0,?,0)`,
user, story.Title, story.URL, story.ID, amount, bidStr, ts, story.Score)
if err != nil {
if err := tx.Rollback(); err != nil {
return Bid{}, err

View File

@ -0,0 +1,7 @@
package webshit
//func TestWebshit_Check(t *testing.T) {
// mb := bot.NewMockBot()
// ws := New(mb.DB())
// ws.checkBids()
//}