From 93f49f1fb3109c2e0d5f427cd2494eaa97e80f04 Mon Sep 17 00:00:00 2001 From: Viktor Lofgren Date: Thu, 17 Aug 2023 17:42:31 +0200 Subject: [PATCH] (search-service) RSS feed for the news feed --- .../nu/marginalia/search/SearchService.java | 1 + .../search/svc/SearchFrontPageService.java | 40 +++++++++++++++++++ .../templates/search/index/index-news.hdb | 3 ++ 3 files changed, 44 insertions(+) diff --git a/code/services-core/search-service/src/main/java/nu/marginalia/search/SearchService.java b/code/services-core/search-service/src/main/java/nu/marginalia/search/SearchService.java index 5fe5751e..30d978f6 100644 --- a/code/services-core/search-service/src/main/java/nu/marginalia/search/SearchService.java +++ b/code/services-core/search-service/src/main/java/nu/marginalia/search/SearchService.java @@ -58,6 +58,7 @@ public class SearchService extends Service { Spark.get("/public/search", searchQueryService::pathSearch); Spark.get("/public/site-search/:site/*", this::siteSearchRedir); Spark.get("/public/", frontPageService::render); + Spark.get("/public/news.xml", frontPageService::renderNewsFeed); Spark.get("/public/:resource", this::serveStatic); Spark.post("/public/site/suggest/", addToCrawlQueueService::suggestCrawling); diff --git a/code/services-core/search-service/src/main/java/nu/marginalia/search/svc/SearchFrontPageService.java b/code/services-core/search-service/src/main/java/nu/marginalia/search/svc/SearchFrontPageService.java index 8d409530..8ebd9f8f 100644 --- a/code/services-core/search-service/src/main/java/nu/marginalia/search/svc/SearchFrontPageService.java +++ b/code/services-core/search-service/src/main/java/nu/marginalia/search/svc/SearchFrontPageService.java @@ -14,6 +14,9 @@ import spark.Response; import java.io.IOException; import java.sql.SQLException; import java.time.LocalDate; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; @@ -72,6 +75,43 @@ public class SearchFrontPageService { return items; } + public Object renderNewsFeed(Request request, Response response) { + List newsItems = getNewsItems(); + + StringBuilder sb = new StringBuilder(); + + sb.append(""" + + + + Marginalia Search News and Mentions + https://search.marginalia.nu/ + News and Mentions of Marginalia Search + en-us + 60 + """); + + sb.append("").append(ZonedDateTime.now().format(DateTimeFormatter.RFC_1123_DATE_TIME)).append("\n"); + sb.append("").append(ZonedDateTime.now().format(DateTimeFormatter.RFC_1123_DATE_TIME)).append("\n"); + sb.append("60\n"); + for (var item : newsItems) { + sb.append("\n"); + sb.append("").append(item.title()).append("\n"); + sb.append("").append(item.url()).append("\n"); + if (item.source != null) { + sb.append("").append(item.source()).append("\n"); + } + sb.append("").append(item.date().atStartOfDay().atZone(ZoneId.systemDefault()).format(DateTimeFormatter.RFC_1123_DATE_TIME)).append("\n"); + sb.append("\n"); + } + sb.append("\n"); + sb.append("\n"); + + response.type("application/rss+xml"); + + return sb.toString(); + } + private record IndexModel(List news, int searchPerMinute) { } private record NewsItem(String title, String url, String source, LocalDate date) {} } diff --git a/code/services-core/search-service/src/main/resources/templates/search/index/index-news.hdb b/code/services-core/search-service/src/main/resources/templates/search/index/index-news.hdb index ce1ae82e..52d5abd4 100644 --- a/code/services-core/search-service/src/main/resources/templates/search/index/index-news.hdb +++ b/code/services-core/search-service/src/main/resources/templates/search/index/index-news.hdb @@ -10,5 +10,8 @@ {{/each}} + {{/if}} \ No newline at end of file