113 lines
3.1 KiB
Ruby
113 lines
3.1 KiB
Ruby
# typed: true
|
|
class Domain::Fa::Job::ScanPostJob < Domain::Fa::Job::Base
|
|
queue_as :fa_post
|
|
|
|
def perform(args)
|
|
@post =
|
|
args[:post] ||
|
|
begin
|
|
Domain::Fa::Post.find_or_initialize_by(fa_id: args[:fa_id])
|
|
end
|
|
|
|
@caused_by_entry = args[:caused_by_entry]
|
|
@force_scan = !!args[:force_scan]
|
|
logger.prefix =
|
|
proc { "[fa_id #{@post.fa_id.to_s.bold} / #{@post.state.bold}]" }
|
|
|
|
if @post.state == "ok" && !@post.scanned? || @force_scan
|
|
if (@post.state_detail["404_count"] || 0) > 2
|
|
logger.info "too many 404s, skipping"
|
|
return
|
|
end
|
|
|
|
scan_post
|
|
end
|
|
|
|
if (@post.state == "ok" && @post.file_uri && @post.file.nil?) || @force_scan
|
|
logger.info("enqueue file job (#{self.priority})")
|
|
defer_job(
|
|
Domain::Fa::Job::ScanFileJob,
|
|
{ post: @post, caused_by_entry: @submission_entry || @caused_by_entry },
|
|
{ priority: self.priority },
|
|
)
|
|
end
|
|
|
|
enqueue_user_scan(@post.creator, @submission_entry) if @post.creator
|
|
ensure
|
|
if @submission_entry && @submission_entry.status_code == 200
|
|
enqueue_jobs_from_found_links(
|
|
@submission_entry,
|
|
suppress_jobs: [{ job: self.class, fa_id: @post.fa_id }],
|
|
)
|
|
end
|
|
logger.info "finished post scan"
|
|
end
|
|
|
|
private
|
|
|
|
def scan_post
|
|
response =
|
|
http_client.get(
|
|
"https://www.furaffinity.net/view/#{@post.fa_id}/",
|
|
caused_by_entry: @caused_by_entry,
|
|
)
|
|
if response.status_code != 200
|
|
fatal_error(
|
|
"error scanning fa_id #{@post.fa_id}: #{response.status_code}, log entry #{response.log_entry.id}",
|
|
)
|
|
end
|
|
|
|
@submission_entry = response.log_entry
|
|
|
|
page = Domain::Fa::Parser::Page.new(response.body)
|
|
if page.submission_not_found?
|
|
logger.error("post was removed")
|
|
@post.state = :removed
|
|
@post.save!
|
|
return
|
|
end
|
|
|
|
unless page.probably_submission?
|
|
if response.body =~
|
|
/The page you are trying to reach is currently pending deletion/
|
|
logger.error("post is pending deletion")
|
|
@post.state = :removed
|
|
@post.save!
|
|
return
|
|
else
|
|
fatal_error("not a submission page")
|
|
end
|
|
end
|
|
|
|
submission = page.submission
|
|
|
|
unless submission.id.to_i == @post.fa_id
|
|
raise("id mismatch: #{submission.id} != #{@post.fa_id}")
|
|
end
|
|
|
|
@post.last_submission_page = @submission_entry
|
|
@post.title = submission.title
|
|
@post.creator =
|
|
Domain::Fa::User.find_or_build_from_submission_parser(submission)
|
|
@post.category = submission.category
|
|
@post.description =
|
|
submission.description_html.encode(
|
|
"UTF-8",
|
|
invalid: :replace,
|
|
undef: :replace,
|
|
)
|
|
@post.keywords = submission.keywords_array
|
|
@post.file_uri = submission.full_res_img
|
|
@post.theme = submission.theme
|
|
@post.species = submission.species
|
|
@post.gender = submission.gender
|
|
@post.num_favorites = submission.num_favorites
|
|
@post.num_comments = submission.num_comments
|
|
@post.num_views = submission.num_views
|
|
@post.posted_at = submission.posted_date
|
|
@post.scanned_at = Time.now
|
|
|
|
@post.save!
|
|
end
|
|
end
|