Melsungen news page added

master
alex 2022-09-10 20:14:14 +02:00
parent f247d094e2
commit c792d19a31
11 changed files with 125 additions and 11 deletions

5
config.sample.yaml Normal file → Executable file
View File

@ -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"]
roomId: "news_room_id"
- page: "melsungen"
update_time: ["16:00"]
room_id: "news_room_id"

BIN
data.db

Binary file not shown.

0
go.mod Normal file → Executable file
View File

0
go.sum Normal file → Executable file
View File

7
main.go Normal file → Executable file
View File

@ -32,10 +32,17 @@ func main() {
for _, updateTime := range page.UpdateTime {
updateTimeSplit := strings.Split(updateTime, ":")
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)
}
}
}
}
}

6
matrix-bot/matrix-bot.go Normal file → Executable file
View File

@ -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 <h4>%s</h4>%s<br>%s", header, content, url), false, true)
msg := format.RenderMarkdown(fmt.Sprintf("Thema: %s <h4>%s</h4>%s<br>%s", topic, header, content, url), false, true)
client.SendMessageEvent(id.RoomID(cfg.RoomId), event.EventMessage, &msg)
client.SendMessageEvent(id.RoomID(roomID), event.EventMessage, &msg)
}

2
modules/config/config.go Normal file → Executable file
View File

@ -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() {

0
modules/database/database.go Normal file → Executable file
View File

102
pages/melsungen.go Normal file
View File

@ -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)
}
})
}

6
pages/school.go Normal file → Executable file
View File

@ -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)
}
}

0
readme.md Normal file → Executable file
View File