feat: Basic polling
This commit is contained in:
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