Добавлено: расписание на неделю (получение таблицы и первый скелет в HTML)
This commit is contained in:
parent
e48df42bb0
commit
ea69e5dd2d
2
go.mod
2
go.mod
|
@ -11,6 +11,8 @@ require (
|
|||
|
||||
require (
|
||||
github.com/andybalholm/cascadia v1.3.1 // indirect
|
||||
github.com/deckarep/golang-set/v2 v2.3.0 // indirect
|
||||
github.com/icza/gox v0.0.0-20230117093757-93f961aa2755 // indirect
|
||||
golang.org/x/net v0.7.0 // indirect
|
||||
)
|
||||
|
||||
|
|
4
go.sum
4
go.sum
|
@ -48,6 +48,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma
|
|||
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/deckarep/golang-set/v2 v2.3.0 h1:qs18EKUfHm2X9fA50Mr/M5hccg2tNnVqsiBImnyDs0g=
|
||||
github.com/deckarep/golang-set/v2 v2.3.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
|
||||
github.com/denisenkom/go-mssqldb v0.10.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
|
||||
|
@ -137,6 +139,8 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p
|
|||
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
|
||||
github.com/icza/gox v0.0.0-20230117093757-93f961aa2755 h1:CdxhyIdXDB8Ilp3ogohR8g1omzjKzDsVh5kd71M7Pvc=
|
||||
github.com/icza/gox v0.0.0-20230117093757-93f961aa2755/go.mod h1:VbcN86fRkkUMPX2ufM85Um8zFndLZswoIW1eYtpAcVk=
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
|
||||
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
|
||||
|
|
7
main.go
7
main.go
|
@ -96,6 +96,13 @@ func main() {
|
|||
}
|
||||
bot.GetSummary(shedule, false, *query.Message)
|
||||
}
|
||||
} else if strings.Contains(query.Data, "week") {
|
||||
data := strings.Split(query.Data, "_")
|
||||
shedule, dw, err := tg.ParseQuery(data)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
bot.GetWeekSummary(shedule, dw, false, *query.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,14 +35,15 @@ type Teacher struct {
|
|||
}
|
||||
|
||||
type Lesson struct {
|
||||
LessonId int64 `xorm:"pk autoincr"`
|
||||
Type string
|
||||
Name string
|
||||
GroupId int64
|
||||
Begin time.Time
|
||||
End time.Time
|
||||
TeacherId int64
|
||||
Place string
|
||||
Comment string
|
||||
SubGroup string
|
||||
LessonId int64 `xorm:"pk autoincr"`
|
||||
NumInShedule int
|
||||
Type string
|
||||
Name string
|
||||
GroupId int64
|
||||
Begin time.Time
|
||||
End time.Time
|
||||
TeacherId int64
|
||||
Place string
|
||||
Comment string
|
||||
SubGroup string
|
||||
}
|
||||
|
|
|
@ -73,6 +73,7 @@ func UploadShedule(db *xorm.Engine, sh Shedule) error {
|
|||
return err
|
||||
}
|
||||
if len(existsLessons) == 0 {
|
||||
pair.NumInShedule = lesson.NumInShedule
|
||||
pair.Place = subLesson.Place
|
||||
pair.Comment = subLesson.Comment
|
||||
pair.SubGroup = subLesson.SubGroup
|
||||
|
|
|
@ -9,9 +9,10 @@ import (
|
|||
)
|
||||
|
||||
type Lesson struct {
|
||||
Begin time.Time
|
||||
End time.Time
|
||||
SubLessons []SubLesson
|
||||
Begin time.Time
|
||||
End time.Time
|
||||
NumInShedule int
|
||||
SubLessons []SubLesson
|
||||
}
|
||||
|
||||
type SubLesson struct {
|
||||
|
@ -83,9 +84,10 @@ func Parse(doc *goquery.Document, isGroup bool, sheduleId int64, week int) (*She
|
|||
}
|
||||
idx := (len(raw_dates))*t/2 + d
|
||||
lesson := Lesson{
|
||||
Begin: begin,
|
||||
End: end,
|
||||
SubLessons: lessons[idx],
|
||||
Begin: begin,
|
||||
End: end,
|
||||
NumInShedule: t / 2,
|
||||
SubLessons: lessons[idx],
|
||||
}
|
||||
time_line = append(time_line, lesson)
|
||||
}
|
||||
|
|
|
@ -160,8 +160,60 @@ func (bot *Bot) GetDaySummary(shedules []database.ShedulesInUser, dt int, isPers
|
|||
}
|
||||
|
||||
func (bot *Bot) GetLessons(shedules []database.ShedulesInUser, now time.Time, isRetry ...int) ([]database.Lesson, error) {
|
||||
log.Println(now.Format("01-02-2006 15:04:05 -07"), now.Format("01-02-2006 15:04:05"))
|
||||
|
||||
condition := CreateCondition(shedules)
|
||||
|
||||
var lessons []database.Lesson
|
||||
err := bot.DB.
|
||||
Where("end > ?", now.Format("2006-01-02 15:04:05")).
|
||||
And(condition).
|
||||
OrderBy("begin").
|
||||
Limit(16).
|
||||
Find(&lessons)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(lessons) > 0 {
|
||||
return lessons, nil
|
||||
} else if len(isRetry) == 0 || isRetry[0] < 2 {
|
||||
_, week := now.ISOWeek()
|
||||
isRetry, err = bot.LoadShedule(shedules, week, isRetry...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dw := isRetry[0]
|
||||
return bot.GetLessons(shedules, now, dw+1)
|
||||
} else {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (bot *Bot) LoadShedule(shedules []database.ShedulesInUser, week int, isRetry ...int) ([]int, error) {
|
||||
if len(isRetry) == 0 {
|
||||
isRetry = []int{0}
|
||||
}
|
||||
dw := isRetry[0]
|
||||
week -= bot.Week
|
||||
for _, sh := range shedules {
|
||||
doc, err := ssau_parser.ConnectById(sh.SheduleId, sh.IsTeacher, week+dw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
shedule, err := ssau_parser.Parse(doc, !sh.IsTeacher, sh.SheduleId, week+dw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = ssau_parser.UploadShedule(bot.DB, *shedule)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return isRetry, nil
|
||||
}
|
||||
|
||||
func CreateCondition(shedules []database.ShedulesInUser) string {
|
||||
var groups []string
|
||||
var teachers []string
|
||||
|
||||
|
@ -185,46 +237,7 @@ func (bot *Bot) GetLessons(shedules []database.ShedulesInUser, now time.Time, is
|
|||
teachers_str += strings.Join(teachers, ",")
|
||||
condition += "teacherId in (" + teachers_str + ") "
|
||||
}
|
||||
|
||||
var lessons []database.Lesson
|
||||
err := bot.DB.
|
||||
Where("end > ?", now.Format("2006-01-02 15:04:05")).
|
||||
And(condition).
|
||||
OrderBy("begin").
|
||||
Limit(16).
|
||||
Find(&lessons)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(lessons) > 0 {
|
||||
return lessons, nil
|
||||
} else if len(isRetry) == 0 || isRetry[0] < 2 {
|
||||
if len(isRetry) == 0 {
|
||||
isRetry = []int{0}
|
||||
}
|
||||
dw := isRetry[0]
|
||||
_, week := now.ISOWeek()
|
||||
week -= bot.Week
|
||||
for _, sh := range shedules {
|
||||
doc, err := ssau_parser.ConnectById(sh.SheduleId, sh.IsTeacher, week+dw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
shedule, err := ssau_parser.Parse(doc, !sh.IsTeacher, sh.SheduleId, week+dw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = ssau_parser.UploadShedule(bot.DB, *shedule)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return bot.GetLessons(shedules, now, dw+1)
|
||||
} else {
|
||||
return nil, nil
|
||||
}
|
||||
return condition
|
||||
}
|
||||
|
||||
func (bot *Bot) GetDayShedule(lessons [][]database.Lesson) (string, error) {
|
||||
|
|
|
@ -61,9 +61,9 @@ func SummaryKeyboard(clickedButton string, sheduleId int64, isTeacher bool, dt i
|
|||
day := []tgbotapi.InlineKeyboardButton{
|
||||
tgbotapi.NewInlineKeyboardButtonData("День", "day"+tail),
|
||||
}
|
||||
/*week := []tgbotapi.InlineKeyboardButton{
|
||||
week := []tgbotapi.InlineKeyboardButton{
|
||||
tgbotapi.NewInlineKeyboardButtonData("Неделя", "week"+tail),
|
||||
}*/
|
||||
}
|
||||
|
||||
var arrows []tgbotapi.InlineKeyboardButton
|
||||
if clickedButton == "day" || clickedButton == "week" {
|
||||
|
@ -82,7 +82,7 @@ func SummaryKeyboard(clickedButton string, sheduleId int64, isTeacher bool, dt i
|
|||
switch clickedButton {
|
||||
case "day":
|
||||
markup = [][]tgbotapi.InlineKeyboardButton{
|
||||
arrows, near, //week,
|
||||
arrows, near, week,
|
||||
}
|
||||
case "week":
|
||||
markup = [][]tgbotapi.InlineKeyboardButton{
|
||||
|
@ -90,7 +90,7 @@ func SummaryKeyboard(clickedButton string, sheduleId int64, isTeacher bool, dt i
|
|||
}
|
||||
default:
|
||||
markup = [][]tgbotapi.InlineKeyboardButton{
|
||||
day, //week,
|
||||
day, week,
|
||||
}
|
||||
}
|
||||
/*if sheduleId == 0 {
|
||||
|
|
|
@ -0,0 +1,142 @@
|
|||
package tg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"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"
|
||||
"github.com/icza/gox/timex"
|
||||
)
|
||||
|
||||
func (bot *Bot) GetWeekLessons(shedules []database.ShedulesInUser, week int, isRetry ...int) ([]database.Lesson, error) {
|
||||
condition := CreateCondition(shedules)
|
||||
|
||||
var lessons []database.Lesson
|
||||
err := bot.DB.
|
||||
Where("WEEK(`begin`, 1) = ?", week+bot.Week).
|
||||
And(condition).
|
||||
OrderBy("begin").
|
||||
Find(&lessons)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(lessons) > 0 {
|
||||
return lessons, nil
|
||||
} else if len(isRetry) == 0 || isRetry[0] < 2 {
|
||||
isRetry, err = bot.LoadShedule(shedules, week+bot.Week, isRetry...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dw := isRetry[0]
|
||||
return bot.GetWeekLessons(shedules, week, dw+1)
|
||||
} else {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
var days = [6]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
|
||||
|
||||
func (bot *Bot) GetWeekSummary(shedules []database.ShedulesInUser, dw int, isPersonal bool, editMsg ...tgbotapi.Message) {
|
||||
_, week := time.Now().ISOWeek()
|
||||
week += dw
|
||||
lessons, _ := bot.GetWeekLessons(shedules, week-bot.Week)
|
||||
|
||||
var dates []time.Time
|
||||
begins := make(map[time.Time]bool)
|
||||
ends := make(map[time.Time]bool)
|
||||
height := 0
|
||||
for _, lesson := range lessons {
|
||||
t := lesson.Begin
|
||||
begin := time.Date(2000, 1, 1, t.Hour(), t.Minute(), 0, 0, t.Location())
|
||||
begins[begin] = true
|
||||
|
||||
e := lesson.End
|
||||
end := time.Date(2000, 1, 1, e.Hour(), e.Minute(), 0, 0, e.Location())
|
||||
ends[end] = true
|
||||
|
||||
if lesson.NumInShedule > height {
|
||||
height = lesson.NumInShedule
|
||||
}
|
||||
}
|
||||
var times []ssau_parser.Lesson
|
||||
for b := range begins {
|
||||
l := ssau_parser.Lesson{
|
||||
Begin: b,
|
||||
}
|
||||
times = append(times, l)
|
||||
}
|
||||
i := 0
|
||||
for e := range ends {
|
||||
times[i].End = e
|
||||
i++
|
||||
}
|
||||
|
||||
weekBegin := timex.WeekStart(lessons[0].Begin.Year(), week)
|
||||
for i := range days {
|
||||
dates = append(dates, weekBegin.Add(time.Hour*time.Duration(24*i)))
|
||||
}
|
||||
|
||||
shedule := make([][6][]database.Lesson, height+1)
|
||||
pairs := GroupPairs(lessons)
|
||||
|
||||
for _, p := range pairs {
|
||||
day := int(math.Floor(p[0].Begin.Sub(weekBegin).Hours() / 24))
|
||||
shedule[p[0].NumInShedule][day] = p
|
||||
}
|
||||
|
||||
bot.CreateHTMLShedule(week, shedule, dates, times)
|
||||
}
|
||||
|
||||
const head = `<html lang="ru">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Тестовая страница с расписанием</title>
|
||||
<link rel="stylesheet" href="./rasp.css\">
|
||||
<meta name='viewport' content='width=device-width,initial-scale=1'/>
|
||||
<meta name="mobile-web-app-capable" content="yes">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
`
|
||||
const lessonHead = `<div class="subj %s">\n'
|
||||
<div><p></p></div>\n'
|
||||
<h2>%s</h2><hr>\n`
|
||||
|
||||
var weekdays = [6]string{
|
||||
"пн",
|
||||
"вт",
|
||||
"ср",
|
||||
"чт",
|
||||
"пт",
|
||||
"сб",
|
||||
}
|
||||
|
||||
func (bot *Bot) CreateHTMLShedule(week int, shedule [][6][]database.Lesson, dates []time.Time, times []ssau_parser.Lesson) string {
|
||||
html := head
|
||||
html += fmt.Sprintf("<div class=\"note\"><div id=\"week\">%d неделя</div></div>\n", week)
|
||||
html += "<div class=\"rasp\">\n<div class=\"head\">Время</div>\n"
|
||||
|
||||
for i, d := range dates {
|
||||
day := d.Format("02")
|
||||
html += fmt.Sprintf("<div class=\"head\">%s<p>%s</p></div>", weekdays[i], day)
|
||||
}
|
||||
for t, tline := range shedule {
|
||||
begin := times[t].Begin.Format("15:04")
|
||||
end := times[t].End.Format("15:04")
|
||||
html += fmt.Sprintf("<div class=\"time\">%s<hr>%s</div>", begin, end)
|
||||
for _, l := range tline {
|
||||
if len(l) > 0 {
|
||||
html += fmt.Sprintf(lessonHead, l[0].Type, l[0].Name)
|
||||
html += "</div>\n"
|
||||
} else {
|
||||
html += "<div class=\"subj\"></div>\n"
|
||||
}
|
||||
}
|
||||
}
|
||||
html += "</div></body></html>"
|
||||
return html
|
||||
}
|
Reference in New Issue