today_ai_weather/app/views/rss/feed.rss.builder
songtianlun 4e322bd6e9 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.
2025-03-05 14:59:02 +08:00

64 lines
3.0 KiB
Ruby
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# app/views/rss/feed.rss.builder
xml.instruct! :xml, version: "1.0"
xml.rss version: "2.0",
"xmlns:atom" => "http://www.w3.org/2005/Atom",
"xmlns:weather" => "http://todayaiweather.com/ns/weather/" do
xml.channel do
xml.title "Today AI Weather Art"
xml.description "Daily AI-generated weather art and forecasts"
xml.link root_url
xml.language "en"
xml.atom :link, href: rss_feed_url(format: :rss), rel: "self", type: "application/rss+xml"
@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 do
content = <<~HTML
<div class="weather-art-data">
<p>Weather art for #{art.city.full_name} on #{art.weather_date&.strftime('%Y-%m-%d')}</p>
#{' '}
<!-- 使data属性 -->
<div style="display:none"#{' '}
data-country="#{CGI.escapeHTML(art.city&.country&.name.to_s)}"
data-state="#{CGI.escapeHTML(art.city&.state&.name.to_s)}"
data-city="#{CGI.escapeHTML(art.city&.name.to_s)}"
data-weather-date="#{art.weather_date&.strftime('%Y-%m-%d')}"
data-description="#{CGI.escapeHTML(art.description.to_s)}"
data-prompt="#{CGI.escapeHTML(art.prompt.to_s)}">
</div>
#{' '}
<!-- 或使用Meta标签形式 -->
<meta name="weather:country" content="#{CGI.escapeHTML(art.city&.country&.name.to_s)}">
<meta name="weather:state" content="#{CGI.escapeHTML(art.city&.state&.name.to_s)}">
<meta name="weather:city" content="#{CGI.escapeHTML(art.city&.name.to_s)}">
<meta name="weather:date" content="#{art.weather_date&.strftime('%Y-%m-%d')}">
<meta name="weather:description" content="#{CGI.escapeHTML(art.description.to_s)}">
<meta name="weather:prompt" content="#{CGI.escapeHTML(art.prompt.to_s)}">
</div>
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)
# 添加图片
xml.enclosure url: rails_blob_url(art.webp_image.processed), type: "image/webp" if art.image.attached?
# 使用自定义命名空间添加字段
xml.weather :country, art.city&.country&.name
xml.weather :city, art.city&.name
xml.weather :state, art.city&.state&.name
xml.weather :weatherDescription, art.description
xml.weather :prompt, art.prompt
xml.weather :date, art.weather_date&.strftime("%Y-%m-%d")
xml.weather :dateYear, art.weather_date&.strftime("%Y")
xml.weather :cityUrl, city_url(art.city)
end
end
end
end