add all_lessons
This commit is contained in:
parent
78705f95ea
commit
6325033fdf
|
@ -4,6 +4,7 @@ type Command string
|
||||||
|
|
||||||
//today_lessons - пары сегодня
|
//today_lessons - пары сегодня
|
||||||
//tomorrow_lessons - пары завтра
|
//tomorrow_lessons - пары завтра
|
||||||
|
//all_lessons - пары на несколько дней
|
||||||
//weather - погода у универа
|
//weather - погода у универа
|
||||||
//new_year - дней до нового года
|
//new_year - дней до нового года
|
||||||
//ping - проверка связи
|
//ping - проверка связи
|
||||||
|
@ -14,6 +15,7 @@ const (
|
||||||
Ping = Command("/ping")
|
Ping = Command("/ping")
|
||||||
TodayLessons = Command("/today_lessons")
|
TodayLessons = Command("/today_lessons")
|
||||||
TomorrowLessons = Command("/tomorrow_lessons")
|
TomorrowLessons = Command("/tomorrow_lessons")
|
||||||
|
AllLessons = Command("/all_lessons")
|
||||||
Weather = Command("/weather")
|
Weather = Command("/weather")
|
||||||
NewYear = Command("/new_year")
|
NewYear = Command("/new_year")
|
||||||
)
|
)
|
||||||
|
|
|
@ -2,10 +2,10 @@ package date
|
||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
func Today() int {
|
func Today(offset time.Duration) string {
|
||||||
location, err := time.LoadLocation("Asia/Novosibirsk")
|
location, err := time.LoadLocation("Asia/Novosibirsk")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
return int(time.Now().In(location).Weekday())
|
return time.Now().In(location).Add(offset).Format("2006-01-02")
|
||||||
}
|
}
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -5,6 +5,7 @@ go 1.17
|
||||||
require (
|
require (
|
||||||
github.com/3crabs/go-yandex-weather-api v0.6.0
|
github.com/3crabs/go-yandex-weather-api v0.6.0
|
||||||
github.com/PuerkitoBio/goquery v1.7.1
|
github.com/PuerkitoBio/goquery v1.7.1
|
||||||
|
github.com/erizocosmico/go-ics v0.0.0-20180527181030-697b9000b86f
|
||||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
|
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
|
||||||
github.com/umputun/go-flags v1.5.1
|
github.com/umputun/go-flags v1.5.1
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -5,6 +5,8 @@ github.com/PuerkitoBio/goquery v1.7.1/go.mod h1:XY0pP4kfraEmmV1O7Uf6XyjoslwsneBb
|
||||||
github.com/andybalholm/cascadia v1.2.0/go.mod h1:YCyR8vOZT9aZ1CHEd8ap0gMVm2aFgxBp0T0eFw1RUQY=
|
github.com/andybalholm/cascadia v1.2.0/go.mod h1:YCyR8vOZT9aZ1CHEd8ap0gMVm2aFgxBp0T0eFw1RUQY=
|
||||||
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
|
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
|
||||||
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
|
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
|
||||||
|
github.com/erizocosmico/go-ics v0.0.0-20180527181030-697b9000b86f h1:IUdUh6y8VDhn5iO/ggBCVb9H0vOI3MDlhDSDCrrSC8Q=
|
||||||
|
github.com/erizocosmico/go-ics v0.0.0-20180527181030-697b9000b86f/go.mod h1:VFnDugGvBJREsor/aiVcXhvAbyjatl0AKXEaGV9u63I=
|
||||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
|
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
|
||||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
|
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
|
||||||
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
|
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
|
||||||
|
|
21
main.go
21
main.go
|
@ -189,6 +189,9 @@ func run() {
|
||||||
case commands.TomorrowLessons:
|
case commands.TomorrowLessons:
|
||||||
sendTomorrowLessonsToChat(bot, chatID, "Завтра, "+update.Message.From.FirstName+", эти пары:")
|
sendTomorrowLessonsToChat(bot, chatID, "Завтра, "+update.Message.From.FirstName+", эти пары:")
|
||||||
|
|
||||||
|
case commands.AllLessons:
|
||||||
|
sendAllLessonsToChat(bot, chatID, "Пока знаю про эти пары:")
|
||||||
|
|
||||||
case commands.Weather:
|
case commands.Weather:
|
||||||
w, err := weather.GetWeatherWithCache(opts.Key, 53.346853, 83.777012, time.Hour)
|
w, err := weather.GetWeatherWithCache(opts.Key, 53.346853, 83.777012, time.Hour)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -215,7 +218,7 @@ func sendTodayLessonsToChat(bot *tgbot.BotAPI, chatID int64, prefix string) {
|
||||||
tgbot.NewMessage(
|
tgbot.NewMessage(
|
||||||
chatID,
|
chatID,
|
||||||
messages.LessonsMessage(
|
messages.LessonsMessage(
|
||||||
parser.ParseByDay(date.Today()),
|
parser.ParseByDay(date.Today(0)),
|
||||||
prefix,
|
prefix,
|
||||||
"Сегодня пар нет",
|
"Сегодня пар нет",
|
||||||
),
|
),
|
||||||
|
@ -229,7 +232,7 @@ func sendTomorrowLessonsToChat(bot *tgbot.BotAPI, chatID int64, prefix string) {
|
||||||
tgbot.NewMessage(
|
tgbot.NewMessage(
|
||||||
chatID,
|
chatID,
|
||||||
messages.LessonsMessage(
|
messages.LessonsMessage(
|
||||||
parser.ParseByDay(date.Today()+1),
|
parser.ParseByDay(date.Today(24*time.Hour)),
|
||||||
prefix,
|
prefix,
|
||||||
"Завтра пар нет",
|
"Завтра пар нет",
|
||||||
),
|
),
|
||||||
|
@ -237,6 +240,20 @@ func sendTomorrowLessonsToChat(bot *tgbot.BotAPI, chatID int64, prefix string) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sendAllLessonsToChat(bot *tgbot.BotAPI, chatID int64, prefix string) {
|
||||||
|
send(
|
||||||
|
bot,
|
||||||
|
tgbot.NewMessage(
|
||||||
|
chatID,
|
||||||
|
messages.LessonsMessage(
|
||||||
|
parser.ParseByDay(""),
|
||||||
|
prefix,
|
||||||
|
"Пусто",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func sendNewYearToChat(bot *tgbot.BotAPI, chatID int64, imageService new_year_service.NewYearService) {
|
func sendNewYearToChat(bot *tgbot.BotAPI, chatID int64, imageService new_year_service.NewYearService) {
|
||||||
url := imageService.GetRandomImageURL()
|
url := imageService.GetRandomImageURL()
|
||||||
message := imageService.GetRandomMessage()
|
message := imageService.GetRandomMessage()
|
||||||
|
|
|
@ -29,7 +29,7 @@ func ThanksMessage() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func HelpMessage(chatID int64) string {
|
func HelpMessage(chatID int64) string {
|
||||||
return "Вот чем я могу вам помочь (v1.3.0), отправь:\n\n" +
|
return "Вот чем я могу вам помочь (v1.4.0), отправь:\n\n" +
|
||||||
"- /ping отобью pong\n" +
|
"- /ping отобью pong\n" +
|
||||||
"- /today_lessons покажу расписание на сегодня\n" +
|
"- /today_lessons покажу расписание на сегодня\n" +
|
||||||
"- /tomorrow_lessons покажу расписание на завтра\n" +
|
"- /tomorrow_lessons покажу расписание на завтра\n" +
|
||||||
|
@ -52,11 +52,22 @@ func LessonsMessage(schedule []parser.Lesson, prefix, emptyText string) string {
|
||||||
if prefix != "" {
|
if prefix != "" {
|
||||||
s += prefix + "\n\n"
|
s += prefix + "\n\n"
|
||||||
}
|
}
|
||||||
|
endDate := ""
|
||||||
for _, l := range schedule {
|
for _, l := range schedule {
|
||||||
|
if endDate != l.TimeStart.Format("01.02") {
|
||||||
|
if endDate != "" {
|
||||||
|
s += "\n"
|
||||||
|
}
|
||||||
|
endDate = l.TimeStart.Format("01.02")
|
||||||
|
s += endDate + "\n"
|
||||||
|
}
|
||||||
if strings.Contains(l.User, "Ярных В.В.") {
|
if strings.Contains(l.User, "Ярных В.В.") {
|
||||||
s += "⭐️ "
|
s += "⭐️ "
|
||||||
}
|
}
|
||||||
s += fmt.Sprintf("%s %s (%s)\n", l.Time, l.Name, l.Place)
|
l.Name = strings.ReplaceAll(l.Name, l.Place, "")
|
||||||
|
l.Name = strings.ReplaceAll(l.Name, "()", "")
|
||||||
|
l.Name = strings.TrimSpace(l.Name)
|
||||||
|
s += fmt.Sprintf("%s %s (%s)\n", l.TimeMsg, l.Name, l.Place)
|
||||||
}
|
}
|
||||||
if prefix != "" {
|
if prefix != "" {
|
||||||
s += "\n" +
|
s += "\n" +
|
||||||
|
@ -92,7 +103,7 @@ func WeatherMessage(w weather.Weather) string {
|
||||||
|
|
||||||
func NewYearMessage(message new_year_service.NewYearMessage) string {
|
func NewYearMessage(message new_year_service.NewYearMessage) string {
|
||||||
loc := time.FixedZone("UTC+7", +7*60*60)
|
loc := time.FixedZone("UTC+7", +7*60*60)
|
||||||
ny := time.Date(2023, 1, 1, 0, 0, 0, 0, loc)
|
ny := time.Date(2024, 1, 1, 0, 0, 0, 0, loc)
|
||||||
now := time.Now().In(loc)
|
now := time.Now().In(loc)
|
||||||
days := ny.Sub(now).Hours() / 24
|
days := ny.Sub(now).Hours() / 24
|
||||||
if message.Header != "" {
|
if message.Header != "" {
|
||||||
|
|
|
@ -4,9 +4,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"student_bot/date"
|
"student_bot/date"
|
||||||
"student_bot/parser"
|
"student_bot/parser"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
lessons := parser.ParseByDay(date.Today())
|
lessons := parser.ParseByDay(date.Today(-48*time.Hour))
|
||||||
fmt.Println(lessons)
|
fmt.Println(lessons)
|
||||||
}
|
}
|
||||||
|
|
120
parser/parser.go
120
parser/parser.go
|
@ -1,126 +1,54 @@
|
||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/erizocosmico/go-ics"
|
||||||
)
|
)
|
||||||
|
|
||||||
const scheduleURLTemplate = "https://www.asu.ru/timetable/students/12/2129440415/?date=%s-%s"
|
|
||||||
|
|
||||||
type Lesson struct {
|
type Lesson struct {
|
||||||
Day int
|
Number string
|
||||||
Number string
|
TimeStart time.Time
|
||||||
Time string
|
TimeMsg string
|
||||||
Name string
|
Name string
|
||||||
User string
|
User string
|
||||||
Place string
|
Place string
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseByDay(day int) []Lesson {
|
func ParseByDay(day string) []Lesson {
|
||||||
return selectLessonsByDay(parse(), day)
|
return selectLessonsByDay(parse(), day)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parse() []Lesson {
|
func parse() []Lesson {
|
||||||
location, err := time.LoadLocation("Asia/Novosibirsk")
|
link := "https://www.asu.ru/timetable/students/12/2129440415/?file=2129440415.ics"
|
||||||
|
calendar, err := ics.ParseCalendar(link, 0, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
now := time.Now().In(location)
|
|
||||||
url := fmt.Sprintf(scheduleURLTemplate, now.Format("20060102"), now.Add(24*time.Hour).Format("20060102"))
|
|
||||||
|
|
||||||
// Get html
|
|
||||||
res, err := http.Get(url)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
defer func(Body io.ReadCloser) {
|
|
||||||
_ = Body.Close()
|
|
||||||
}(res.Body)
|
|
||||||
if res.StatusCode != 200 {
|
|
||||||
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load the HTML document
|
|
||||||
doc, err := goquery.NewDocumentFromReader(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find the review items
|
|
||||||
k := 0
|
|
||||||
var lessons []Lesson
|
var lessons []Lesson
|
||||||
var l Lesson
|
for _, event := range calendar.Events {
|
||||||
doc.Find(".schedule_table tr td").Each(func(i int, s *goquery.Selection) {
|
l := Lesson{
|
||||||
text := strings.TrimSpace(s.Text())
|
Number: "",
|
||||||
if strings.Contains(text, "Понедельник") ||
|
TimeStart: event.Start,
|
||||||
strings.Contains(text, "Вторник") ||
|
TimeMsg: event.Start.Format("15:04") + " - " + event.End.Format("15:04"),
|
||||||
strings.Contains(text, "Среда") ||
|
Name: event.Summary,
|
||||||
strings.Contains(text, "Четверг") ||
|
User: event.Description,
|
||||||
strings.Contains(text, "Пятница") ||
|
Place: event.Location,
|
||||||
strings.Contains(text, "Суббота") {
|
|
||||||
k = 0
|
|
||||||
l = Lesson{}
|
|
||||||
if strings.Contains(text, "Понедельник") {
|
|
||||||
l.Day = 1
|
|
||||||
}
|
|
||||||
if strings.Contains(text, "Вторник") {
|
|
||||||
l.Day = 2
|
|
||||||
}
|
|
||||||
if strings.Contains(text, "Среда") {
|
|
||||||
l.Day = 3
|
|
||||||
}
|
|
||||||
if strings.Contains(text, "Четверг") {
|
|
||||||
l.Day = 4
|
|
||||||
}
|
|
||||||
if strings.Contains(text, "Пятница") {
|
|
||||||
l.Day = 5
|
|
||||||
}
|
|
||||||
if strings.Contains(text, "Суббота") {
|
|
||||||
l.Day = 6
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
n := (k - 1) % 6
|
if l.Place == "" {
|
||||||
if n == 0 {
|
l.Place = "не приходи"
|
||||||
l.Number = trim(text)
|
|
||||||
}
|
}
|
||||||
if n == 1 {
|
lessons = append(lessons, l)
|
||||||
l.Time = trim(text)
|
}
|
||||||
}
|
|
||||||
if n == 2 {
|
|
||||||
l.Name = trim(text)
|
|
||||||
}
|
|
||||||
if n == 3 {
|
|
||||||
l.User = trim(text)
|
|
||||||
}
|
|
||||||
if n == 4 {
|
|
||||||
l.Place = trim(text)
|
|
||||||
if l.Place == "" {
|
|
||||||
l.Place = "не приходи"
|
|
||||||
}
|
|
||||||
lessons = append(lessons, l)
|
|
||||||
}
|
|
||||||
k++
|
|
||||||
})
|
|
||||||
return lessons
|
return lessons
|
||||||
}
|
}
|
||||||
|
|
||||||
func trim(s string) string {
|
func selectLessonsByDay(schedule []Lesson, day string) []Lesson {
|
||||||
s = strings.Replace(s, "пр.з.", "пр.", -1)
|
|
||||||
s = strings.Replace(s, " ", "", -1)
|
|
||||||
s = strings.Replace(s, "\n", " ", -1)
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func selectLessonsByDay(schedule []Lesson, day int) []Lesson {
|
|
||||||
var todayLessons []Lesson
|
var todayLessons []Lesson
|
||||||
for _, l := range schedule {
|
for _, l := range schedule {
|
||||||
if l.Day == day {
|
if day == "" || strings.Contains(l.TimeStart.String(), day) {
|
||||||
todayLessons = append(todayLessons, l)
|
todayLessons = append(todayLessons, l)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue