(search-service) RSS feed for the news feed

This commit is contained in:
Viktor Lofgren 2023-08-17 17:42:31 +02:00
parent b83bb5a48a
commit 93f49f1fb3
3 changed files with 44 additions and 0 deletions

View File

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

View File

@ -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<NewsItem> newsItems = getNewsItems();
StringBuilder sb = new StringBuilder();
sb.append("""
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Marginalia Search News and Mentions</title>
<link>https://search.marginalia.nu/</link>
<description>News and Mentions of Marginalia Search</description>
<language>en-us</language>
<ttl>60</ttl>
""");
sb.append("<lastBuildDate>").append(ZonedDateTime.now().format(DateTimeFormatter.RFC_1123_DATE_TIME)).append("</lastBuildDate>\n");
sb.append("<pubDate>").append(ZonedDateTime.now().format(DateTimeFormatter.RFC_1123_DATE_TIME)).append("</pubDate>\n");
sb.append("<ttl>60</ttl>\n");
for (var item : newsItems) {
sb.append("<item>\n");
sb.append("<title>").append(item.title()).append("</title>\n");
sb.append("<link>").append(item.url()).append("</link>\n");
if (item.source != null) {
sb.append("<author>").append(item.source()).append("</author>\n");
}
sb.append("<pubDate>").append(item.date().atStartOfDay().atZone(ZoneId.systemDefault()).format(DateTimeFormatter.RFC_1123_DATE_TIME)).append("</pubDate>\n");
sb.append("</item>\n");
}
sb.append("</channel>\n");
sb.append("</rss>\n");
response.type("application/rss+xml");
return sb.toString();
}
private record IndexModel(List<NewsItem> news, int searchPerMinute) { }
private record NewsItem(String title, String url, String source, LocalDate date) {}
}

View File

@ -10,5 +10,8 @@
{{/each}}
</dl>
</div>
<div class="utils">
<a href="/news.xml">📡 RSS Feed</a>
</div>
</section>
{{/if}}