Skip to content

Commit

Permalink
commit all at once
Browse files Browse the repository at this point in the history
  • Loading branch information
stamm committed Mar 2, 2024
1 parent 5e71736 commit d34aa1a
Show file tree
Hide file tree
Showing 18 changed files with 1,025 additions and 87 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/intelinvest_test1.csv
/example.csv
/.envrc
*.out
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
.DEFAULT_GOAL := run
MODE ?= stat
RACE ?= -race
.PHONY: run
run:
go run -race cmd/cli/main.go -m ${MODE}
go run ${RACE} cmd/cli/main.go -m ${MODE}
run_alt:
go run -race -tags alt cmd/cli/main.go -m ${MODE}
go run ${RACE} -tags alt cmd/cli/main.go -m ${MODE}
expect:
go run -race cmd/cli/main.go -m expect
go run ${RACE} cmd/cli/main.go -m expect
csv:
go run -race cmd/csv/main.go > intelinvest_test1.csv
go run ${RACE} cmd/csv/main.go > intelinvest_test1.csv
action:
go run ${RACE} cmd/cli/main.go -m what_buy
secondary:
go run ${RACE} cmd/cli/main.go -m secondary

.PHONY: bot
bot:
Expand Down
133 changes: 123 additions & 10 deletions cmd/bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import (
cron "github.com/robfig/cron/v3"
)

var keyboard = tgbotapi.NewReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("action"),
tgbotapi.NewKeyboardButton("secondary"),
),
)

func main() {
slog.Info("Start daemon")
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
Expand All @@ -23,7 +30,8 @@ func main() {
}

bot.Debug = true
cronStart(bot)
cronDailyStart(bot)
cronHourlyStart(bot)

// logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
logger := log.New(os.Stderr, "[tg]: ", log.LstdFlags|log.Lmsgprefix)
Expand All @@ -41,21 +49,80 @@ func main() {
reply := "Для старта нужно ввести куку sessionid из браузера. Эта кука очень приватна. Мы не храним введёную куку, поэтому её нужно указывать каждый раз.\nTo start, you must enter the sessionid cookie from your browser. This cookie is very private. We don't store entered cookie. You need to enter cookie sessionid every time."
isErr, isStart := false, false
if update.Message.Text != "/start" {
var err error
cookies := strings.Split(update.Message.Text, ",")
reply, err = pkg.Run(context.Background(), cookies, false, true)
if err != nil {
reply = "Error: " + err.Error()
isErr = true
switch update.Message.Text {
case "action":
data := strings.Split(os.Getenv("JETLEND_CFG"), ";")
cfg := make(map[int64][]string, len(data))
for _, v := range data {
s := strings.Split(v, "=")
n, err := strconv.Atoi(s[0])
if err != nil {
continue
}
sids := strings.Split(s[1], ",")
cfg[int64(n)] = sids
}
log.Printf("+%v", cfg)
ctx := context.Background()
sids, ok := cfg[update.Message.From.ID]
if ok {
var have bool
reply, have, err = pkg.WhatBuy(ctx, sids, false, true)
if err != nil {
reply = "Error: " + err.Error()
isErr = true
}
if !have {
reply = ""
}
}
case "secondary":
data := strings.Split(os.Getenv("JETLEND_CFG"), ";")
cfg := make(map[int64][]string, len(data))
for _, v := range data {
s := strings.Split(v, "=")
n, err := strconv.Atoi(s[0])
if err != nil {
continue
}
sids := strings.Split(s[1], ",")
cfg[int64(n)] = sids
}
log.Printf("+%v", cfg)
ctx := context.Background()
sids, ok := cfg[update.Message.From.ID]
if ok {
var have bool
reply, have, err = pkg.SecondaryMarket(ctx, sids, false, true)
if err != nil {
reply = "Error: " + err.Error()
isErr = true
}
if !have {
reply = ""
}
}
default:
var err error
cookies := strings.Split(update.Message.Text, ",")
reply, err = pkg.Run(context.Background(), cookies, false, true)
if err != nil {
reply = "Error: " + err.Error()
isErr = true
}
}
} else {
isStart = true
}

log.Printf("reply: %s", reply)
if reply == "" {
continue
}

msg := tgbotapi.NewMessage(update.Message.Chat.ID, reply)
msg.ReplyToMessageID = update.Message.MessageID
msg.ReplyMarkup = keyboard
if !isErr && !isStart {
msg.ParseMode = "MarkdownV2"
}
Expand All @@ -66,19 +133,28 @@ func main() {
}
}

func cronStart(bot *tgbotapi.BotAPI) error {
func cronDailyStart(bot *tgbotapi.BotAPI) error {
logger := log.New(os.Stderr, "[cron]: ", log.LstdFlags|log.Lmsgprefix)
c := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(logger)))
schedule := os.Getenv("JETLEND_SCHEDULE")
if schedule == "" {
schedule = "0 21 * * *"
}
_, err := c.AddFunc(schedule, func() { send(bot) })
_, err := c.AddFunc(schedule, func() { sendDaily(bot) })
c.Start()
return err
}

func send(bot *tgbotapi.BotAPI) {
func cronHourlyStart(bot *tgbotapi.BotAPI) error {
logger := log.New(os.Stderr, "[cron]: ", log.LstdFlags|log.Lmsgprefix)
c := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(logger)))
schedule := "19 6-17 * * *"
_, err := c.AddFunc(schedule, func() { sendHourly(bot) })
c.Start()
return err
}

func sendDaily(bot *tgbotapi.BotAPI) {
log.Printf("start sending by cron\n")
data := strings.Split(os.Getenv("JETLEND_CFG"), ";")
d, ok := os.LookupEnv("JETLEND_DAYS")
Expand Down Expand Up @@ -129,3 +205,40 @@ func send(bot *tgbotapi.BotAPI) {
}
}
}

func sendHourly(bot *tgbotapi.BotAPI) {
log.Printf("start sending by cron\n")
data := strings.Split(os.Getenv("JETLEND_CFG"), ";")
cfg := make(map[int64][]string, len(data))
for _, v := range data {
s := strings.Split(v, "=")
n, err := strconv.Atoi(s[0])
if err != nil {
continue
}
sids := strings.Split(s[1], ",")
cfg[int64(n)] = sids
}
log.Printf("+%v", cfg)
ctx := context.Background()
for chatID, sids := range cfg {
isErr := false
reply, have, err := pkg.WhatBuy(ctx, sids, false, true)
if err != nil {
reply = "Error: " + err.Error()
isErr = true
}
if !have {
continue
}

msg := tgbotapi.NewMessage(chatID, reply)
if !isErr {
msg.ParseMode = "MarkdownV2"
}

if _, err := bot.Send(msg); err != nil {
log.Printf("error send for chat_id %d: %s", chatID, err)
}
}
}
16 changes: 16 additions & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ func main() {
panic(err)
}
fmt.Print(msg)
case "what_buy":
msg, have, err := pkg.WhatBuy(ctx, cookies, true, false)
if err != nil {
panic(err)
}
if have {
fmt.Print(msg)
}
case "secondary":
msg, have, err := pkg.SecondaryMarket(ctx, cookies, true, false)
if err != nil {
panic(err)
}
if have {
fmt.Print(msg)
}
}
// stop()
// fmt.Printf("ctx done: %s", <-ctx.Done())
Expand Down
21 changes: 12 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
module github.com/stamm/jetlend

go 1.21
go 1.22

require (
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a
github.com/jedib0t/go-pretty/v6 v6.5.4
github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.8.2
golang.org/x/sync v0.1.0
golang.org/x/text v0.8.0
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.6.0
golang.org/x/text v0.14.0
gotest.tools v2.2.0+incompatible
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gocarina/gocsv v0.0.0-20230325173030-9a18a846a479 // indirect
github.com/jedib0t/go-pretty/v6 v6.4.6 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.5.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/sys v0.17.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
22 changes: 22 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@ github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGi
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/gocarina/gocsv v0.0.0-20230325173030-9a18a846a479 h1:KaCpc4e48emF9hYmMB9INyfpGJHAZxEAS9EqWFkpTig=
github.com/gocarina/gocsv v0.0.0-20230325173030-9a18a846a479/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a h1:RYfmiM0zluBJOiPDJseKLEN4BapJ42uSi9SZBQ2YyiA=
github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jedib0t/go-pretty/v6 v6.4.6 h1:v6aG9h6Uby3IusSSEjHaZNXpHFhzqMmjXcPq1Rjl9Jw=
github.com/jedib0t/go-pretty/v6 v6.4.6/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs=
github.com/jedib0t/go-pretty/v6 v6.5.4 h1:gOGo0613MoqUcf0xCj+h/V3sHDaZasfv152G6/5l91s=
github.com/jedib0t/go-pretty/v6 v6.5.4/go.mod h1:5LQIxa52oJ/DlDSLv0HEkWOFMDGoWkJb9ss5KqPpJBg=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -24,15 +36,25 @@ github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
16 changes: 16 additions & 0 deletions http/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package http

import (
"context"

"github.com/stamm/jetlend/pkg"
)

type HTTP interface {
ExpectAmount(ctx context.Context, cfg pkg.Config, days int) ([]byte, error)
// LoansPorfolio(ctx context.Context, cfg pkg.Config) (string, error)
// Delayed(ctx context.Context, cfg pkg.Config) (int, error)
// Balance(ctx context.Context, cfg pkg.Config) (int, int, error)
// PrimaryMarket(ctx context.Context, cfg pkg.Config) (string, error)
// SecondaryMarket(ctx context.Context, cfg pkg.Config) (string, error)
}
Loading

0 comments on commit d34aa1a

Please sign in to comment.