From 5a02be0517d3c5de55aa454d0bb298f541b41d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BF=E3=81=A6=E3=82=8B=E3=81=9E?= Date: Sat, 31 Jan 2026 03:07:19 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=E3=83=8B=E3=82=B3=E3=83=8B=E3=82=B3?= =?UTF-8?q?=E5=90=8C=E6=9C=9F=E3=81=A7=E3=82=AA=E3=83=AA=E3=82=B8=E3=83=8A?= =?UTF-8?q?=E3=83=AB=E3=81=AE=E6=8A=95=E7=A8=BF=E6=97=A5=E6=99=82=E9=80=A3?= =?UTF-8?q?=E6=90=BA=EF=BC=88#246=EF=BC=89=20(#248)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #246 Co-authored-by: miteruzo Reviewed-on: https://git.miteruzo.com/miteruzo/btrc-hub/pulls/248 --- backend/lib/tasks/sync_nico.rake | 13 ++++++++++--- backend/spec/tasks/nico_sync_spec.rb | 11 +++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/backend/lib/tasks/sync_nico.rake b/backend/lib/tasks/sync_nico.rake index 86bd941..591307a 100644 --- a/backend/lib/tasks/sync_nico.rake +++ b/backend/lib/tasks/sync_nico.rake @@ -47,11 +47,18 @@ namespace :nico do code = datum['code'] post = Post.where('url REGEXP ?', "nicovideo\\.jp/watch/#{ Regexp.escape(code) }([^0-9]|$)") .first - unless post - title = datum['title'] + title = datum['title'] + original_created_at = datum['uploaded_at'] && Time.iso8601(datum['uploaded_at']) + original_created_from = original_created_at&.change(sec: 0) + original_created_before = original_created_from&.+(1.minute) + if post + attrs = { title:, original_created_from:, original_created_before: } + post.update!(attrs) if attrs.any? { |k, v| post.public_send(k) != v } + else url = "https://www.nicovideo.jp/watch/#{ code }" thumbnail_base = fetch_thumbnail.(url) rescue nil - post = Post.new(title:, url:, thumbnail_base:, uploaded_user: nil) + post = Post.new(title:, url:, thumbnail_base:, uploaded_user: nil, + original_created_from:, original_created_before:) if thumbnail_base.present? post.thumbnail.attach( io: URI.open(thumbnail_base), diff --git a/backend/spec/tasks/nico_sync_spec.rb b/backend/spec/tasks/nico_sync_spec.rb index e4cef17..3f773f4 100644 --- a/backend/spec/tasks/nico_sync_spec.rb +++ b/backend/spec/tasks/nico_sync_spec.rb @@ -13,7 +13,6 @@ RSpec.describe "nico:sync" do end def link_nico_to_tag!(nico_tag, tag) - # NicoTagRelation がある前提(君の model からそう見える) NicoTagRelation.create!(nico_tag_id: nico_tag.id, tag_id: tag.id) end @@ -35,7 +34,12 @@ RSpec.describe "nico:sync" do Tag.tagme # pythonの出力(AAA が付く) - stub_python([{ "code" => "sm9", "title" => "t", "tags" => ["AAA"] }]) + stub_python([{ + 'code' => 'sm9', + 'title' => 't', + 'tags' => ['AAA'], + 'uploaded_at' => '2026-01-01T12:34:56+09:00', + 'deleted_at' => '2026-01-31T00:00:00+09:00' }]) # 外部HTTPは今回「既存 post なので呼ばれない」はずだが、念のため塞ぐ allow(URI).to receive(:open).and_return(StringIO.new("")) @@ -49,6 +53,9 @@ RSpec.describe "nico:sync" do expect(active_tag_names).to include("nico:AAA") expect(active_tag_names).to include("spec_linked") + expect(post.original_created_from).to eq(Time.iso8601('2026-01-01T03:34:00Z')) + expect(post.original_created_before).to eq(Time.iso8601('2026-01-01T03:35:00Z')) + # 差分が出るので bot が付く(kept_non_nico_ids != desired_non_nico_ids) expect(active_tag_names).to include("bot操作") end From 5451d9ec9fa470b1d99d04b9a6574417509bf23b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BF=E3=81=A6=E3=82=8B=E3=81=9E?= Date: Sat, 31 Jan 2026 03:22:23 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E3=83=8B=E3=82=B3=E3=83=8B=E3=82=B3?= =?UTF-8?q?=E5=90=8C=E6=9C=9F=E6=99=82=E3=81=AB=E3=82=AA=E3=83=AA=E3=82=B8?= =?UTF-8?q?=E3=83=8A=E3=83=AB=E3=81=AE=E6=8A=95=E7=A8=BF=E6=97=A5=E6=99=82?= =?UTF-8?q?=E9=80=A3=E6=90=BA=EF=BC=8C=E5=BD=A2=E5=BC=8F=E3=81=AE=E3=83=90?= =?UTF-8?q?=E3=82=B0=E4=BF=AE=E6=AD=A3=EF=BC=88#246=EF=BC=89=20(#249)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #246 #246 #246 Co-authored-by: miteruzo Reviewed-on: https://git.miteruzo.com/miteruzo/btrc-hub/pulls/249 --- backend/lib/tasks/sync_nico.rake | 3 ++- backend/spec/tasks/nico_sync_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/backend/lib/tasks/sync_nico.rake b/backend/lib/tasks/sync_nico.rake index 591307a..7fa8fee 100644 --- a/backend/lib/tasks/sync_nico.rake +++ b/backend/lib/tasks/sync_nico.rake @@ -48,7 +48,8 @@ namespace :nico do post = Post.where('url REGEXP ?', "nicovideo\\.jp/watch/#{ Regexp.escape(code) }([^0-9]|$)") .first title = datum['title'] - original_created_at = datum['uploaded_at'] && Time.iso8601(datum['uploaded_at']) + original_created_at = datum['uploaded_at'] && + Time.strptime(datum['uploaded_at'], '%Y-%m-%d %H:%M:%S') original_created_from = original_created_at&.change(sec: 0) original_created_before = original_created_from&.+(1.minute) if post diff --git a/backend/spec/tasks/nico_sync_spec.rb b/backend/spec/tasks/nico_sync_spec.rb index 3f773f4..daa243e 100644 --- a/backend/spec/tasks/nico_sync_spec.rb +++ b/backend/spec/tasks/nico_sync_spec.rb @@ -38,8 +38,8 @@ RSpec.describe "nico:sync" do 'code' => 'sm9', 'title' => 't', 'tags' => ['AAA'], - 'uploaded_at' => '2026-01-01T12:34:56+09:00', - 'deleted_at' => '2026-01-31T00:00:00+09:00' }]) + 'uploaded_at' => '2026-01-01 12:34:56', + 'deleted_at' => '2026-01-31 00:00:00' }]) # 外部HTTPは今回「既存 post なので呼ばれない」はずだが、念のため塞ぐ allow(URI).to receive(:open).and_return(StringIO.new("")) From 5dd683bd4fef39bdb9020c85f7be4ef1060280cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BF=E3=81=A6=E3=82=8B=E3=81=9E?= Date: Sat, 31 Jan 2026 11:38:32 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E3=82=B5=E3=83=A0=E3=83=8D=E5=86=8D?= =?UTF-8?q?=E5=8F=96=E5=BE=97=EF=BC=88#193=EF=BC=89=20(#250)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #193 Co-authored-by: miteruzo Reviewed-on: https://git.miteruzo.com/miteruzo/btrc-hub/pulls/250 --- backend/lib/tasks/sync_nico.rake | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/backend/lib/tasks/sync_nico.rake b/backend/lib/tasks/sync_nico.rake index 7fa8fee..989c75e 100644 --- a/backend/lib/tasks/sync_nico.rake +++ b/backend/lib/tasks/sync_nico.rake @@ -1,10 +1,12 @@ namespace :nico do desc 'ニコニコ DB 同期' task sync: :environment do + require 'json' require 'open3' require 'open-uri' require 'nokogiri' require 'set' + require 'time' fetch_thumbnail = -> url do html = URI.open(url, read_timeout: 60, 'User-Agent' => 'Mozilla/5.0').read @@ -45,16 +47,37 @@ namespace :nico do data = JSON.parse(stdout) data.each do |datum| code = datum['code'] - post = Post.where('url REGEXP ?', "nicovideo\\.jp/watch/#{ Regexp.escape(code) }([^0-9]|$)") - .first + + post = + Post + .where('url REGEXP ?', "nicovideo\\.jp/watch/#{ Regexp.escape(code) }([^0-9]|$)") + .first + title = datum['title'] original_created_at = datum['uploaded_at'] && Time.strptime(datum['uploaded_at'], '%Y-%m-%d %H:%M:%S') original_created_from = original_created_at&.change(sec: 0) original_created_before = original_created_from&.+(1.minute) + if post attrs = { title:, original_created_from:, original_created_before: } - post.update!(attrs) if attrs.any? { |k, v| post.public_send(k) != v } + + unless post.thumbnail.attached? + thumbnail_base = fetch_thumbnail.(post.url) rescue nil + if thumbnail_base.present? + post.thumbnail.attach( + io: URI.open(thumbnail_base), + filename: File.basename(URI.parse(thumbnail_base).path), + content_type: 'image/jpeg') + attrs[:thumbnail_base] = thumbnail_base + end + end + + post.assign_attributes(attrs) + if post.changed? + post.save! + post.resized_thumbnail! if post.thumbnail.attached? + end else url = "https://www.nicovideo.jp/watch/#{ code }" thumbnail_base = fetch_thumbnail.(url) rescue nil From f3cd108b2e2f09d28277953cede9b28aaa23d63e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BF=E3=81=A6=E3=82=8B=E3=81=9E?= Date: Sat, 31 Jan 2026 15:00:56 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=E3=83=8B=E3=82=B3=E3=83=8B=E3=82=B3?= =?UTF-8?q?=E5=90=8C=E6=9C=9F=E3=81=AB=E3=81=A6=20=E2=80=9C=E3=83=8B?= =?UTF-8?q?=E3=82=B3=E3=83=8B=E3=82=B3=E2=80=9D=20=E3=81=8A=E3=82=88?= =?UTF-8?q?=E3=81=B3=20=E2=80=9C=E5=8B=95=E7=94=BB=E2=80=9D=20=E3=82=92?= =?UTF-8?q?=E8=87=AA=E5=8B=95=E4=BB=98=E4=B8=8E=E3=81=99=E3=82=8B=E3=82=84?= =?UTF-8?q?=E3=81=85=E3=81=AB=EF=BC=88#156=EF=BC=89=20(#251)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #156 Co-authored-by: miteruzo Reviewed-on: https://git.miteruzo.com/miteruzo/btrc-hub/pulls/251 --- backend/app/models/tag.rb | 10 +++++++++- backend/lib/tasks/sync_nico.rake | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/backend/app/models/tag.rb b/backend/app/models/tag.rb index e772e9c..c4b36e6 100644 --- a/backend/app/models/tag.rb +++ b/backend/app/models/tag.rb @@ -67,9 +67,17 @@ class Tag < ApplicationRecord @bot ||= find_or_create_by_tag_name!('bot操作', category: :meta) end + def self.video + @video ||= find_or_create_by_tag_name!('動画', category: :meta) + end + + def self.niconico + @niconico ||= find_or_create_by_tag_name!('ニコニコ', category: :meta) + end + def self.normalise_tags tag_names, with_tagme: true, deny_nico: true if deny_nico && tag_names.any? { |n| n.start_with?('nico:') } - raise NicoTagNormalisationError + raise NicoTagNormalisationError end tags = tag_names.map do |name| diff --git a/backend/lib/tasks/sync_nico.rake b/backend/lib/tasks/sync_nico.rake index 989c75e..b3596cb 100644 --- a/backend/lib/tasks/sync_nico.rake +++ b/backend/lib/tasks/sync_nico.rake @@ -91,7 +91,7 @@ namespace :nico do end post.save! post.resized_thumbnail! - sync_post_tags!(post, [Tag.tagme.id]) + sync_post_tags!(post, [Tag.tagme.id, Tag.bot.id, Tag.niconico.id, Tag.video.id]) end kept_ids = PostTag.kept.where(post_id: post.id).pluck(:tag_id).to_set