2024-05-03 03:04:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Requests
|
|
|
|
class E621
|
|
|
|
include HTTParty
|
|
|
|
base_uri "https://e621.net"
|
|
|
|
|
|
|
|
def status
|
|
|
|
r = self.class.get("/posts.json?limit=0", {
|
2024-05-06 07:47:53 +00:00
|
|
|
**Websites.config.httparty_options,
|
2024-05-03 03:04:43 +00:00
|
|
|
headers: {
|
2024-05-06 07:47:53 +00:00
|
|
|
**Websites.config.http_headers,
|
2024-05-03 03:04:43 +00:00
|
|
|
"User-Agent" => "E621Status/1.0.0 (https://status.e621.ws; \"donovan_dmc\")",
|
|
|
|
},
|
|
|
|
timeout: 5,
|
|
|
|
})
|
|
|
|
status = r.code
|
|
|
|
if r.code == 503 && r.headers["Content-Type"]&.start_with?("text/html")
|
|
|
|
status = 1
|
|
|
|
elsif r.code == 501
|
|
|
|
status = 200
|
|
|
|
Rails.logger.warn("E621 status check returned 501, ignoring")
|
|
|
|
end
|
|
|
|
status
|
|
|
|
rescue Net::OpenTimeout
|
|
|
|
Rails.logger.warn("E621 status check timed out")
|
|
|
|
408
|
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.warn("E621 status check failed: #{e}")
|
|
|
|
0
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_post(id: nil, md5: nil)
|
|
|
|
raise(ArgumentError, "id or md5 must be given") if id.nil? && md5.nil?
|
|
|
|
path = "/"
|
|
|
|
path = "/posts/#{id}.json" if id
|
|
|
|
path = "/posts.json?md5=#{md5}" if md5
|
2024-05-06 07:47:53 +00:00
|
|
|
r = self.class.get(path, Websites.config.httparty_options)
|
2024-05-03 03:04:43 +00:00
|
|
|
return nil if r.code != 200
|
|
|
|
JSON.parse(r.body)["post"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_posts(ids: nil, md5s: nil, status: nil)
|
|
|
|
raise(ArgumentError, "ids or md5s must be given") if ids.nil? && md5s.nil?
|
|
|
|
path = "/posts.json?"
|
|
|
|
path += "tags=id:#{ids.join(',')}%20limit:100" if ids
|
|
|
|
path += "tags=md5:#{md5s.join(',')}%20limit:100" if md5s
|
|
|
|
path += "%20status:#{status}" if path.include?("tags=") && !status.nil?
|
|
|
|
path += "tags=status:#{status}" if !path.include?("tags=") && !status.nil?
|
2024-05-06 07:47:53 +00:00
|
|
|
Rails.logger.info("Fetching posts from e621: #{path}") if Rails.env.development?
|
|
|
|
r = self.class.get(path, Websites.config.httparty_options)
|
2024-05-03 03:04:43 +00:00
|
|
|
JSON.parse(r.body)["posts"] || []
|
|
|
|
end
|
|
|
|
|
2024-05-06 07:47:53 +00:00
|
|
|
def get_all_posts(ids: [], md5s: [])
|
|
|
|
posts = []
|
|
|
|
ids.each_slice(100) do |slice|
|
|
|
|
posts += get_posts(ids: slice, status: "any")
|
|
|
|
end
|
|
|
|
md5s -= posts.map { |p| p["file"]["md5"] }
|
|
|
|
md5s.each_slice(100) do |slice|
|
|
|
|
posts += get_posts(md5s: slice, status: "any")
|
|
|
|
end
|
|
|
|
missing = {
|
|
|
|
ids: ids - posts.pluck("id"),
|
|
|
|
md5s: md5s - posts.map { |p| p["file"]["md5"] },
|
|
|
|
}
|
|
|
|
|
|
|
|
{ posts: posts, missing: missing }
|
|
|
|
end
|
|
|
|
|
2024-05-03 03:04:43 +00:00
|
|
|
def find_replacement(md5:)
|
2024-05-06 07:47:53 +00:00
|
|
|
r = self.class.get("/post_replacements.json?search[md5]=#{md5}", Websites.config.httparty_options)
|
2024-05-03 03:04:43 +00:00
|
|
|
JSON.parse(r.body)&.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.status
|
|
|
|
new.status
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_post(**)
|
|
|
|
new.get_post(**)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_posts(**)
|
|
|
|
new.get_posts(**)
|
|
|
|
end
|
2024-05-06 07:47:53 +00:00
|
|
|
|
|
|
|
def self.get_all_posts(**)
|
|
|
|
new.get_all_posts(**)
|
|
|
|
end
|
2024-05-03 03:04:43 +00:00
|
|
|
end
|
|
|
|
end
|