Skip to content

Commit

Permalink
Rewrite Tagesschau alerter to replace removed API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
eht16 committed Jul 21, 2024
1 parent 230a3c4 commit 2d2fe41
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
2 changes: 1 addition & 1 deletion ninette.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ags_husum = 010540000000
enable = true
fetch_interval = 240
class_path = ninette.provider.tagesschau.TagesschauBreakingNewsProvider
api_url = https://www.tagesschau.de/ipa/v1/web/headerapp/
api_url = https://www.tagesschau.de/api2u/homepage

# Test provider to create an alert every hour, useful for testing e.g. alerters
[provider_ping]
Expand Down
72 changes: 47 additions & 25 deletions ninette/provider/tagesschau.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,61 @@ def _process(self):

def _process_tagesschau_breaking_news(self):
news = self._fetch_tagesschau_news_feed()
alert = self._process_tagesschau_news(news)
if alert:
alerts = self._process_tagesschau_news(news)
for alert in list(alerts):
if self._alert_needs_to_be_processed(alert):
self._mark_alert_as_processed(alert)
return [alert]
else:
self._logger.debug('Skipping already processed alert "%s"', alert.identifier)
alerts.remove(alert)

return None
return alerts or None

def _fetch_tagesschau_news_feed(self):
response = self._perform_http_request(self._api_url)
return response.json()

def _process_tagesschau_news(self, news):
if 'breakingNews' in news:
breaking_news = news['breakingNews']
alert_text = ALERT_TEXT.format(
identifier=breaking_news['id'],
title=breaking_news['headline'],
url=breaking_news['url'],
text=breaking_news['text'],
date=breaking_news['date'])

alert = self._factor_alert(
identifier=breaking_news['id'],
title=f'Tagesschau: {breaking_news["headline"]}',
alert_type='BreakingNews',
text=alert_text)

# attach the original json to the alert
alert.attach_original_event(news)

return alert

return None
alerts_news = self._process_news_list(news.get('news', []))
alerts_regional = self._process_news_list(news.get('regional', []))
return alerts_news + alerts_regional

def _process_news_list(self, news):
alerts = []
for news_item in news:
if news_item['breakingNews']:
text = self._factor_breaking_news_text(news_item)
alert_text = ALERT_TEXT.format(
identifier=news_item['sophoraId'],
title=news_item['title'],
url=news_item['detailsweb'],
text=text,
date=news_item['date'])

alert = self._factor_alert(
identifier=news_item['sophoraId'],
title=f'Tagesschau: {news_item["title"]}',
alert_type='BreakingNews',
text=alert_text)

# attach the original json to the alert
alert.attach_original_event(news_item)

alerts.append(alert)

return alerts

def _factor_breaking_news_text(self, news_item):
news_text = []
for content_item in news_item.get('content', []):
if content_item['type'] == 'text':
plain_text = self._replace_html(content_item['value'])
news_text.append(plain_text)
news_text.append('\n')
elif content_item['type'] == 'headline':
news_text.append('\n')
plain_text = self._replace_html(content_item['value'])
news_text.append(plain_text)
news_text.append('\n')

return '\n'.join(news_text)

0 comments on commit 2d2fe41

Please sign in to comment.