Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions lib/tasks/fetch_news.rake
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ def safe_open(url)
end
end

def fetch_rss_items(url, logger)
logger.info("Fetching RSS → #{url}")
begin
rss = safe_open(url)
feed = RSS::Parser.parse(rss, false)
feed.items.map { |item| item_to_hash(item) }
rescue => e
logger.warn("⚠️ Failed to fetch #{url}: #{e.message}")
[]
end
end

def item_to_hash(item)
{
'url' => item.link,
'title' => item.title,
'published_at' => item.pubDate.to_s
}
end

namespace :news do
desc 'RSS フィードから最新ニュースを取得し、db/news.yml に書き出す'
task fetch: :environment do
Expand Down Expand Up @@ -45,24 +65,7 @@ namespace :news do
]
end

# RSS 取得&パース
new_items = feed_urls.flat_map do |url|
logger.info("Fetching RSS → #{url}")
begin
rss = safe_open(url)
feed = RSS::Parser.parse(rss, false)
feed.items.map do |item|
{
'url' => item.link,
'title' => item.title,
'published_at' => item.pubDate.to_s
}
end
rescue => e
logger.warn("⚠️ Failed to fetch #{url}: #{e.message}")
[]
end
end
new_items = feed_urls.flat_map { |url| fetch_rss_items(url, logger) }

# 既存データをハッシュに変換(URL をキーに)
existing_items_hash = existing_news.index_by { |item| item['url'] }
Expand All @@ -73,12 +76,12 @@ namespace :news do

new_items.each do |new_item|
if existing_items_hash.key?(new_item['url'])
# 既存アイテムの更新
existing_item = existing_items_hash[new_item['url']]
updated_item = existing_item.merge(new_item) # 新しい情報で更新
updated_items << updated_item
# タイトルまたは公開日が変わった場合のみ更新
if existing_item['title'] != new_item['title'] || existing_item['published_at'] != new_item['published_at']
updated_items << existing_item.merge(new_item)
end
else
# 完全に新しいアイテム
truly_new_items << new_item
end
end
Expand Down
20 changes: 17 additions & 3 deletions lib/tasks/import_news.rake
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,31 @@ namespace :news do

# entries を計算
entries = raw['news'] || []
new_count = 0
updated_count = 0

entries.each do |attrs|
news = News.find_or_initialize_by(url: attrs['url'])
is_new = news.new_record?

news.assign_attributes(
title: attrs['title'],
published_at: attrs['published_at']
)
news.save!
puts "[news] #{news.published_at.to_date} #{news.title}"

if is_new || news.changed?
news.save!
if is_new
new_count += 1
status = 'new'
else
updated_count += 1
status = 'updated'
end
puts "[News] #{news.published_at.to_date} #{news.title} (#{status})"
end
end

puts "Imported #{entries.size} items."
puts "Imported #{new_count + updated_count} items (#{new_count} new, #{updated_count} updated)."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提案なので更新はお任せしますが、fetch_news.rake と同様に logger で出力する方法に揃えてあげても良さそうです📝
(出力の内容は良さそうです🙆‍♀️)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます!🙌
確かに logger 出力に揃えた方がスッキリすると思うので、fetch_news.rake と同様の logger 設定に変更します✨

end
end