commit 57f10b4f2308e9535e97f52da3dc8d17341751ed Author: Alex Date: Tue Feb 22 22:14:11 2022 +0100 init project diff --git a/config.sample.yaml b/config.sample.yaml new file mode 100644 index 0000000..fdb8bb8 --- /dev/null +++ b/config.sample.yaml @@ -0,0 +1,8 @@ +bot: + homeserver_url: "your_homeserver_url" + user: "your_bot_user" + password: "your_bot_password" + roomId: "news_room_id" +pages: + - page: "school" + update_time: ["03:00", "15:45"] \ No newline at end of file diff --git a/data.db b/data.db new file mode 100644 index 0000000..1f43e74 Binary files /dev/null and b/data.db differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8daf75c --- /dev/null +++ b/go.mod @@ -0,0 +1,16 @@ +module roese.dev/matrix-development-lauge + +go 1.17 + +require ( + github.com/PuerkitoBio/goquery v1.8.0 // indirect + github.com/anaskhan96/soup v1.2.5 // indirect + github.com/andybalholm/cascadia v1.3.1 // indirect + github.com/mattn/go-sqlite3 v1.14.11 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect + golang.org/x/crypto v0.0.0-20220213190939-1e6e3497d506 // indirect + golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect + golang.org/x/text v0.3.7 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + maunium.net/go/mautrix v0.10.11 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..59435fd --- /dev/null +++ b/go.sum @@ -0,0 +1,49 @@ +github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U= +github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI= +github.com/anaskhan96/soup v1.2.5 h1:V/FHiusdTrPrdF4iA1YkVxsOpdNcgvqT1hG+YtcZ5hM= +github.com/anaskhan96/soup v1.2.5/go.mod h1:6YnEp9A2yywlYdM4EgDz9NEHclocMepEtku7wg6Cq3s= +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/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/mattn/go-sqlite3 v1.14.11 h1:gt+cp9c0XGqe9S/wAHTL3n/7MqY+siPWgWJgqdsFrzQ= +github.com/mattn/go-sqlite3 v1.14.11/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.14.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/sjson v1.2.4/go.mod h1:098SZ494YoMWPmMO6ct4dcFnqxwj9r/gF0Etp19pSNM= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20220213190939-1e6e3497d506 h1:EuGTJDfeg/PGZJp3gq1K+14eSLFTsrj1eg8KQuiUyKg= +golang.org/x/crypto v0.0.0-20220213190939-1e6e3497d506/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk= +golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +maunium.net/go/maulogger/v2 v2.3.2/go.mod h1:TYWy7wKwz/tIXTpsx8G3mZseIRiC5DoMxSZazOHy68A= +maunium.net/go/mautrix v0.10.11 h1:u3D5+Ko7Pk0ruVFUAgjfk5E6U5Ys9VVObEGrytr0Hk4= +maunium.net/go/mautrix v0.10.11/go.mod h1:Ynac6y32yvdJC8YiYvWjWp6u1WjVTNq+JssC+07ZZWw= diff --git a/main.go b/main.go new file mode 100644 index 0000000..3f7d1e0 --- /dev/null +++ b/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "log" + "strings" + "time" + + _ "github.com/mattn/go-sqlite3" + "roese.dev/matrix-development-lauge/modules/config" + "roese.dev/matrix-development-lauge/modules/database" + "roese.dev/matrix-development-lauge/pages" +) + +func init() { + config.LoadConfig() + + database.InitDatabase() +} + +func main() { + ticker := time.NewTicker(time.Minute) + + cfg := config.Cfg + + for _ = range ticker.C { + dt := time.Now() + + hour := dt.Format("15") + minute := dt.Format("04") + + for _, page := range cfg.Pages { + for _, updateTime := range page.UpdateTime { + updateTimeSplit := strings.Split(updateTime, ":") + + if hour == updateTimeSplit[0] && minute == updateTimeSplit[1] { + pages.School() + log.Println("request", page.Page, updateTime, dt) + } + } + } + } +} diff --git a/matrix-bot/matrix-bot.go b/matrix-bot/matrix-bot.go new file mode 100644 index 0000000..6e95cfb --- /dev/null +++ b/matrix-bot/matrix-bot.go @@ -0,0 +1,34 @@ +package matrixbot + +import ( + "maunium.net/go/mautrix" + "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/format" + "maunium.net/go/mautrix/id" + "roese.dev/matrix-development-lauge/modules/config" +) + +func SendMessage(msg string) { + cfg := config.Cfg.Bot + + client, err := mautrix.NewClient(cfg.HomeserverUrl, "", "") + + if err != nil { + panic(err) + } + + _, err = client.Login(&mautrix.ReqLogin{ + Type: "m.login.password", + Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: cfg.User}, + Password: cfg.Password, + StoreCredentials: true, + }) + + if err != nil { + panic(err) + } + + content := format.RenderMarkdown(msg, false, true) + + client.SendMessageEvent(id.RoomID(cfg.RoomId), event.EventMessage, &content) +} diff --git a/modules/config/config.go b/modules/config/config.go new file mode 100644 index 0000000..d4b45a3 --- /dev/null +++ b/modules/config/config.go @@ -0,0 +1,41 @@ +package config + +import ( + "io/ioutil" + "log" + + "gopkg.in/yaml.v2" +) + +var Cfg Config + +type Config struct { + Pages []PageItem `yaml:"pages,flow"` + Bot Bot +} + +type PageItem struct { + Page string + UpdateTime []string `yaml:"update_time,flow"` +} + +type Bot struct { + HomeserverUrl string `yaml:"homeserver_url"` + User string + Password string + RoomId string `yaml:"room_id"` +} + +func LoadConfig() { + file, err := ioutil.ReadFile("./config.yaml") + + if err != nil { + log.Fatalln("failed to load yaml file", err) + } + + err = yaml.Unmarshal(file, &Cfg) + + if err != nil { + log.Fatalln("failed to unmarshal config", err) + } +} diff --git a/modules/database/database.go b/modules/database/database.go new file mode 100644 index 0000000..c496f10 --- /dev/null +++ b/modules/database/database.go @@ -0,0 +1,31 @@ +package database + +import ( + "database/sql" + "log" +) + +func InitDatabase() { + db := GetDatabase() + + defer db.Close() + + sqlStmt := `CREATE TABLE IF NOT EXISTS pages (page_id VARCHAR(255), page_title TEXT);` + + _, err := db.Exec(sqlStmt) + + if err != nil { + log.Printf("%q: %s\n", err, sqlStmt) + return + } +} + +func GetDatabase() *sql.DB { + db, err := sql.Open("sqlite3", "./data.db") + + if err != nil { + log.Fatal(err) + } + + return db +} diff --git a/pages/school.go b/pages/school.go new file mode 100644 index 0000000..a47df02 --- /dev/null +++ b/pages/school.go @@ -0,0 +1,84 @@ +package pages + +import ( + "database/sql" + "fmt" + "log" + "net/http" + + "github.com/PuerkitoBio/goquery" + matrixbot "roese.dev/matrix-development-lauge/matrix-bot" + "roese.dev/matrix-development-lauge/modules/database" +) + +func School() { + res, err := http.Get("https://radko-stoeckl-schule.de") + + if err != nil { + log.Fatal(err) + } + + defer res.Body.Close() + + if res.StatusCode != 200 { + log.Fatal("status code") + } + + doc, err := goquery.NewDocumentFromReader(res.Body) + + if err != nil { + log.Fatal(err) + } + + firstListItem := doc.Find(".news-teaser ul li:first-of-type") + + latest_page_title := firstListItem.Find("a h4").Text() + + db := database.GetDatabase() + + defer db.Close() + + stmt, err := db.Prepare("SELECT page_title FROM pages WHERE page_id = ?") + + if err != nil { + log.Fatal(err) + } + + defer stmt.Close() + + var pageTitle string + + send := false + + err = stmt.QueryRow("school").Scan(&pageTitle) + + if err == sql.ErrNoRows { + _, err = db.Exec(fmt.Sprintf("INSERT INTO pages(page_id, page_title) values('school', '%s')", latest_page_title)) + + if err != nil { + log.Fatal(err) + } + + send = true + } + + if err != nil { + log.Fatal(err) + } + + if pageTitle != latest_page_title { + _, err = db.Exec(fmt.Sprintf("UPDATE pages SET page_title = '%s' WHERE page_id = 'school'", latest_page_title)) + + if err != nil { + log.Fatal(err) + } + + send = true + } + + if send { + text := firstListItem.Find("a .news-summary").Text() + + matrixbot.SendMessage(text) + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..e92a2a9 --- /dev/null +++ b/readme.md @@ -0,0 +1 @@ +## This is a bot that automatically sends requests to certain websites and sends a message when a change has been made. \ No newline at end of file