From 4e322bd6e9edb510d3c684b6c4e88e99243bf6d4 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Wed, 5 Mar 2025 14:59:02 +0800 Subject: [PATCH] feat: add JSON feed support for weather art - Implement JSON feed generation in the RssController - Add new API endpoint to serve JSON feed - Update RSS feed view to include hidden machine-readable data This commit introduces a new feature that allows the application to serve weather art data in JSON format. The JSON feed includes metadata such as the title, description, and additional custom fields for better integration with other services. The changes also enhance the existing RSS feed by embedding machine-readable data within the HTML structure, improving accessibility and usability for automated systems. --- app/controllers/rss_controller.rb | 31 +++++++++++++++++++++++++++++++ app/views/rss/feed.rss.builder | 29 ++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/app/controllers/rss_controller.rb b/app/controllers/rss_controller.rb index 2440cb3..6181b3c 100644 --- a/app/controllers/rss_controller.rb +++ b/app/controllers/rss_controller.rb @@ -4,6 +4,37 @@ class RssController < ApplicationController respond_to do |format| format.rss { render layout: false } + format.json { render json: generate_json_feed } end end + + private + + def generate_json_feed + { + version: "https://jsonfeed.org/version/1.1", + title: "Today AI Weather Art", + home_page_url: root_url, + feed_url: rss_feed_url(format: :json), + items: @weather_arts.map do |art| + { + id: city_weather_art_url(art.city, art), + url: city_weather_art_url(art.city, art), + title: "#{art.city.full_name} Weather Art", + content_html: "

#{art.description}

", + date_published: art.created_at.iso8601, + image: art.image.attached? ? rails_blob_url(art.webp_image.processed) : nil, + # 自定义字段 + _weather: { + country: art.city&.country&.name, + city: art.city&.name, + state: art.city&.state&.name, + description: art.description, + prompt: art.prompt, + date: art.weather_date&.strftime("%Y-%m-%d") + } + } + end + } + end end diff --git a/app/views/rss/feed.rss.builder b/app/views/rss/feed.rss.builder index 8f8c53a..6aabc10 100644 --- a/app/views/rss/feed.rss.builder +++ b/app/views/rss/feed.rss.builder @@ -13,7 +13,34 @@ xml.rss version: "2.0", @weather_arts.each do |art| xml.item do xml.title "#{art.city.full_name} Weather Art" - xml.description "Weather art for #{art.city.full_name} on #{art.weather_date&.strftime('%Y-%m-%d')}" + # xml.description "Weather art for #{art.city.full_name} on #{art.weather_date&.strftime('%Y-%m-%d')}" + xml.description do + content = <<~HTML +
+

Weather art for #{art.city.full_name} on #{art.weather_date&.strftime('%Y-%m-%d')}

+ #{' '} + +
+
+ #{' '} + + + + + + + +
+ HTML + + xml.cdata!(content) + end xml.pubDate art.created_at.to_fs(:rfc822) xml.link city_weather_art_url(art.city, art) xml.guid city_weather_art_url(art.city, art)