feat: “ニジラー情報不詳” タグの自動付与(#106) (#196)
#106 #106 エラー対応 #106 Merge branch 'main' into '#106' Merge branch 'main' into '#106' Merge remote-tracking branch 'origin/main' into '#106' #106 誤字 Merge remote-tracking branch 'origin/main' into '#106' #106 ニジラー情報なし Co-authored-by: miteruzo <miteruzo@naver.com> Reviewed-on: #196
This commit was merged in pull request #196.
This commit is contained in:
@@ -76,6 +76,10 @@ class Tag < ApplicationRecord
|
||||
@bot ||= find_or_create_by_tag_name!('bot操作', category: :meta)
|
||||
end
|
||||
|
||||
def self.no_deerjikist
|
||||
@no_deerjikist ||= find_or_create_by_tag_name!('ニジラー情報不詳', category: :meta)
|
||||
end
|
||||
|
||||
def self.video
|
||||
@video ||= find_or_create_by_tag_name!('動画', category: :meta)
|
||||
end
|
||||
@@ -100,6 +104,7 @@ class Tag < ApplicationRecord
|
||||
end
|
||||
|
||||
tags << Tag.tagme if with_tagme && tags.size < 10 && tags.none?(Tag.tagme)
|
||||
tags << Tag.no_deerjikist if tags.all? { |t| t.category != 'deerjikist' }
|
||||
tags.uniq(&:id)
|
||||
end
|
||||
|
||||
|
||||
@@ -2,9 +2,9 @@ namespace :nico do
|
||||
desc 'ニコニコ DB 同期'
|
||||
task sync: :environment do
|
||||
require 'json'
|
||||
require 'open3'
|
||||
require 'open-uri'
|
||||
require 'nokogiri'
|
||||
require 'open-uri'
|
||||
require 'open3'
|
||||
require 'set'
|
||||
require 'time'
|
||||
|
||||
@@ -15,12 +15,12 @@ namespace :nico do
|
||||
doc.at('meta[name="thumbnail"]')&.[]('content').presence
|
||||
end
|
||||
|
||||
def sync_post_tags! post, desired_tag_ids, current_ids: nil
|
||||
current_ids ||= PostTag.kept.where(post_id: post.id).pluck(:tag_id).to_set
|
||||
desired_ids = desired_tag_ids.compact.to_set
|
||||
def sync_post_tags! post, desired_tag_ids, current_tag_ids: nil
|
||||
current_tag_ids ||= PostTag.kept.where(post_id: post.id).pluck(:tag_id).to_set
|
||||
desired_tag_ids = desired_tag_ids.compact.to_set
|
||||
|
||||
to_add = desired_ids - current_ids
|
||||
to_remove = current_ids - desired_ids
|
||||
to_add = desired_tag_ids - current_tag_ids
|
||||
to_remove = current_tag_ids - desired_tag_ids
|
||||
|
||||
Tag.where(id: to_add.to_a).find_each do |tag|
|
||||
begin
|
||||
@@ -42,7 +42,10 @@ namespace :nico do
|
||||
{ 'MYSQL_USER' => mysql_user, 'MYSQL_PASS' => mysql_pass },
|
||||
'python3', "#{ nizika_nico_path }/get_videos.py")
|
||||
|
||||
abort unless status.success?
|
||||
unless status.success?
|
||||
warn stderr
|
||||
abort
|
||||
end
|
||||
|
||||
data = JSON.parse(stdout)
|
||||
data.each do |datum|
|
||||
@@ -93,40 +96,50 @@ namespace :nico do
|
||||
sync_post_tags!(post, [Tag.tagme.id, Tag.bot.id, Tag.niconico.id, Tag.video.id])
|
||||
end
|
||||
|
||||
kept_ids = post.tags.pluck(:id).to_set
|
||||
kept_non_nico_ids = post.tags.not_nico.pluck(:id).to_set
|
||||
tags = post.tags
|
||||
# 既存のタグ Id. 集合
|
||||
kept_tag_ids = tags.pluck(:id).to_set
|
||||
# うち内部タグ Id. 集合
|
||||
kept_non_nico_tag_ids = tags.not_nico.pluck(:id).to_set
|
||||
|
||||
desired_nico_ids = []
|
||||
desired_non_nico_ids = []
|
||||
# 記載すべき外部タグ Id. および連携される内部タグ Id. のリスト
|
||||
desired_nico_tag_based_ids = []
|
||||
# 記載すべき内部タグ Id. のリスト
|
||||
desired_non_nico_tag_ids = []
|
||||
|
||||
datum['tags'].each do |raw|
|
||||
name = "nico:#{ raw }"
|
||||
tag = Tag.find_or_create_by_tag_name!(name, category: :nico)
|
||||
desired_nico_ids << tag.id
|
||||
unless tag.id.in?(kept_ids)
|
||||
desired_nico_tag_based_ids << tag.id
|
||||
|
||||
# 新たに記載される外部タグと連携される内部タグを記載
|
||||
unless tag.id.in?(kept_tag_ids)
|
||||
linked_ids = tag.linked_tags.pluck(:id)
|
||||
desired_non_nico_ids.concat(linked_ids)
|
||||
desired_nico_ids.concat(linked_ids)
|
||||
desired_non_nico_tag_ids.concat(linked_ids)
|
||||
desired_nico_tag_based_ids.concat(linked_ids)
|
||||
end
|
||||
end
|
||||
|
||||
deerjikist = Deerjikist.find_by(platform: :nico, code: datum['user'])
|
||||
if deerjikist
|
||||
desired_non_nico_ids << deerjikist.tag_id
|
||||
desired_nico_ids << deerjikist.tag_id
|
||||
desired_non_nico_tag_ids << deerjikist.tag_id
|
||||
desired_nico_tag_based_ids << deerjikist.tag_id
|
||||
elsif !(Tag.where(id: kept_non_nico_tag_ids).where(category: :deerjikist).exists?)
|
||||
desired_non_nico_tag_ids << Tag.no_deerjikist.id
|
||||
desired_nico_tag_based_ids << Tag.no_deerjikist.id
|
||||
end
|
||||
|
||||
desired_nico_ids.uniq!
|
||||
desired_nico_tag_based_ids.uniq!
|
||||
|
||||
desired_all_ids = kept_non_nico_ids.to_a + desired_nico_ids
|
||||
desired_non_nico_ids.concat(kept_non_nico_ids.to_a)
|
||||
desired_non_nico_ids.uniq!
|
||||
if kept_non_nico_ids.to_set != desired_non_nico_ids.to_set
|
||||
desired_all_ids << Tag.bot.id
|
||||
desired_all_tag_ids = kept_non_nico_tag_ids.to_a + desired_nico_tag_based_ids
|
||||
desired_non_nico_tag_ids.concat(kept_non_nico_tag_ids.to_a)
|
||||
desired_non_nico_tag_ids.uniq!
|
||||
if kept_non_nico_tag_ids != desired_non_nico_tag_ids.to_set
|
||||
desired_all_tag_ids << Tag.bot.id
|
||||
end
|
||||
desired_all_ids.uniq!
|
||||
desired_all_tag_ids.uniq!
|
||||
|
||||
sync_post_tags!(post, desired_all_ids, current_ids: kept_ids)
|
||||
sync_post_tags!(post, desired_all_tag_ids, current_tag_ids: kept_tag_ids)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user