Donovan Daniels
b1c702e3cd
poorly tested but it worked well enough, I'm sure I'll be patching bugs over the next few weeks Also remove turbo because it sucks Also changed the way we handle hosts in dev
94 lines
2.7 KiB
Ruby
94 lines
2.7 KiB
Ruby
# 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", {
|
|
**Websites.config.httparty_options,
|
|
headers: {
|
|
**Websites.config.http_headers,
|
|
"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
|
|
r = self.class.get(path, Websites.config.httparty_options)
|
|
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?
|
|
Rails.logger.info("Fetching posts from e621: #{path}") if Rails.env.development?
|
|
r = self.class.get(path, Websites.config.httparty_options)
|
|
JSON.parse(r.body)["posts"] || []
|
|
end
|
|
|
|
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
|
|
|
|
def find_replacement(md5:)
|
|
r = self.class.get("/post_replacements.json?search[md5]=#{md5}", Websites.config.httparty_options)
|
|
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
|
|
|
|
def self.get_all_posts(**)
|
|
new.get_all_posts(**)
|
|
end
|
|
end
|
|
end
|