2024-05-03 03:04:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Requests
|
|
|
|
class E621
|
|
|
|
include HTTParty
|
|
|
|
base_uri "https://e621.net"
|
|
|
|
|
2024-05-07 18:33:06 +00:00
|
|
|
def options
|
|
|
|
opt = Websites.config.httparty_options
|
|
|
|
opt[:headers]["Authorization"] = "Basic #{Base64.strict_encode64("#{Websites.config.e621_username}:#{Websites.config.e621_apikey}")}" if Websites.config.e621_username.present? && Websites.config.e621_apikey.present?
|
|
|
|
opt
|
|
|
|
end
|
|
|
|
|
2024-05-03 03:04:43 +00:00
|
|
|
def status
|
2024-05-07 18:33:06 +00:00
|
|
|
r = self.class.get("/posts.json?limit=0", options.deep_merge({
|
2024-05-03 03:04:43 +00:00
|
|
|
headers: {
|
|
|
|
"User-Agent" => "E621Status/1.0.0 (https://status.e621.ws; \"donovan_dmc\")",
|
|
|
|
},
|
|
|
|
timeout: 5,
|
2024-05-07 18:33:06 +00:00
|
|
|
}))
|
2024-05-03 03:04:43 +00:00
|
|
|
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-07 18:33:06 +00:00
|
|
|
r = self.class.get(path, options)
|
2024-05-03 03:04:43 +00:00
|
|
|
return nil if r.code != 200
|
|
|
|
JSON.parse(r.body)["post"]
|
|
|
|
end
|
|
|
|
|
2024-05-07 18:33:06 +00:00
|
|
|
def get_posts_by_tags(tags:, limit: 100, page: 1)
|
|
|
|
path = "/posts.json?tags=#{tags}&limit=#{limit}&page=#{page}"
|
|
|
|
r = self.class.get(path, options)
|
|
|
|
JSON.parse(r.body)["posts"] || []
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_all_posts_by_tags(tags:)
|
|
|
|
posts = []
|
|
|
|
page = 1
|
|
|
|
loop do
|
|
|
|
posts += p = get_posts_by_tags(tags: tags, limit: 320, page: page)
|
|
|
|
break if p.length < 320
|
|
|
|
page += 1
|
|
|
|
end
|
|
|
|
posts
|
|
|
|
end
|
|
|
|
|
2024-05-03 03:04:43 +00:00
|
|
|
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?
|
2024-05-07 18:33:06 +00:00
|
|
|
r = self.class.get(path, 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-07 18:33:06 +00:00
|
|
|
r = self.class.get("/post_replacements.json?search[md5]=#{md5}", options)
|
2024-05-03 03:04:43 +00:00
|
|
|
JSON.parse(r.body)&.first
|
|
|
|
end
|
|
|
|
|
2024-06-04 19:48:30 +00:00
|
|
|
def name_to_id(name)
|
|
|
|
Cache.fetch("name_to_id:#{name.downcase}", expires_in: 1.day) do
|
|
|
|
r = self.class.get("/users.json?search[name_matches]=#{name}", options)
|
|
|
|
JSON.parse(r.body)&.first&.dig("id")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-03 03:04:43 +00:00
|
|
|
def self.status
|
|
|
|
new.status
|
|
|
|
end
|
|
|
|
|
2024-06-04 19:48:30 +00:00
|
|
|
def self.get_post(...)
|
|
|
|
new.get_post(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_posts(...)
|
|
|
|
new.get_posts(...)
|
2024-05-03 03:04:43 +00:00
|
|
|
end
|
|
|
|
|
2024-06-04 19:48:30 +00:00
|
|
|
def self.get_all_posts(...)
|
|
|
|
new.get_all_posts(...)
|
2024-05-03 03:04:43 +00:00
|
|
|
end
|
2024-05-06 07:47:53 +00:00
|
|
|
|
2024-06-04 19:48:30 +00:00
|
|
|
def self.get_posts_by_tags(...)
|
|
|
|
new.get_posts_by_tags(...)
|
2024-05-06 07:47:53 +00:00
|
|
|
end
|
2024-05-07 18:33:06 +00:00
|
|
|
|
2024-06-04 19:48:30 +00:00
|
|
|
def self.get_all_posts_by_tags(...)
|
|
|
|
new.get_all_posts_by_tags(...)
|
2024-05-07 18:33:06 +00:00
|
|
|
end
|
|
|
|
|
2024-06-04 19:48:30 +00:00
|
|
|
def self.find_replacement(...)
|
|
|
|
new.find_replacement(...)
|
2024-05-07 18:33:06 +00:00
|
|
|
end
|
|
|
|
|
2024-06-04 19:48:30 +00:00
|
|
|
def self.name_to_id(...)
|
|
|
|
new.name_to_id(...)
|
2024-05-07 18:33:06 +00:00
|
|
|
end
|
2024-05-03 03:04:43 +00:00
|
|
|
end
|
|
|
|
end
|