diff --git a/app/controllers/api/v1/posts_controller.rb b/app/controllers/api/v1/posts_controller.rb index c398e1b..6ddd8a3 100644 --- a/app/controllers/api/v1/posts_controller.rb +++ b/app/controllers/api/v1/posts_controller.rb @@ -87,7 +87,7 @@ class Api::V1::PostsController < Api::V1::ApplicationController end def process_post - if !@post.processed && @post.type != 'WebPage' + if !@post.processed ProcessPostWorker.perform_async @post.url end end diff --git a/app/workers/process_post_worker.rb b/app/workers/process_post_worker.rb index a784b95..d7b0eb6 100644 --- a/app/workers/process_post_worker.rb +++ b/app/workers/process_post_worker.rb @@ -13,14 +13,13 @@ class ProcessPostWorker if page.images.best.present? post.create_thumbnail file: URI.parse(page.images.best) end + post.save rescue SocketError => e Rails.logger.error "出错了: #{e}" + ensure readability_extract_content post end - post.save - readability_extract_content post - end private @@ -31,8 +30,9 @@ class ProcessPostWorker response = RestClient.get "https://readability.com/api/content/v1/parser?token=#{token}&url=#{post.url}" if response.code == 200 post_json = JSON.parse response.body + post.content = HTMLEntities.new.decode post_json['content'] - post.good_format = true + post.good_format = true if post.type != 'WebPage' post.title = post_json['title'] post.save end