サムネ再取得(#193) (#250)
#193 Co-authored-by: miteruzo <miteruzo@naver.com> Reviewed-on: #250
This commit was merged in pull request #250.
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
namespace :nico do
|
||||
desc 'ニコニコ DB 同期'
|
||||
task sync: :environment do
|
||||
require 'json'
|
||||
require 'open3'
|
||||
require 'open-uri'
|
||||
require 'nokogiri'
|
||||
require 'set'
|
||||
require 'time'
|
||||
|
||||
fetch_thumbnail = -> url do
|
||||
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.each do |datum|
|
||||
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
|
||||
|
||||
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: }
|
||||
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
|
||||
url = "https://www.nicovideo.jp/watch/#{ code }"
|
||||
thumbnail_base = fetch_thumbnail.(url) rescue nil
|
||||
|
||||
Reference in New Issue
Block a user