Уходим с 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 45 additions and 13 deletions
Showing only changes of commit 231dacec86 - Show all commits

View File

@ -19,8 +19,10 @@ func main() {
if err != nil {
log.Fatal(err)
}
defer engine.Close()
bot := new(tg.Bot)
bot.Week = 5
err = bot.InitBot(os.Getenv("TELEGRAM_APITOKEN"), *engine)
if err != nil {
log.Fatal(err)

View File

@ -1,15 +1,15 @@
package ssau_parser
import (
"fmt"
"log"
"strconv"
"strings"
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
"xorm.io/xorm"
)
func uploadShedule(db *xorm.Engine, sh Shedule) error {
func UploadShedule(db *xorm.Engine, sh Shedule) error {
err := addGroupOrTeacher(db, sh)
if err != nil {
return err
@ -34,7 +34,7 @@ func uploadShedule(db *xorm.Engine, sh Shedule) error {
}
if !exists && subLesson.TeacherId != 0 {
uri := "/rasp?staffId=" + strconv.FormatInt(subLesson.TeacherId, 10)
uri := GenerateUri(subLesson.TeacherId, true)
doc, _, _, err := Connect(uri, sh.Week)
if err != nil {
return err
@ -55,7 +55,7 @@ func uploadShedule(db *xorm.Engine, sh Shedule) error {
}
if !exists {
uri := "/rasp?groupId=" + strconv.FormatInt(groupId, 10)
uri := GenerateUri(subLesson.TeacherId, false)
doc, _, _, err := Connect(uri, sh.Week)
if err != nil {
return err
@ -83,12 +83,22 @@ func uploadShedule(db *xorm.Engine, sh Shedule) error {
}
}
if len(pairs) > 0 {
_, err := db.Insert(pairs)
_, err := db.Insert(&pairs)
return err
}
return nil
}
func GenerateUri(id int64, isTeacher bool) string {
var uri string
if isTeacher {
uri = fmt.Sprintf("/rasp?staffId=%d", id)
} else {
uri = fmt.Sprintf("/rasp?groupId=%d", id)
}
return uri
}
func isGroupExists(db *xorm.Engine, groupId int64) (bool, error) {
var exists []database.Group
err := db.Find(&exists, database.Group{GroupId: groupId})
@ -137,7 +147,7 @@ func addGroupOrTeacher(db *xorm.Engine, sh Shedule) error {
TeacherId: sh.SheduleId,
LastName: name[0],
FirstName: name[1],
MidName: name[2],
MidName: name[2], // Уебать Сасау и придумать что-то с хреново прописанныит преподами
SpecName: sh.SpecName,
}
db.InsertOne(teacher)

View File

@ -118,3 +118,9 @@ func Connect(uri string, week int) (*goquery.Document, bool, int64, error) {
return doc, isGroup, sheduleId, nil
}
func ConnectById(id int64, isTeacher bool, week int) (*goquery.Document, error) {
uri := GenerateUri(id, isTeacher)
doc, _, _, err := Connect(uri, week)
return doc, err
}

View File

@ -30,7 +30,7 @@ func TestConnect(t *testing.T) {
}
func TestParse(t *testing.T) {
list, err := FindInRasp("2305")
list, err := FindInRasp("2108")
if err != nil {
t.Error(err)
}
@ -53,7 +53,7 @@ func TestParse(t *testing.T) {
if err != nil {
t.Error(err)
}
err = uploadShedule(engine, *shedule)
err = UploadShedule(engine, *shedule)
if err != nil {
t.Error(err)
}

View File

@ -10,8 +10,9 @@ import (
type Bot struct {
TG *tgbotapi.BotAPI
DB xorm.Engine
DB *xorm.Engine
TG_user database.TgUser
Week int
}
func (bot *Bot) InitBot(token string, engine xorm.Engine) error {
@ -22,7 +23,7 @@ func (bot *Bot) InitBot(token string, engine xorm.Engine) error {
}
bot.TG.Debug = true
bot.DB = engine
bot.DB = &engine
log.Printf("Authorized on account %s", bot.TG.Self.UserName)
return nil

View File

@ -12,7 +12,7 @@ import (
)
func (bot *Bot) InitUser(id int64, name string) (*database.TgUser, error) {
db := &bot.DB
db := bot.DB
var users []database.TgUser
err := db.Find(&users, &database.TgUser{TgId: id})
if err != nil {

View File

@ -8,11 +8,12 @@ import (
"time"
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/ssau_parser"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"xorm.io/xorm"
)
func (bot *Bot) GetSummary() {
func (bot *Bot) GetSummary(isRetry ...bool) {
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"))
@ -92,6 +93,18 @@ func (bot *Bot) GetSummary() {
msg := tgbotapi.NewMessage(bot.TG_user.TgId, str)
bot.TG.Send(msg)
} else if len(isRetry) == 0 {
_, week := time.Now().ISOWeek()
week -= bot.Week
for _, sh := range shedules {
doc, err := ssau_parser.ConnectById(sh.SheduleId, sh.IsTeacher, week)
shedule, err := ssau_parser.Parse(doc, !sh.IsTeacher, sh.SheduleId, week)
err = ssau_parser.UploadShedule(bot.DB, *shedule)
if err != nil {
log.Fatal(err)
}
}
bot.GetSummary(true)
}
}
@ -101,7 +114,7 @@ func (bot *Bot) GetDayShedule(lessons [][]database.Lesson) (string, error) {
day := lessons[0][0].Begin.Day()
for _, pair := range lessons {
if pair[0].Begin.Day() == day {
line, err := PairToStr(pair, &bot.DB)
line, err := PairToStr(pair, bot.DB)
if err != nil {
return "", err
}