|
|
@@ -1,10 +1,12 @@ |
|
|
namespace :nico do |
|
|
namespace :nico do |
|
|
desc 'ニコニコ DB 同期' |
|
|
desc 'ニコニコ DB 同期' |
|
|
task sync: :environment do |
|
|
task sync: :environment do |
|
|
|
|
|
require 'json' |
|
|
require 'open3' |
|
|
require 'open3' |
|
|
require 'open-uri' |
|
|
require 'open-uri' |
|
|
require 'nokogiri' |
|
|
require 'nokogiri' |
|
|
require 'set' |
|
|
require 'set' |
|
|
|
|
|
require 'time' |
|
|
|
|
|
|
|
|
fetch_thumbnail = -> url do |
|
|
fetch_thumbnail = -> url do |
|
|
html = URI.open(url, read_timeout: 60, 'User-Agent' => 'Mozilla/5.0').read |
|
|
html = URI.open(url, read_timeout: 60, 'User-Agent' => 'Mozilla/5.0').read |
|
|
@@ -45,13 +47,42 @@ namespace :nico do |
|
|
data = JSON.parse(stdout) |
|
|
data = JSON.parse(stdout) |
|
|
data.each do |datum| |
|
|
data.each do |datum| |
|
|
code = datum['code'] |
|
|
code = datum['code'] |
|
|
post = Post.where('url REGEXP ?', "nicovideo\\.jp/watch/#{ Regexp.escape(code) }([^0-9]|$)") |
|
|
|
|
|
.first |
|
|
|
|
|
unless post |
|
|
|
|
|
title = datum['title'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
post = |
|
|
|
|
|
Post |
|
|
|
|
|
.where('url REGEXP ?', "nicovideo\\.jp/watch/#{ Regexp.escape(code) }([^0-9]|$)") |
|
|
|
|
|
.first |
|
|
|
|
|
|
|
|
|
|
|
title = datum['title'] |
|
|
|
|
|
original_created_at = datum['uploaded_at'] && |
|
|
|
|
|
Time.strptime(datum['uploaded_at'], '%Y-%m-%d %H:%M:%S') |
|
|
|
|
|
original_created_from = original_created_at&.change(sec: 0) |
|
|
|
|
|
original_created_before = original_created_from&.+(1.minute) |
|
|
|
|
|
|
|
|
|
|
|
if post |
|
|
|
|
|
attrs = { title:, original_created_from:, original_created_before: } |
|
|
|
|
|
|
|
|
|
|
|
unless post.thumbnail.attached? |
|
|
|
|
|
thumbnail_base = fetch_thumbnail.(post.url) rescue 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') |
|
|
|
|
|
attrs[:thumbnail_base] = thumbnail_base |
|
|
|
|
|
end |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
post.assign_attributes(attrs) |
|
|
|
|
|
if post.changed? |
|
|
|
|
|
post.save! |
|
|
|
|
|
post.resized_thumbnail! if post.thumbnail.attached? |
|
|
|
|
|
end |
|
|
|
|
|
else |
|
|
url = "https://www.nicovideo.jp/watch/#{ code }" |
|
|
url = "https://www.nicovideo.jp/watch/#{ code }" |
|
|
thumbnail_base = fetch_thumbnail.(url) rescue nil |
|
|
thumbnail_base = fetch_thumbnail.(url) rescue nil |
|
|
post = Post.new(title:, url:, thumbnail_base:, uploaded_user: nil) |
|
|
|
|
|
|
|
|
post = Post.new(title:, url:, thumbnail_base:, uploaded_user: nil, |
|
|
|
|
|
original_created_from:, original_created_before:) |
|
|
if thumbnail_base.present? |
|
|
if thumbnail_base.present? |
|
|
post.thumbnail.attach( |
|
|
post.thumbnail.attach( |
|
|
io: URI.open(thumbnail_base), |
|
|
io: URI.open(thumbnail_base), |
|
|
@@ -60,7 +91,7 @@ namespace :nico do |
|
|
end |
|
|
end |
|
|
post.save! |
|
|
post.save! |
|
|
post.resized_thumbnail! |
|
|
post.resized_thumbnail! |
|
|
sync_post_tags!(post, [Tag.tagme.id]) |
|
|
|
|
|
|
|
|
sync_post_tags!(post, [Tag.tagme.id, Tag.bot.id, Tag.niconico.id, Tag.video.id]) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
kept_ids = PostTag.kept.where(post_id: post.id).pluck(:tag_id).to_set |
|
|
kept_ids = PostTag.kept.where(post_id: post.id).pluck(:tag_id).to_set |
|
|
|