From 31e36684a9ed2bc2cb239afa0c9c2a06f66bf0a2 Mon Sep 17 00:00:00 2001 From: nacchan Date: Thu, 31 Jul 2025 14:34:09 +0900 Subject: [PATCH 1/5] =?UTF-8?q?refactor:=20RSS=E5=8F=96=E5=BE=97=E3=83=BB?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E5=87=A6=E7=90=86=E3=82=92=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=E5=88=86=E5=89=B2=EF=BC=86=E3=83=AD=E3=82=B8?= =?UTF-8?q?=E3=83=83=E3=82=AF=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/fetch_news.rake | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/tasks/fetch_news.rake b/lib/tasks/fetch_news.rake index 415d5df25..50b583624 100644 --- a/lib/tasks/fetch_news.rake +++ b/lib/tasks/fetch_news.rake @@ -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 @@ -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'] } From f0230eee249277675da8f6bd60bd335e8f6cb505 Mon Sep 17 00:00:00 2001 From: nacchan Date: Thu, 31 Jul 2025 15:35:13 +0900 Subject: [PATCH 2/5] =?UTF-8?q?fix:=20=E6=97=A2=E5=AD=98=E3=83=8B=E3=83=A5?= =?UTF-8?q?=E3=83=BC=E3=82=B9=E3=81=AE=E5=B7=AE=E5=88=86=E3=81=8C=E3=81=82?= =?UTF-8?q?=E3=82=8B=E5=A0=B4=E5=90=88=E3=81=AE=E3=81=BF=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/fetch_news.rake | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/tasks/fetch_news.rake b/lib/tasks/fetch_news.rake index 50b583624..8e6b444c6 100644 --- a/lib/tasks/fetch_news.rake +++ b/lib/tasks/fetch_news.rake @@ -76,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 From f0160ad4dc9618d166605cc4e4d33a10dd594421 Mon Sep 17 00:00:00 2001 From: nacchan Date: Thu, 31 Jul 2025 15:55:54 +0900 Subject: [PATCH 3/5] =?UTF-8?q?fix:=20import=E6=99=82=E3=81=AB=E6=96=B0?= =?UTF-8?q?=E8=A6=8F=E3=83=BB=E5=A4=89=E6=9B=B4=E3=81=8C=E3=81=82=E3=82=8B?= =?UTF-8?q?=E5=A0=B4=E5=90=88=E3=81=AE=E3=81=BF=E4=BF=9D=E5=AD=98=E3=81=97?= =?UTF-8?q?=E3=80=81=E4=BB=B6=E6=95=B0=E3=82=92=E9=9B=86=E8=A8=88=E3=81=97?= =?UTF-8?q?=E3=81=A6=E5=87=BA=E5=8A=9B=E3=81=99=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/import_news.rake | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/tasks/import_news.rake b/lib/tasks/import_news.rake index bebe76611..8a977c075 100644 --- a/lib/tasks/import_news.rake +++ b/lib/tasks/import_news.rake @@ -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)." end end From 79d486db0f82aa3ef9e20afddd92a1053b1b9738 Mon Sep 17 00:00:00 2001 From: nacchan Date: Tue, 5 Aug 2025 14:52:42 +0900 Subject: [PATCH 4/5] =?UTF-8?q?chore:=20news:import=20=E3=82=BF=E3=82=B9?= =?UTF-8?q?=E3=82=AF=E3=82=92=20logger=20=E5=87=BA=E5=8A=9B=E3=81=AB?= =?UTF-8?q?=E7=B5=B1=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/import_news.rake | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/tasks/import_news.rake b/lib/tasks/import_news.rake index 8a977c075..35e96da7e 100644 --- a/lib/tasks/import_news.rake +++ b/lib/tasks/import_news.rake @@ -3,6 +3,12 @@ require 'yaml' namespace :news do desc 'db/news.yml を読み込んで News テーブルを upsert する' task import_from_yaml: :environment do + file_logger = ActiveSupport::Logger.new('log/news.log') + console = ActiveSupport::Logger.new(STDOUT) + logger = ActiveSupport::BroadcastLogger.new(file_logger, console) + + logger.info "==== START news:import_from_yaml ====" + yaml_path = Rails.root.join('db', 'news.yml') raw = YAML.safe_load(File.read(yaml_path), permitted_classes: [Time], aliases: true) @@ -22,17 +28,15 @@ namespace :news do 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})" + status = is_new ? 'new' : 'updated' + new_count += 1 if is_new + updated_count += 1 unless is_new + + logger.info "[News] #{news.published_at.to_date} #{news.title} (#{status})" # ← puts から logger.info に変更 end end - puts "Imported #{new_count + updated_count} items (#{new_count} new, #{updated_count} updated)." + logger.info "Imported #{new_count + updated_count} items (#{new_count} new, #{updated_count} updated)." + logger.info "==== END news:import_from_yaml ====" end end From c41be03855d46d00a5679dcec76e87c97085e163 Mon Sep 17 00:00:00 2001 From: nacchan Date: Tue, 5 Aug 2025 15:44:27 +0900 Subject: [PATCH 5/5] =?UTF-8?q?refactor:=20=E4=B8=8D=E8=A6=81=E3=81=AA?= =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/import_news.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/import_news.rake b/lib/tasks/import_news.rake index 35e96da7e..45c77ee05 100644 --- a/lib/tasks/import_news.rake +++ b/lib/tasks/import_news.rake @@ -32,7 +32,7 @@ namespace :news do new_count += 1 if is_new updated_count += 1 unless is_new - logger.info "[News] #{news.published_at.to_date} #{news.title} (#{status})" # ← puts から logger.info に変更 + logger.info "[News] #{news.published_at.to_date} #{news.title} (#{status})" end end