Уходим с Python на Go: первые успехи #5
2
main.go
2
main.go
|
@ -19,8 +19,10 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
defer engine.Close()
|
||||||
|
|
||||||
bot := new(tg.Bot)
|
bot := new(tg.Bot)
|
||||||
|
bot.Week = 5
|
||||||
err = bot.InitBot(os.Getenv("TELEGRAM_APITOKEN"), *engine)
|
err = bot.InitBot(os.Getenv("TELEGRAM_APITOKEN"), *engine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
package ssau_parser
|
package ssau_parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
|
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
|
||||||
"xorm.io/xorm"
|
"xorm.io/xorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func uploadShedule(db *xorm.Engine, sh Shedule) error {
|
func UploadShedule(db *xorm.Engine, sh Shedule) error {
|
||||||
err := addGroupOrTeacher(db, sh)
|
err := addGroupOrTeacher(db, sh)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -34,7 +34,7 @@ func uploadShedule(db *xorm.Engine, sh Shedule) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !exists && subLesson.TeacherId != 0 {
|
if !exists && subLesson.TeacherId != 0 {
|
||||||
uri := "/rasp?staffId=" + strconv.FormatInt(subLesson.TeacherId, 10)
|
uri := GenerateUri(subLesson.TeacherId, true)
|
||||||
doc, _, _, err := Connect(uri, sh.Week)
|
doc, _, _, err := Connect(uri, sh.Week)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -55,7 +55,7 @@ func uploadShedule(db *xorm.Engine, sh Shedule) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
uri := "/rasp?groupId=" + strconv.FormatInt(groupId, 10)
|
uri := GenerateUri(subLesson.TeacherId, false)
|
||||||
doc, _, _, err := Connect(uri, sh.Week)
|
doc, _, _, err := Connect(uri, sh.Week)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -83,12 +83,22 @@ func uploadShedule(db *xorm.Engine, sh Shedule) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(pairs) > 0 {
|
if len(pairs) > 0 {
|
||||||
_, err := db.Insert(pairs)
|
_, err := db.Insert(&pairs)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
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) {
|
func isGroupExists(db *xorm.Engine, groupId int64) (bool, error) {
|
||||||
var exists []database.Group
|
var exists []database.Group
|
||||||
err := db.Find(&exists, database.Group{GroupId: groupId})
|
err := db.Find(&exists, database.Group{GroupId: groupId})
|
||||||
|
@ -137,7 +147,7 @@ func addGroupOrTeacher(db *xorm.Engine, sh Shedule) error {
|
||||||
TeacherId: sh.SheduleId,
|
TeacherId: sh.SheduleId,
|
||||||
LastName: name[0],
|
LastName: name[0],
|
||||||
FirstName: name[1],
|
FirstName: name[1],
|
||||||
MidName: name[2],
|
MidName: name[2], // Уебать Сасау и придумать что-то с хреново прописанныит преподами
|
||||||
SpecName: sh.SpecName,
|
SpecName: sh.SpecName,
|
||||||
}
|
}
|
||||||
db.InsertOne(teacher)
|
db.InsertOne(teacher)
|
||||||
|
|
|
@ -118,3 +118,9 @@ func Connect(uri string, week int) (*goquery.Document, bool, int64, error) {
|
||||||
|
|
||||||
return doc, isGroup, sheduleId, nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ func TestConnect(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParse(t *testing.T) {
|
func TestParse(t *testing.T) {
|
||||||
list, err := FindInRasp("2305")
|
list, err := FindInRasp("2108")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ func TestParse(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
err = uploadShedule(engine, *shedule)
|
err = UploadShedule(engine, *shedule)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,8 +10,9 @@ import (
|
||||||
|
|
||||||
type Bot struct {
|
type Bot struct {
|
||||||
TG *tgbotapi.BotAPI
|
TG *tgbotapi.BotAPI
|
||||||
DB xorm.Engine
|
DB *xorm.Engine
|
||||||
TG_user database.TgUser
|
TG_user database.TgUser
|
||||||
|
Week int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bot *Bot) InitBot(token string, engine xorm.Engine) error {
|
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.TG.Debug = true
|
||||||
|
|
||||||
bot.DB = engine
|
bot.DB = &engine
|
||||||
|
|
||||||
log.Printf("Authorized on account %s", bot.TG.Self.UserName)
|
log.Printf("Authorized on account %s", bot.TG.Self.UserName)
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (bot *Bot) InitUser(id int64, name string) (*database.TgUser, error) {
|
func (bot *Bot) InitUser(id int64, name string) (*database.TgUser, error) {
|
||||||
db := &bot.DB
|
db := bot.DB
|
||||||
var users []database.TgUser
|
var users []database.TgUser
|
||||||
err := db.Find(&users, &database.TgUser{TgId: id})
|
err := db.Find(&users, &database.TgUser{TgId: id})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -8,11 +8,12 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
|
"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"
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
"xorm.io/xorm"
|
"xorm.io/xorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (bot *Bot) GetSummary() {
|
func (bot *Bot) GetSummary(isRetry ...bool) {
|
||||||
now := time.Now().Add(time.Hour * time.Duration(5))
|
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"))
|
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)
|
msg := tgbotapi.NewMessage(bot.TG_user.TgId, str)
|
||||||
bot.TG.Send(msg)
|
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()
|
day := lessons[0][0].Begin.Day()
|
||||||
for _, pair := range lessons {
|
for _, pair := range lessons {
|
||||||
if pair[0].Begin.Day() == day {
|
if pair[0].Begin.Day() == day {
|
||||||
line, err := PairToStr(pair, &bot.DB)
|
line, err := PairToStr(pair, bot.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue