diff --git a/go.mod b/go.mod index 1674701..edc9b6e 100644 --- a/go.mod +++ b/go.mod @@ -37,6 +37,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml v1.9.5 // indirect github.com/pelletier/go-toml/v2 v2.0.2 // indirect + github.com/robfig/cron/v3 v3.0.0 // indirect github.com/spf13/afero v1.8.2 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/go.sum b/go.sum index 3cec9f9..a003cc8 100644 --- a/go.sum +++ b/go.sum @@ -200,6 +200,8 @@ github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qR github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +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= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= diff --git a/main.go b/main.go index 55dbec1..a621ac6 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "github.com/robfig/cron/v3" "github.com/spf13/viper" "gorm.io/driver/sqlite" "gorm.io/gorm" @@ -217,5 +218,14 @@ func main() { router.GET("/cache/view", web.cacheView) router.POST("/cache/clear", web.cacheClear) + var cronFilters []Filter + db.Model(&Filter{}).Find(&cronFilters, "type = 'cron'") + c := cron.New() + for _, cronFilter := range cronFilters { + c.AddFunc(cronFilter.Var1, func() { triggerSchedule(cronFilter.WatchID, db) }) + } + c.Start() + router.Run("0.0.0.0:8080") + } diff --git a/models.go b/models.go index 9dc38d7..7a126fe 100644 --- a/models.go +++ b/models.go @@ -18,7 +18,7 @@ type Filter struct { Name string `form:"filter_name" yaml:"filter_name" json:"filter_name" binding:"required" validate:"min=1"` X int `form:"x" yaml:"x" json:"x" validate:"default=0"` Y int `form:"y" yaml:"y" json:"y" validate:"default=0"` - Type string `form:"filter_type" yaml:"filter_type" json:"filter_type" binding:"required" validate:"oneof=url xpath json css replace match substring math store"` + Type string `form:"filter_type" yaml:"filter_type" json:"filter_type" binding:"required" validate:"oneof=url xpath json css replace match substring math store condition cron"` Var1 string `form:"var1" yaml:"var1" json:"var1" binding:"required"` Var2 *string `form:"var2" yaml:"var2" json:"var2"` Var3 *string `form:"var3" yaml:"var3" json:"var3"` diff --git a/scraping.go b/scraping.go index 5563203..cc80a2d 100644 --- a/scraping.go +++ b/scraping.go @@ -28,7 +28,7 @@ func processFilters(filters []Filter, db *gorm.DB, urlCache map[string]string, u filters = filters[1:] var allParentsProcessed = true for _, parent := range filter.Parents { - if _, contains := processedMap[parent.ID]; !contains { + if _, contains := processedMap[parent.ID]; !contains && parent.Type != "cron" { allParentsProcessed = false break } @@ -117,6 +117,10 @@ func getFilterResult(filters []Filter, filter *Filter, db *gorm.DB, urlCache map { notifyFilter(filters, filter, db) } + case filter.Type == "cron": + { + + } case filter.Type == "condition": { switch filter.Var1 { @@ -741,7 +745,6 @@ func notifyFilter(filters []Filter, filter *Filter, db *gorm.DB) { } if !haveResults { filter.log("No output from previous filter(s), need at least 1 to 'trigger'") - log.Println("test") return } tmpl, err := template.New("notify").Parse(filter.Var1) @@ -766,3 +769,17 @@ func notifyFilter(filters []Filter, filter *Filter, db *gorm.DB) { log.Print(buffer.String()) } + +func triggerSchedule(watchID uint, db *gorm.DB) { + var watch Watch + db.Model(&Watch{}).First(&watch, watchID) + + var filters []Filter + db.Model(&Filter{}).Where("watch_id = ?", watch.ID).Find(&filters) + + var connections []FilterConnection + db.Model(&FilterConnection{}).Where("watch_id = ?", watch.ID).Find(&connections) + + buildFilterTree(filters, connections) + processFilters(filters, db, make(map[string]string, 0), true, true) +} diff --git a/static/edit.js b/static/edit.js index 52342b8..3da1dee 100644 --- a/static/edit.js +++ b/static/edit.js @@ -489,6 +489,33 @@ function onTypeChange(node) { var3Div.appendChild(var3Input); break; } + case "cron": { + var var1Input = document.createElement("input"); + var1Input.name = "var1"; + var1Input.id = "var1Input"; + var1Input.value = var1Value; + var1Input.classList.add("form-control"); + var1Label.innerHTML = "CRON"; + var1Input.placeholder = "30 3-6,20-23 * * *"; + var1Div.appendChild(var1Input); + var var2Input = document.createElement("input"); + var2Input.name = "var2"; + var2Input.id = "var2Input"; + var2Input.value = var2Value; + var2Input.classList.add("form-control"); + var2Input.disabled = true; + var2Label.innerHTML = "-"; + var2Div.appendChild(var2Input); + var var3Input = document.createElement("input"); + var3Input.name = "var3"; + var3Input.id = "var3Input"; + var3Input.value = var3Value; + var3Input.classList.add("form-control"); + var3Input.disabled = true; + var3Label.innerHTML = "-"; + var3Div.appendChild(var3Input); + break; + } } } function onMathChange(node) { diff --git a/static/edit.ts b/static/edit.ts index 3de0a3f..3bbf26d 100644 --- a/static/edit.ts +++ b/static/edit.ts @@ -475,6 +475,35 @@ function onTypeChange(node: DiagramNode | null = null){ var2Label.innerHTML = "-"; var2Div.appendChild(var2Input); + let var3Input = document.createElement("input"); + var3Input.name = "var3"; + var3Input.id = "var3Input"; + var3Input.value = var3Value; + var3Input.classList.add("form-control"); + var3Input.disabled = true; + var3Label.innerHTML = "-"; + var3Div.appendChild(var3Input); + break; + } + case "cron":{ + let var1Input = document.createElement("input"); + var1Input.name = "var1"; + var1Input.id = "var1Input"; + var1Input.value = var1Value; + var1Input.classList.add("form-control") + var1Label.innerHTML = "CRON"; + var1Input.placeholder = "30 3-6,20-23 * * *"; + var1Div.appendChild(var1Input); + + let var2Input = document.createElement("input"); + var2Input.name = "var2"; + var2Input.id = "var2Input"; + var2Input.value = var2Value; + var2Input.classList.add("form-control") + var2Input.disabled = true; + var2Label.innerHTML = "-"; + var2Div.appendChild(var2Input); + let var3Input = document.createElement("input"); var3Input.name = "var3"; var3Input.id = "var3Input"; diff --git a/templates/watch/view.html b/templates/watch/view.html index 8eac48b..c19bbdc 100644 --- a/templates/watch/view.html +++ b/templates/watch/view.html @@ -68,7 +68,8 @@ - + ` +