Skip to content

Commit

Permalink
Fix breaking news structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Brawl345 committed Apr 29, 2023
1 parent f462ae6 commit b25678b
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions handler/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
const ApiUrl string = "https://www.tagesschau.de/json/headerapp"

type TagesschauResponse struct {
BreakingNews struct {
BreakingNews []struct {
Id string `json:"id"`
Headline string `json:"headline"`
Text string `json:"text"`
Expand Down Expand Up @@ -60,14 +60,16 @@ func (h Handler) check() error {
return fmt.Errorf("can not unmarshal JSON: %w", err)
}

if result.BreakingNews.Id == "" {
if len(result.BreakingNews) == 0 || result.BreakingNews[0].Id == "" {
if isDebugMode() {
log.Println("No breaking news found")
}
return nil
}

if result.BreakingNews.Url == "" {
breakingNews := result.BreakingNews[0]

if breakingNews.Url == "" {
return errors.New("invalid breaking news")
}

Expand All @@ -76,7 +78,7 @@ func (h Handler) check() error {
return fmt.Errorf("error while getting last entry: %w", err)
}

if lastEntry == result.BreakingNews.Id {
if lastEntry == breakingNews.Id {
if isDebugMode() {
log.Println("Already notified of this breaking news")
}
Expand All @@ -87,15 +89,20 @@ func (h Handler) check() error {

sb := strings.Builder{}

sb.WriteString(fmt.Sprintf("<b>%s</b>\n", html.EscapeString(result.BreakingNews.Headline)))
sb.WriteString(fmt.Sprintf("<i>%s</i>\n", html.EscapeString(result.BreakingNews.Date)))
if result.BreakingNews.Text != "" {
sb.WriteString(fmt.Sprintf("%s\n", html.EscapeString(strings.TrimSpace(result.BreakingNews.Text))))
sb.WriteString(fmt.Sprintf("<b>%s</b>\n", html.EscapeString(breakingNews.Headline)))
sb.WriteString(fmt.Sprintf("<i>%s</i>\n", html.EscapeString(strings.Replace(breakingNews.Date, "Stand: ", "", 1))))
if breakingNews.Text != "" {
sb.WriteString(fmt.Sprintf("%s\n", html.EscapeString(strings.TrimSpace(breakingNews.Text))))
}

url := breakingNews.Url
if !strings.HasPrefix(url, "http") {
url = "https://www.tagesschau.de/" + url
}

textLink := fmt.Sprintf("<a href=\"%s\">Eilmeldung aufrufen</a>", result.BreakingNews.Url)
textLink := fmt.Sprintf("<a href=\"%s\">Eilmeldung aufrufen</a>", url)
replyMarkup := h.Bot.NewMarkup()
btn := replyMarkup.URL("Eilmeldung aufrufen", result.BreakingNews.Url)
btn := replyMarkup.URL("Eilmeldung aufrufen", url)
replyMarkup.Inline(replyMarkup.Row(btn))

groupText := "#EIL: " + sb.String()
Expand All @@ -122,7 +129,7 @@ func (h Handler) check() error {
}
}

err = h.DB.System.SetLastEntry(result.BreakingNews.Id)
err = h.DB.System.SetLastEntry(breakingNews.Id)
if err != nil {
return fmt.Errorf("failed writing last entry to DB: %w", err)
}
Expand Down

0 comments on commit b25678b

Please sign in to comment.