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
+4
View File
@@ -59,3 +59,7 @@ gem "nokogiri", "~> 1.18"
gem 'gollum'
gem 'diff-lcs'
gem 'dotenv-rails'
gem 'whenever', require: false
+8
View File
@@ -84,12 +84,16 @@ GEM
brakeman (7.0.2)
racc
builder (3.3.0)
chronic (0.10.2)
concurrent-ruby (1.3.5)
connection_pool (2.5.3)
crass (1.0.6)
date (3.4.1)
diff-lcs (1.6.2)
dotenv (3.1.8)
dotenv-rails (3.1.8)
dotenv (= 3.1.8)
railties (>= 6.1)
drb (2.2.1)
ed25519 (1.4.0)
erubi (1.13.1)
@@ -396,6 +400,8 @@ GEM
base64
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
whenever (1.0.0)
chronic (>= 0.6.3)
zeitwerk (2.7.2)
PLATFORMS
@@ -414,6 +420,7 @@ DEPENDENCIES
bootsnap
brakeman
diff-lcs
dotenv-rails
gollum
image_processing (~> 1.14)
jwt
@@ -428,6 +435,7 @@ DEPENDENCIES
sqlite3 (>= 2.1)
thruster
tzinfo-data
whenever
BUNDLED WITH
2.6.5
+1 -1
View File
@@ -44,7 +44,7 @@ class PostsController < ApplicationController
# TODO: URL が正規のものがチェック,不正ならエラー
title = params[:title]
post = Post.new(title: title, url: params[:url], thumbnail_base: '', uploaded_user: current_user)
post = Post.new(title:, url: params[:url], thumbnail_base: '', uploaded_user: current_user)
post.thumbnail.attach(params[:thumbnail])
if post.save
post.resized_thumbnail!
+3
View File
@@ -0,0 +1,3 @@
every 1.day, at: '3:00 pm' do
rake 'nico:sync', environment: 'production'
end
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