This commit is contained in:
2025-07-02 01:51:23 +09:00
parent 7af66db498
commit e211d0ff52
6 changed files with 51 additions and 1 deletions
View File
+35
View File
@@ -0,0 +1,35 @@
namespace :nico do
desc 'ニコニコ DB 同期'
task sync: :environment do
require 'open3'
mysql_user = ENV['MYSQL_USER']
mysql_pass = ENV['MYSQL_PASS']
nizika_nico_path = ENV['NIZIKA_NICO_PATH']
stdout, stderr, status = Open3.capture3(
{ 'MYSQL_USER' => mysql_user, 'MYSQL_PASS' => mysql_pass },
'python3', "#{ nizika_nico_path }/get_videos.py")
if status.success?
data = JSON.parse(stdout)
data.each do |datum|
post = Post.where('url LIKE ?', '%nicovideo.jp%').find { |post|
post.url =~ %r{#{ Regexp.escape(datum['code']) }(?!\d)}
}
unless post
title = datum['title']
url = "https://www.nicovideo.jp/watch/#{ datum['code'] }"
post = Post.new(title:, url:, thumbnail_base: '', uploaded_user: nil)
post.save!
end
post.tags.destroy(post.tags.where(category: 'nico'))
datum['tags'].each do |name|
name = "nico:#{ name }"
tag = Tag.find_or_initialize_by(name:, category: 'nico')
post.tags << tag
end
end
end
end
end