Уходим с Python на Go: первые успехи #5

Merged
anufriev.g.a merged 17 commits from go into main 2023-03-15 16:33:43 +04:00
7 changed files with 79 additions and 67 deletions
Showing only changes of commit be6061daad - Show all commits

11
main.go
View File

@ -42,10 +42,10 @@ func main() {
if tg_user.PosTag == "not_started" { if tg_user.PosTag == "not_started" {
bot.Start() bot.Start()
} else if tg_user.PosTag == "add" {
bot.Find(msg.Text)
} else if msg.Text == "Главное меню" { } else if msg.Text == "Главное меню" {
bot.GetSummary() bot.GetPersonalSummary()
} else if tg_user.PosTag == "add" || tg_user.PosTag == "ready" {
bot.Find(msg.Text)
} else { } else {
bot.Etc() bot.Etc()
} }
@ -63,8 +63,11 @@ func main() {
if query.Data == "cancel" { if query.Data == "cancel" {
bot.Cancel(query) bot.Cancel(query)
} else if strings.Contains(tg_user.PosTag, "confirm") { } else if strings.Contains(tg_user.PosTag, "confirm_add") {
bot.Confirm(query) bot.Confirm(query)
} else if strings.Contains(tg_user.PosTag, "confirm_see") {
bot.SeeShedule(query)
bot.DeleteMsg(query)
} }
} }
} }

View File

@ -146,8 +146,7 @@ func addGroupOrTeacher(db *xorm.Engine, sh Shedule) error {
teacher := database.Teacher{ teacher := database.Teacher{
TeacherId: sh.SheduleId, TeacherId: sh.SheduleId,
LastName: name[0], LastName: name[0],
FirstName: name[1], FirstName: strings.Join(name[1:], " "),
MidName: name[2], // Уебать Сасау и придумать что-то с хреново прописанныит преподами
SpecName: sh.SpecName, SpecName: sh.SpecName,
} }
db.InsertOne(teacher) db.InsertOne(teacher)

View File

@ -34,8 +34,13 @@ type Shedule struct {
} }
func GetSheduleInfo(doc *goquery.Document, sh *Shedule) { func GetSheduleInfo(doc *goquery.Document, sh *Shedule) {
sh.SpecName = doc.Find(".info-block__description div").First().Text()[1:] spec := doc.Find(".info-block__description div").First().Text()
if spec != "" {
spec = spec[1:]
}
sh.SpecName = spec
sh.GroupName = doc.Find(".info-block__title").First().Text()[1:] sh.GroupName = doc.Find(".info-block__title").First().Text()[1:]
} }
// Parse goquery shedule site // Parse goquery shedule site

View File

@ -106,14 +106,18 @@ func (bot *Bot) Find(query string) error {
if len(allGroups) != 0 { if len(allGroups) != 0 {
if bot.TG_user.PosTag == "add" { if bot.TG_user.PosTag == "add" {
bot.TG_user.PosTag = "confirm_group" bot.TG_user.PosTag = "confirm_add_group"
} else {
bot.TG_user.PosTag = "confirm_see_group"
} }
msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Вот что я нашёл.\nВыбери свою группу") msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Вот что я нашёл.\nВыбери свою группу")
msg.ReplyMarkup = GenerateKeyboard(GenerateGroupsArray(allGroups), query) msg.ReplyMarkup = GenerateKeyboard(GenerateGroupsArray(allGroups), query)
bot.TG.Send(msg) bot.TG.Send(msg)
} else if len(allTeachers) != 0 { } else if len(allTeachers) != 0 {
if bot.TG_user.PosTag == "add" { if bot.TG_user.PosTag == "add" {
bot.TG_user.PosTag = "confirm_teacher" bot.TG_user.PosTag = "confirm_add_teacher"
} else {
bot.TG_user.PosTag = "confirm_see_teacher"
} }
msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Вот что я нашёл.\nВыбери нужного преподавателя") msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Вот что я нашёл.\nВыбери нужного преподавателя")
msg.ReplyMarkup = GenerateKeyboard(GenerateTeachersArray(allTeachers), query) msg.ReplyMarkup = GenerateKeyboard(GenerateTeachersArray(allTeachers), query)
@ -127,8 +131,27 @@ func (bot *Bot) Find(query string) error {
return err return err
} }
func (bot *Bot) SeeShedule(query *tgbotapi.CallbackQuery) error {
isGroup := bot.TG_user.PosTag == "confirm_see_group"
groupId, err := strconv.ParseInt(query.Data, 0, 64)
if err != nil {
return err
}
shedule := database.ShedulesInUser{
IsTeacher: !isGroup,
SheduleId: groupId,
}
err = bot.GetSummary([]database.ShedulesInUser{shedule})
if err != nil {
return err
}
bot.TG_user.PosTag = "ready"
err = bot.UpdateUserDB()
return err
}
func (bot *Bot) Confirm(query *tgbotapi.CallbackQuery) error { func (bot *Bot) Confirm(query *tgbotapi.CallbackQuery) error {
isGroup := bot.TG_user.PosTag == "confirm_group" isGroup := bot.TG_user.PosTag == "confirm_add_group"
groupId, err := strconv.ParseInt(query.Data, 0, 64) groupId, err := strconv.ParseInt(query.Data, 0, 64)
if err != nil { if err != nil {
return err return err

View File

@ -13,18 +13,24 @@ import (
"xorm.io/xorm" "xorm.io/xorm"
) )
func (bot *Bot) GetSummary(isRetry ...bool) { func (bot *Bot) GetPersonalSummary() {
now := time.Now().Add(time.Hour * time.Duration(5))
log.Println(now.Format("01-02-2006 15:04:05 -07"), now.Format("01-02-2006 15:04:05"))
var lessons []database.Lesson
var shedules []database.ShedulesInUser var shedules []database.ShedulesInUser
bot.DB.ID(bot.TG_user.L9Id).Find(&shedules) bot.DB.ID(bot.TG_user.L9Id).Find(&shedules)
if len(shedules) == 0 { if len(shedules) == 0 {
bot.Etc() bot.Etc()
return return
} else {
err := bot.GetSummary(shedules)
if err != nil {
log.Fatal(err)
}
} }
}
func (bot *Bot) GetSummary(shedules []database.ShedulesInUser, isRetry ...bool) error {
now := time.Now().Add(time.Hour * time.Duration(5) * (-1))
log.Println(now.Format("01-02-2006 15:04:05 -07"), now.Format("01-02-2006 15:04:05"))
var groups []string var groups []string
var teachers []string var teachers []string
@ -50,14 +56,17 @@ func (bot *Bot) GetSummary(isRetry ...bool) {
condition += "teacherId in (" + teachers_str + ") " condition += "teacherId in (" + teachers_str + ") "
} }
bot.DB. var lessons []database.Lesson
err := bot.DB.
Where("end > ?", now.Format("2006-01-02 15:04:05")). Where("end > ?", now.Format("2006-01-02 15:04:05")).
And(condition). And(condition).
OrderBy("begin"). OrderBy("begin").
Limit(16). Limit(16).
Find(&lessons) Find(&lessons)
log.Println(lessons) if err != nil {
return err
}
if len(lessons) != 0 { if len(lessons) != 0 {
var firstPair, secondPair []database.Lesson var firstPair, secondPair []database.Lesson
@ -80,15 +89,6 @@ func (bot *Bot) GetSummary(isRetry ...bool) {
str = "Сводка на сегодня\n\n" str = "Сводка на сегодня\n\n"
day, _ := bot.GetDayShedule(pairs) day, _ := bot.GetDayShedule(pairs)
str += day str += day
/*
firstPairStr, _ := PairToStr(firstPair, &bot.DB)
str += firstPairStr
if len(secondPair) != 0 && firstPair[0].Begin.Day() == secondPair[0].Begin.Day() {
secondPairStr, _ := PairToStr(secondPair, &bot.DB)
str += "\n--\n" + secondPairStr
}
*/
} }
msg := tgbotapi.NewMessage(bot.TG_user.TgId, str) msg := tgbotapi.NewMessage(bot.TG_user.TgId, str)
@ -98,15 +98,21 @@ func (bot *Bot) GetSummary(isRetry ...bool) {
week -= bot.Week week -= bot.Week
for _, sh := range shedules { for _, sh := range shedules {
doc, err := ssau_parser.ConnectById(sh.SheduleId, sh.IsTeacher, week) doc, err := ssau_parser.ConnectById(sh.SheduleId, sh.IsTeacher, week)
if err != nil {
return err
}
shedule, err := ssau_parser.Parse(doc, !sh.IsTeacher, sh.SheduleId, week) shedule, err := ssau_parser.Parse(doc, !sh.IsTeacher, sh.SheduleId, week)
if err != nil {
return err
}
err = ssau_parser.UploadShedule(bot.DB, *shedule) err = ssau_parser.UploadShedule(bot.DB, *shedule)
if err != nil { if err != nil {
log.Fatal(err) return err
} }
} }
bot.GetSummary(true) bot.GetSummary(shedules, true)
} }
return nil
} }
func (bot *Bot) GetDayShedule(lessons [][]database.Lesson) (string, error) { func (bot *Bot) GetDayShedule(lessons [][]database.Lesson) (string, error) {
@ -174,7 +180,7 @@ func PairToStr(pair []database.Lesson, db *xorm.Engine) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
name := fmt.Sprintf("%s %s.%s.", t.LastName, t.FirstName[0:2], t.MidName[0:2]) name := GenerateName(t)
str += fmt.Sprintf("👤 %s\n", name) str += fmt.Sprintf("👤 %s\n", name)
} }
if sublesson.SubGroup != "" { if sublesson.SubGroup != "" {

View File

@ -1,34 +0,0 @@
package tg
import (
"log"
"os"
"testing"
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
"github.com/joho/godotenv"
)
func TestGetSummary(t *testing.T) {
if err := godotenv.Load(); err != nil {
log.Fatal("No .env file found")
}
engine, err := database.Connect(os.Getenv("MYSQL_USER"), os.Getenv("MYSQL_PASS"), os.Getenv("MYSQL_DB"))
if err != nil {
log.Fatal(err)
}
bot := new(Bot)
err = bot.InitBot(os.Getenv("TELEGRAM_APITOKEN"), *engine)
if err != nil {
log.Fatal(err)
}
bot.TG_user = database.TgUser{
L9Id: 316268749,
TgId: 1086707888,
}
bot.GetSummary()
}

View File

@ -3,6 +3,7 @@ package tg
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"strings"
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database" "git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
@ -16,10 +17,19 @@ func GenerateGroupsArray(groups []database.Group) []tgbotapi.InlineKeyboardButto
return grKeys return grKeys
} }
func GenerateTeachersArray(groups []database.Teacher) []tgbotapi.InlineKeyboardButton { func GenerateName(t database.Teacher) string {
var initials string
for _, n := range strings.Split(t.FirstName, " ") {
initials += fmt.Sprintf("%s.", n[:2])
}
name := fmt.Sprintf("%s %s", t.LastName, initials)
return name
}
func GenerateTeachersArray(teachers []database.Teacher) []tgbotapi.InlineKeyboardButton {
var teacherKeys []tgbotapi.InlineKeyboardButton var teacherKeys []tgbotapi.InlineKeyboardButton
for _, t := range groups { for _, t := range teachers {
name := fmt.Sprintf("%s %s.%s.", t.LastName, t.FirstName[0:2], t.MidName[0:2]) name := GenerateName(t)
teacherKeys = append(teacherKeys, tgbotapi.NewInlineKeyboardButtonData(name, strconv.FormatInt(t.TeacherId, 10))) teacherKeys = append(teacherKeys, tgbotapi.NewInlineKeyboardButtonData(name, strconv.FormatInt(t.TeacherId, 10)))
} }
return teacherKeys return teacherKeys