Files
redux-scraper/app/jobs/domain/fa/job/scan_fuzzysearch_job.rb
2025-07-25 04:20:40 +00:00

100 lines
2.8 KiB
Ruby

# typed: strict
# frozen_string_literal: true
class Domain::Fa::Job::ScanFuzzysearchJob < Domain::Fa::Job::Base
queue_as :fuzzysearch
sig { override.returns(Symbol) }
def self.http_factory_method
:get_fuzzysearch_http_client
end
sig { override.params(args: T::Hash[Symbol, T.untyped]).returns(T.untyped) }
def perform(args)
post_file = T.let(nil, T.nilable(Domain::PostFile))
post = post_from_args!
fs_client = Scraper::FuzzysearchApiClient.new(http_client)
logger.tagged(make_arg_tag(post)) do
if post.fuzzysearch_checked_at.present? && !force_scan?
logger.warn("fuzzysearch already checked, skipping")
return
end
fa_id = post.fa_id
if fa_id.nil?
logger.error("post has no fa_id, skipping")
return
end
unless post.state_removed?
logger.warn("post is not removed, skipping")
return
end
response = fs_client.search_fa_id_info(fa_id)
if response.is_a?(Symbol)
if response == :rate_limit_exceeded
sleep 2
fatal_error("fuzzysearch rate limit exceeded")
else
fatal_error("unknown fuzzysearch response: #{response}")
end
end
post.fuzzysearch_checked_at = Time.now
if response.is_a?(HttpLogEntry)
post.fuzzysearch_entry = response
logger.error("fuzzysearch query failed or returned no results")
return
end
post.fuzzysearch_entry = response.log_entry
post.fuzzysearch_json = response.json
if creator = post.creator
if creator.url_name != response.artist_url_name
fatal_error(
format_tags(
make_tag("existing", creator.url_name),
make_tag("fuzzysearch", response.artist_url_name),
"fuzzysearch artist url name mismatch",
),
)
end
else
url_name = response.artist_url_name
creator =
Domain::User::FaUser.find_or_initialize_by(url_name:) do |user|
# TODO - bug in has_aux_table, attributes not initialized before
# block is called
user.name ||= response.artist_name
user.full_name ||= response.artist_name
end
creator.save! if creator.new_record?
post.creator = creator
end
if post.keywords.blank? || post.keywords.empty?
post.keywords = response.tags
end
post_file = post.file
post_file ||=
post.build_file(url_str: response.file_url) do |post_file|
post_file.last_status_code = 404
post_file.state = "terminal_error"
end
if post_file.new_record?
post_file.enqueue_job_after_save(
Job::FaPostFurArchiverPostFileJob,
{ post_file: },
)
end
end
ensure
post.save! if post
post_file.save! if post_file
end
end