ニコニコ同期でオリジナルの投稿日時連携(#246) (#248)
#246 Co-authored-by: miteruzo <miteruzo@naver.com> Reviewed-on: #248
This commit was merged in pull request #248.
This commit is contained in:
@@ -47,11 +47,18 @@ namespace :nico do
|
|||||||
code = datum['code']
|
code = datum['code']
|
||||||
post = Post.where('url REGEXP ?', "nicovideo\\.jp/watch/#{ Regexp.escape(code) }([^0-9]|$)")
|
post = Post.where('url REGEXP ?', "nicovideo\\.jp/watch/#{ Regexp.escape(code) }([^0-9]|$)")
|
||||||
.first
|
.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 }"
|
url = "https://www.nicovideo.jp/watch/#{ code }"
|
||||||
thumbnail_base = fetch_thumbnail.(url) rescue nil
|
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?
|
if thumbnail_base.present?
|
||||||
post.thumbnail.attach(
|
post.thumbnail.attach(
|
||||||
io: URI.open(thumbnail_base),
|
io: URI.open(thumbnail_base),
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ RSpec.describe "nico:sync" do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def link_nico_to_tag!(nico_tag, tag)
|
def link_nico_to_tag!(nico_tag, tag)
|
||||||
# NicoTagRelation がある前提(君の model からそう見える)
|
|
||||||
NicoTagRelation.create!(nico_tag_id: nico_tag.id, tag_id: tag.id)
|
NicoTagRelation.create!(nico_tag_id: nico_tag.id, tag_id: tag.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -35,7 +34,12 @@ RSpec.describe "nico:sync" do
|
|||||||
Tag.tagme
|
Tag.tagme
|
||||||
|
|
||||||
# pythonの出力(AAA が付く)
|
# 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 なので呼ばれない」はずだが、念のため塞ぐ
|
# 外部HTTPは今回「既存 post なので呼ばれない」はずだが、念のため塞ぐ
|
||||||
allow(URI).to receive(:open).and_return(StringIO.new("<html></html>"))
|
allow(URI).to receive(:open).and_return(StringIO.new("<html></html>"))
|
||||||
@@ -49,6 +53,9 @@ RSpec.describe "nico:sync" do
|
|||||||
expect(active_tag_names).to include("nico:AAA")
|
expect(active_tag_names).to include("nico:AAA")
|
||||||
expect(active_tag_names).to include("spec_linked")
|
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)
|
# 差分が出るので bot が付く(kept_non_nico_ids != desired_non_nico_ids)
|
||||||
expect(active_tag_names).to include("bot操作")
|
expect(active_tag_names).to include("bot操作")
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user