121 lines
4.3 KiB
Ruby
121 lines
4.3 KiB
Ruby
# typed: false
|
|
module Domain::Inkbunny::Job
|
|
class UpdatePostsJob < Base
|
|
def perform(args)
|
|
caused_by_entry = args[:caused_by_entry]
|
|
ib_post_ids = args[:ib_post_ids]
|
|
|
|
if ib_post_ids.empty?
|
|
logger.info "empty ib_post_ids"
|
|
return
|
|
end
|
|
|
|
ib_post_ids.each_slice(100) do |ib_post_ids_chunk|
|
|
process_ib_post_ids_chunk(
|
|
ib_post_ids_chunk,
|
|
caused_by_entry: caused_by_entry,
|
|
)
|
|
end
|
|
end
|
|
|
|
def build_api_submissions_url(ib_post_ids_chunk)
|
|
ib_post_ids_list = ib_post_ids_chunk.join(",")
|
|
"https://inkbunny.net/api_submissions.php?" +
|
|
"submission_ids=#{ib_post_ids_list}" +
|
|
"&show_description=yes&show_writing=yes&show_pools=yes"
|
|
end
|
|
|
|
def process_ib_post_ids_chunk(ib_post_ids_chunk, caused_by_entry:)
|
|
url = build_api_submissions_url(ib_post_ids_chunk)
|
|
response = http_client.get(url, caused_by_entry: caused_by_entry)
|
|
if response.status_code != 200
|
|
fatal_error("api_submissions failed: #{response.status_code}")
|
|
end
|
|
api_submissions_json = JSON.parse(response.body)
|
|
submissions = api_submissions_json["submissions"]
|
|
logger.info("api_submissions page has #{submissions.size} posts")
|
|
submissions.each do |submission_json|
|
|
Domain::Inkbunny::Post.transaction do
|
|
deep_update_post_from_submission_json(
|
|
submission_json,
|
|
caused_by_entry: response.log_entry,
|
|
)
|
|
end
|
|
end
|
|
end
|
|
|
|
def deep_update_post_from_submission_json(submission_json, caused_by_entry:)
|
|
post =
|
|
Domain::Inkbunny::Post.find_by!(
|
|
ib_post_id: submission_json["submission_id"],
|
|
)
|
|
logger.info "deep update post #{post.ib_post_id.to_s.bold}"
|
|
post.deep_updated_at = Time.now
|
|
post.description = submission_json["description"]
|
|
post.writing = submission_json["writing"]
|
|
post.rating = submission_json["rating"]
|
|
post.submission_type = submission_json["submission_type"]
|
|
post.num_views = submission_json["views"]
|
|
post.num_files = submission_json["pagecount"]
|
|
post.num_favs = submission_json["favorites_count"]&.to_i
|
|
post.num_comments = submission_json["comments_count"]&.to_i
|
|
post.last_file_updated_at =
|
|
Time.parse(submission_json["last_file_update_datetime"])
|
|
post.keywords = submission_json["keywords"]
|
|
post.deep_update_log_entry = caused_by_entry
|
|
|
|
if submission_json["user_icon_url_large"]
|
|
user = post.creator
|
|
user.avatar_url_str = submission_json["user_icon_url_large"]
|
|
if user.avatar_url_str_changed?
|
|
user.deep_update_log_entry = caused_by_entry
|
|
logger.info "avatar url changed, enqueuing download for user #{user.name}"
|
|
defer_job(
|
|
Domain::Inkbunny::Job::UserAvatarJob,
|
|
{ user: user, caused_by_entry: caused_by_entry },
|
|
)
|
|
end
|
|
user.save!
|
|
end
|
|
|
|
post_files_by_md5 = post.files.index_by(&:md5_initial)
|
|
file_jsons = submission_json["files"] || fatal_error("no files[] array")
|
|
file_jsons.each do |file_json|
|
|
md5_initial = file_json["initial_file_md5"]
|
|
next if post_files_by_md5[md5_initial]
|
|
|
|
file =
|
|
post.files.create!(
|
|
{
|
|
ib_file_id: file_json["file_id"]&.to_i,
|
|
ib_created_at: Time.parse(file_json["create_datetime"]),
|
|
file_order: file_json["submission_file_order"]&.to_i,
|
|
ib_detail_raw: file_json,
|
|
file_name: file_json["file_name"],
|
|
url_str: file_json["file_url_full"],
|
|
md5_initial: md5_initial,
|
|
md5_full: file_json["full_file_md5"],
|
|
md5s: {
|
|
initial_file_md5: md5_initial,
|
|
full_file_md5: file_json["full_file_md5"],
|
|
large_file_md5: file_json["large_file_md5"],
|
|
small_file_md5: file_json["small_file_md5"],
|
|
thumbnail_md5: file_json["thumbnail_md5"],
|
|
},
|
|
},
|
|
)
|
|
|
|
logger.info "[ib_post_id #{post.ib_post_id.to_s.bold}] " +
|
|
"new file #{file.ib_file_id.to_s.bold} - #{file.file_name.black.bold}"
|
|
|
|
defer_job(
|
|
Domain::Inkbunny::Job::FileJob,
|
|
{ file: file, caused_by_entry: caused_by_entry },
|
|
{ priority: 1 },
|
|
)
|
|
end
|
|
post.save!
|
|
end
|
|
end
|
|
end
|