feat: Basic polling
This commit is contained in:
6
.env.example
Normal file
6
.env.example
Normal file
@ -0,0 +1,6 @@
|
||||
SCHEDULE_BASE_URL=
|
||||
SCHEDULE_FACULTY_ID=
|
||||
SCHEDULE_GROUP_ID=
|
||||
SCHEDULE_START_DATE=2025-1-6
|
||||
SCHEDULE_FLAG_PATH=/tmp/Flagfile
|
||||
SCHEDULE_CRON_PATTERN=*/10 * * * * *
|
||||
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.env
|
||||
!.env.example
|
||||
13
cmd/service/main.go
Normal file
13
cmd/service/main.go
Normal file
@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"git.inkling.su/mrqiz/schedule-poll/internal/config"
|
||||
"git.inkling.su/mrqiz/schedule-poll/internal/schedule"
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c := cron.New(cron.WithSeconds())
|
||||
c.AddFunc(config.AppConfig.CronPattern, schedule.PollSchedule)
|
||||
c.Run()
|
||||
}
|
||||
13
go.mod
Normal file
13
go.mod
Normal file
@ -0,0 +1,13 @@
|
||||
module git.inkling.su/mrqiz/schedule-poll
|
||||
|
||||
go 1.23.3
|
||||
|
||||
require (
|
||||
github.com/go-resty/resty/v2 v2.16.3
|
||||
github.com/robfig/cron/v3 v3.0.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/joho/godotenv v1.5.1 // indirect
|
||||
golang.org/x/net v0.33.0 // indirect
|
||||
)
|
||||
10
go.sum
Normal file
10
go.sum
Normal file
@ -0,0 +1,10 @@
|
||||
github.com/go-resty/resty/v2 v2.16.3 h1:zacNT7lt4b8M/io2Ahj6yPypL7bqx9n1iprfQuodV+E=
|
||||
github.com/go-resty/resty/v2 v2.16.3/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
|
||||
github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
|
||||
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
|
||||
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||
37
internal/config/env.go
Normal file
37
internal/config/env.go
Normal file
@ -0,0 +1,37 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
TelegramBotToken string
|
||||
Schedule struct {
|
||||
BaseUrl string
|
||||
FacultyId string
|
||||
GroupId string
|
||||
StartDate string
|
||||
}
|
||||
FlagfilePath string
|
||||
CronPattern string
|
||||
}
|
||||
|
||||
var AppConfig config
|
||||
|
||||
func init() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
AppConfig.FlagfilePath = os.Getenv("SCHEDULE_FLAG_PATH")
|
||||
AppConfig.Schedule.BaseUrl = os.Getenv("SCHEDULE_BASE_URL")
|
||||
AppConfig.Schedule.FacultyId = os.Getenv("SCHEDULE_FACULTY_ID")
|
||||
AppConfig.Schedule.GroupId = os.Getenv("SCHEDULE_GROUP_ID")
|
||||
AppConfig.Schedule.StartDate = os.Getenv("SCHEDULE_START_DATE")
|
||||
AppConfig.TelegramBotToken = os.Getenv("SCHEDULE_TELEGRAM_TOKEN")
|
||||
AppConfig.CronPattern = os.Getenv("SCHEDULE_CRON_PATTERN")
|
||||
}
|
||||
32
internal/schedule/api.go
Normal file
32
internal/schedule/api.go
Normal file
@ -0,0 +1,32 @@
|
||||
package schedule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
baseUrl string = os.Getenv("SCHEDULE_BASE_URL")
|
||||
)
|
||||
|
||||
func GetWeek(groupId, startDate string) (*Week, error) {
|
||||
client := resty.New()
|
||||
client.SetBaseURL(baseUrl)
|
||||
|
||||
res := Week{}
|
||||
_, err := client.R().
|
||||
SetResult(&res).
|
||||
Get(fmt.Sprintf("/api/v1/ruz/scheduler/%s?date=%s", groupId, startDate))
|
||||
|
||||
return &res, err
|
||||
}
|
||||
|
||||
func GetScheduleUrl(facultyId, groupId, date string) string {
|
||||
return fmt.Sprintf("%s/faculty/%s/groups/%s?date=%s",
|
||||
baseUrl,
|
||||
facultyId,
|
||||
groupId,
|
||||
date)
|
||||
}
|
||||
16
internal/schedule/models.go
Normal file
16
internal/schedule/models.go
Normal file
@ -0,0 +1,16 @@
|
||||
package schedule
|
||||
|
||||
type Lesson struct {
|
||||
Subject string
|
||||
SubjectShort string `json:"subject_short"`
|
||||
}
|
||||
|
||||
type Day struct {
|
||||
Weekday int
|
||||
Date string
|
||||
Lessons []Lesson
|
||||
}
|
||||
|
||||
type Week struct {
|
||||
Days []Day
|
||||
}
|
||||
44
internal/schedule/poll.go
Normal file
44
internal/schedule/poll.go
Normal file
@ -0,0 +1,44 @@
|
||||
package schedule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.inkling.su/mrqiz/schedule-poll/internal/config"
|
||||
)
|
||||
|
||||
func PollSchedule() {
|
||||
flagFilePath := config.AppConfig.FlagfilePath
|
||||
|
||||
if _, fileErr := os.Stat(flagFilePath); fileErr == nil {
|
||||
return
|
||||
}
|
||||
|
||||
facultyId := config.AppConfig.Schedule.FacultyId
|
||||
groupId := config.AppConfig.Schedule.GroupId
|
||||
date := config.AppConfig.Schedule.StartDate
|
||||
|
||||
week, err := GetWeek(groupId, date)
|
||||
|
||||
if err != nil {
|
||||
log.Panicln("Failed to get week data", err)
|
||||
}
|
||||
|
||||
days := len(week.Days)
|
||||
|
||||
if days != 0 {
|
||||
siteUrl := GetScheduleUrl(facultyId, groupId, date)
|
||||
msg := fmt.Sprintf(
|
||||
"❗ Опубликовано расписание на новый семестр!\n\n"+
|
||||
"Появились занятия на неделе с %s\n"+
|
||||
"Доступно дней: %d\n\n"+
|
||||
"%s\n",
|
||||
date,
|
||||
len(week.Days),
|
||||
siteUrl)
|
||||
|
||||
os.WriteFile(flagFilePath, []byte(msg), os.FileMode(0777))
|
||||
log.Printf("Found %d days", len(week.Days))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user