This commit is contained in:
@@ -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,16 +47,37 @@ 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]|$)")
|
|
||||||
|
post =
|
||||||
|
Post
|
||||||
|
.where('url REGEXP ?', "nicovideo\\.jp/watch/#{ Regexp.escape(code) }([^0-9]|$)")
|
||||||
.first
|
.first
|
||||||
|
|
||||||
title = datum['title']
|
title = datum['title']
|
||||||
original_created_at = datum['uploaded_at'] &&
|
original_created_at = datum['uploaded_at'] &&
|
||||||
Time.strptime(datum['uploaded_at'], '%Y-%m-%d %H:%M:%S')
|
Time.strptime(datum['uploaded_at'], '%Y-%m-%d %H:%M:%S')
|
||||||
original_created_from = original_created_at&.change(sec: 0)
|
original_created_from = original_created_at&.change(sec: 0)
|
||||||
original_created_before = original_created_from&.+(1.minute)
|
original_created_before = original_created_from&.+(1.minute)
|
||||||
|
|
||||||
if post
|
if post
|
||||||
attrs = { title:, original_created_from:, original_created_before: }
|
attrs = { title:, original_created_from:, original_created_before: }
|
||||||
post.update!(attrs) if attrs.any? { |k, v| post.public_send(k) != v }
|
|
||||||
|
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
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user