From c792d19a3176a36bda9b8521e053e76f624bfbaa Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 10 Sep 2022 20:14:14 +0200 Subject: [PATCH] Melsungen news page added --- config.sample.yaml | 7 ++- data.db | Bin 8192 -> 8192 bytes go.mod | 0 go.sum | 0 main.go | 13 +++-- matrix-bot/matrix-bot.go | 6 +-- modules/config/config.go | 2 +- modules/database/database.go | 0 pages/melsungen.go | 102 +++++++++++++++++++++++++++++++++++ pages/school.go | 6 ++- readme.md | 0 11 files changed, 125 insertions(+), 11 deletions(-) mode change 100644 => 100755 config.sample.yaml mode change 100644 => 100755 go.mod mode change 100644 => 100755 go.sum mode change 100644 => 100755 main.go mode change 100644 => 100755 matrix-bot/matrix-bot.go mode change 100644 => 100755 modules/config/config.go mode change 100644 => 100755 modules/database/database.go create mode 100644 pages/melsungen.go mode change 100644 => 100755 pages/school.go mode change 100644 => 100755 readme.md diff --git a/config.sample.yaml b/config.sample.yaml old mode 100644 new mode 100755 index fdb8bb8..a3d7dd0 --- a/config.sample.yaml +++ b/config.sample.yaml @@ -2,7 +2,10 @@ 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 + update_time: ["03:00", "15:45"] + roomId: "news_room_id" + - page: "melsungen" + update_time: ["16:00"] + room_id: "news_room_id" \ No newline at end of file diff --git a/data.db b/data.db index 1f43e7484fdeba163f41c3b3ac89bf70492bc7fd..1fa9d0cbe2657d3cdab2799995d4a716453d3df8 100644 GIT binary patch delta 227 zcmX}ly$%6E6a`?5LN~RLxIH$z<0nywzoikCD0F7InAn+RW+*gx1KxoL@P-DVySX{% zeCPJLea$(kTfXO{p0hj2BuO(t9;Er)`Q)@rrVE#rz)0`q3grMfDi)$#lt)Z$3@`x! zN2v{4mB6pKUc;pxrErKP_;r}96@**^^**Ofe`?!^nImusqu=nuL?JO(!L2AYvm`NHN5RlS&%j8}$iT=50F09xR{#J2 diff --git a/go.mod b/go.mod old mode 100644 new mode 100755 diff --git a/go.sum b/go.sum old mode 100644 new mode 100755 diff --git a/main.go b/main.go old mode 100644 new mode 100755 index 3f7d1e0..9c19278 --- a/main.go +++ b/main.go @@ -32,9 +32,16 @@ func main() { 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) + if page.Page == "school" { + if hour == updateTimeSplit[0] && minute == updateTimeSplit[1] { + pages.School() + log.Println("request", page.Page, updateTime, dt) + } + } else if page.Page == "melsungen" { + if hour == updateTimeSplit[0] && minute == updateTimeSplit[1] { + pages.MelsungenNews() + log.Println("request", page.Page, updateTime, dt) + } } } } diff --git a/matrix-bot/matrix-bot.go b/matrix-bot/matrix-bot.go old mode 100644 new mode 100755 index cb555b8..54e18eb --- a/matrix-bot/matrix-bot.go +++ b/matrix-bot/matrix-bot.go @@ -10,7 +10,7 @@ import ( "roese.dev/matrix-development-lauge/modules/config" ) -func SendMessage(header string, content string, url string) { +func SendMessage(roomID string, topic string, header string, content string, url string) { cfg := config.Cfg.Bot client, err := mautrix.NewClient(cfg.HomeserverUrl, "", "") @@ -30,7 +30,7 @@ func SendMessage(header string, content string, url string) { panic(err) } - msg := format.RenderMarkdown(fmt.Sprintf("Thema: Radko-Stöckl-Schule - Aktuelles

%s

%s
%s", header, content, url), false, true) + msg := format.RenderMarkdown(fmt.Sprintf("Thema: %s

%s

%s
%s", topic, header, content, url), false, true) - client.SendMessageEvent(id.RoomID(cfg.RoomId), event.EventMessage, &msg) + client.SendMessageEvent(id.RoomID(roomID), event.EventMessage, &msg) } diff --git a/modules/config/config.go b/modules/config/config.go old mode 100644 new mode 100755 index d4b45a3..4a0ce31 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -17,13 +17,13 @@ type Config struct { type PageItem struct { Page string UpdateTime []string `yaml:"update_time,flow"` + RoomID string `yaml:"room_id"` } type Bot struct { HomeserverUrl string `yaml:"homeserver_url"` User string Password string - RoomId string `yaml:"room_id"` } func LoadConfig() { diff --git a/modules/database/database.go b/modules/database/database.go old mode 100644 new mode 100755 diff --git a/pages/melsungen.go b/pages/melsungen.go new file mode 100644 index 0000000..7df8d05 --- /dev/null +++ b/pages/melsungen.go @@ -0,0 +1,102 @@ +package pages + +import ( + "database/sql" + "fmt" + "log" + "net/http" + "time" + + "github.com/PuerkitoBio/goquery" + matrixbot "roese.dev/matrix-development-lauge/matrix-bot" + "roese.dev/matrix-development-lauge/modules/config" + "roese.dev/matrix-development-lauge/modules/database" +) + +func MelsungenNews() { + log.Println("mels") + res, err := http.Get("https://www.melsungen.de/aktuelles") + + if err != nil { + log.Println("Failed to get response from melsungen news page") + return + } + + defer res.Body.Close() + + if res.StatusCode != 200 { + log.Fatal("status code") + } + + log.Println("ok") + + doc, err := goquery.NewDocumentFromReader(res.Body) + + if err != nil { + log.Fatal(err) + } + + log.Println("Find") + + db := database.GetDatabase() + + defer db.Close() + + var pageTitle string + var firstPostTitle string + + stmt, err := db.Prepare("SELECT page_title FROM pages WHERE page_id = ?") + + if err != nil { + log.Fatal(err) + } + + defer stmt.Close() + + err = stmt.QueryRow("melsungen").Scan(&pageTitle) + + finish := false + + doc.Find(".post-item").Each(func(i int, s *goquery.Selection) { + if !finish { + date := s.Find(".date_label").Text() + postTitle := s.Find(".post-title").Text() + postUrl := s.Find(".post-title a").AttrOr("href", "") + postExcerpt := s.Find(".post-excerpt").Text() + + if firstPostTitle == "" { + firstPostTitle = postTitle + } + + log.Printf("Review %d: %s %s %s\n", i, date, postTitle, postUrl) + + if err == sql.ErrNoRows { + _, err = db.Exec(fmt.Sprintf("INSERT INTO pages(page_id, page_title) values('melsungen', '%s')", firstPostTitle)) + + if err != nil { + log.Fatal(err) + } + + finish = true + } + + if err != nil { + log.Fatal(err) + } + + if pageTitle == postTitle { + _, err = db.Exec(fmt.Sprintf("UPDATE pages SET page_title = '%s' WHERE page_id = 'melsungen'", firstPostTitle)) + + if err != nil { + log.Fatal(err) + } + + finish = true + } else { + matrixbot.SendMessage(config.Cfg.Pages[1].RoomID, "Melsungen - Aktuelles", postTitle, postExcerpt, postUrl) + } + + time.Sleep(2 * time.Second) + } + }) +} diff --git a/pages/school.go b/pages/school.go old mode 100644 new mode 100755 index 37363b5..3f2fdbd --- a/pages/school.go +++ b/pages/school.go @@ -8,6 +8,7 @@ import ( "github.com/PuerkitoBio/goquery" matrixbot "roese.dev/matrix-development-lauge/matrix-bot" + "roese.dev/matrix-development-lauge/modules/config" "roese.dev/matrix-development-lauge/modules/database" ) @@ -15,7 +16,8 @@ func School() { res, err := http.Get("https://radko-stoeckl-schule.de") if err != nil { - log.Fatal(err) + log.Println("Failed to get response from radko stoeckl schule page") + return } defer res.Body.Close() @@ -83,6 +85,6 @@ func School() { log.Println("header", latest_page_title, "text", text, "url", url) - matrixbot.SendMessage(latest_page_title, text, "https://www.radko-stoeckl-schule.de"+url) + matrixbot.SendMessage(config.Cfg.Pages[0].RoomID, "Radko-Stöckl-Schule - Aktuelles", latest_page_title, text, "https://www.radko-stoeckl-schule.de"+url) } } diff --git a/readme.md b/readme.md old mode 100644 new mode 100755