This commit is contained in:
2025-10-13 01:57:10 +09:00
parent 19a17e7ba7
commit 14f67b02e2
9 changed files with 171 additions and 54 deletions
+64 -36
View File
@@ -5,12 +5,30 @@ namespace :nico do
require 'open-uri'
require 'nokogiri'
fetch_thumbnail = -> url {
fetch_thumbnail = -> url do
html = URI.open(url, read_timeout: 60, 'User-Agent' => 'Mozilla/5.0').read
doc = Nokogiri::HTML(html)
doc.at('meta[name="thumbnail"]')&.[]('content').presence
}
end
def sync_post_tags! post, desired_tag_ids
desired_ids = desired_tag_ids.compact.to_set
current_ids = post.tags.pluck(:id).to_set
to_add = desired_ids - current_ids
to_remove = current_ids - desired_ids
Tag.where(id: to_add.to_a).find_each do |tag|
begin
PostTag.create!(post:, tag:)
rescue ActiveRecord::RecordNotUnique
;
end
end
PostTag.where(post_id: post.id, tag_id: to_remove.to_a).kept.find_each(&:discard!)
end
mysql_user = ENV['MYSQL_USER']
mysql_pass = ENV['MYSQL_PASS']
@@ -19,43 +37,53 @@ namespace :nico do
{ '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'] }"
thumbnail_base = fetch_thumbnail.(url) || '' rescue ''
post = Post.new(title:, url:, thumbnail_base:, uploaded_user: nil)
if thumbnail_base.present?
post.thumbnail.attach(
io: URI.open(thumbnail_base),
filename: File.basename(URI.parse(thumbnail_base).path),
content_type: 'image/jpeg')
end
post.save!
post.resized_thumbnail!
end
abort unless status.success?
current_tags = post.tags.where(category: 'nico').pluck(:name).sort
new_tags = datum['tags'].map { |tag| "nico:#{ tag }" }.sort
if current_tags != new_tags
post.tags.destroy(post.tags.where(name: current_tags))
tags_to_add = []
new_tags.each do |name|
tag = Tag.find_or_initialize_by(name:) do |t|
t.category = 'nico'
end
tags_to_add.concat([tag] + tag.linked_tags)
end
tags_to_add << Tag.tagme if post.tags.size < 20
tags_to_add << Tag.bot
post.tags = (post.tags + tags_to_add).uniq
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'] }"
thumbnail_base = fetch_thumbnail.(url) || '' rescue ''
post = Post.new(title:, url:, thumbnail_base:, uploaded_user: nil)
if thumbnail_base.present?
post.thumbnail.attach(
io: URI.open(thumbnail_base),
filename: File.basename(URI.parse(thumbnail_base).path),
content_type: 'image/jpeg')
end
post.save!
post.resized_thumbnail!
end
kept_tags = post.tags.reload
kept_non_nico_ids = kept_tags.where.not(category: 'nico').pluck(:id).to_set
desired_nico_ids = []
datum['tags'].each do |raw|
name = "nico:#{ raw }"
tag = Tag.find_or_initialize_by(name:) do |t|
t.category = 'nico'
end
tag.save! if tag.new_record?
desired_nico_ids << tag.id
desired_nico_ids.concat(tag.linked_tags.pluck(:id))
end
desired_nico_ids.uniq!
desired_extra_ids = []
desired_extra_ids << Tag.tagme.id if kept_tags.size < 10
desired_extra_ids << Tag.bot.id
desired_extra_ids.compact!
desired_extra_ids.uniq!
desired_all_ids = kept_non_nico_ids.to_a + desired_nico_ids + desired_extra_ids
desired_all_ids.uniq!
sync_post_tags!(post, desired_all_ids)
end
end
end