みてるぞ 2 weeks ago
parent
commit
0dfd038fe7
6 changed files with 51 additions and 1 deletions
  1. +4
    -0
      backend/Gemfile
  2. +8
    -0
      backend/Gemfile.lock
  3. +1
    -1
      backend/app/controllers/posts_controller.rb
  4. +3
    -0
      backend/config/schedule.rb
  5. +0
    -0
      backend/lib/tasks/.keep
  6. +35
    -0
      backend/lib/tasks/sync_nico.rake

+ 4
- 0
backend/Gemfile View File

@@ -59,3 +59,7 @@ gem "nokogiri", "~> 1.18"
gem 'gollum'

gem 'diff-lcs'

gem 'dotenv-rails'

gem 'whenever', require: false

+ 8
- 0
backend/Gemfile.lock 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
backend/app/controllers/posts_controller.rb 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
- 0
backend/config/schedule.rb View File

@@ -0,0 +1,3 @@
every 1.day, at: '3:00 pm' do
rake 'nico:sync', environment: 'production'
end

+ 0
- 0
backend/lib/tasks/.keep View File


+ 35
- 0
backend/lib/tasks/sync_nico.rake 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


Loading…
Cancel
Save