/
lottery.go
68 lines (57 loc) · 1.43 KB
/
lottery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"github.com/deet/govenmo"
"github.com/jmcvetta/randutil"
"log"
)
func main() {
account, err := venmoAccount()
if err != nil {
log.Println("Error refreshing account:", err)
} else {
log.Println("Account refreshed")
}
log.Println("balance is", account.Balance)
payments, err := paymentsSinceLastRun(&account)
if err != nil {
log.Println("Error fetching payments:", err)
return
}
balance := 0.0
choices := make([]randutil.Choice, 0)
for _, payment := range payments {
actor := payment.Actor.DisplayName
target := payment.Target.User.DisplayName
note := payment.Note
amount := payment.Amount
log.Println(actor, "paid", target, amount, "dollars for", note)
if target == "Smick Share" {
balance += amount
choice := randutil.Choice{
Weight: int(100 * amount),
Item: payment.Actor.Id,
}
choices = append(choices, choice)
}
}
if balance == 0 {
log.Println("no money to give out")
return
}
log.Println("Final balance is", balance)
log.Println(choices)
winner, err := randutil.WeightedChoice(choices)
if err != nil {
log.Println("Error selecting winner:", err)
return
}
winnerId := winner.Item
target := govenmo.Target{}
target.User.Id = winnerId.(string)
sentPayment, err := account.PayOrCharge(target, balance, "ayy lmao!", "public")
if err != nil {
log.Println("Error sending payment:", err)
} else {
log.Println("Payment succeeded", sentPayment)
}
}