# 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", { 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, { headers: { "User-Agent" => "Websites/4.0.0 (https://github.com/DonovanDMC/Websites; \"donovan_dmc\")", }, }) 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? r = self.class.get(path, { headers: { "User-Agent" => "Websites/4.0.0 (https://github.com/DonovanDMC/Websites; \"donovan_dmc\")", }, }) JSON.parse(r.body)["posts"] || [] end def find_replacement(md5:) r = self.class.get("/post_replacements.json?search[md5]=#{md5}", { headers: { "User-Agent" => "Websites/4.0.0 (https://github.com/DonovanDMC/Websites; \"donovan_dmc\")", }, }) 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 end end