From 0dfd038fe7c23b17b652498356d47786d2a07768 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Wed, 2 Jul 2025 01:51:23 +0900 Subject: [PATCH] #75 --- backend/Gemfile | 4 +++ backend/Gemfile.lock | 8 +++++ backend/app/controllers/posts_controller.rb | 2 +- backend/config/schedule.rb | 3 ++ backend/lib/tasks/.keep | 0 backend/lib/tasks/sync_nico.rake | 35 +++++++++++++++++++++ 6 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 backend/config/schedule.rb delete mode 100644 backend/lib/tasks/.keep create mode 100644 backend/lib/tasks/sync_nico.rake diff --git a/backend/Gemfile b/backend/Gemfile index 73f83db..bb5460b 100644 --- a/backend/Gemfile +++ b/backend/Gemfile @@ -59,3 +59,7 @@ gem "nokogiri", "~> 1.18" gem 'gollum' gem 'diff-lcs' + +gem 'dotenv-rails' + +gem 'whenever', require: false diff --git a/backend/Gemfile.lock b/backend/Gemfile.lock index aadbea7..8494a53 100644 --- a/backend/Gemfile.lock +++ b/backend/Gemfile.lock @@ -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 diff --git a/backend/app/controllers/posts_controller.rb b/backend/app/controllers/posts_controller.rb index 1a86319..70eb22f 100644 --- a/backend/app/controllers/posts_controller.rb +++ b/backend/app/controllers/posts_controller.rb @@ -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! diff --git a/backend/config/schedule.rb b/backend/config/schedule.rb new file mode 100644 index 0000000..3f5ab6e --- /dev/null +++ b/backend/config/schedule.rb @@ -0,0 +1,3 @@ +every 1.day, at: '3:00 pm' do + rake 'nico:sync', environment: 'production' +end diff --git a/backend/lib/tasks/.keep b/backend/lib/tasks/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/backend/lib/tasks/sync_nico.rake b/backend/lib/tasks/sync_nico.rake new file mode 100644 index 0000000..97af101 --- /dev/null +++ b/backend/lib/tasks/sync_nico.rake @@ -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 +