add e621 auth

This commit is contained in:
Donovan Daniels 2024-05-07 13:33:06 -05:00
parent 0f2acc808c
commit 1acaa8114e
Signed by: Donovan_DMC
GPG Key ID: 907D29CBFD6157BA
2 changed files with 46 additions and 7 deletions

View File

@ -5,15 +5,19 @@ module Requests
include HTTParty
base_uri "https://e621.net"
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
def status
r = self.class.get("/posts.json?limit=0", {
**Websites.config.httparty_options,
r = self.class.get("/posts.json?limit=0", options.deep_merge({
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
@ -35,11 +39,28 @@ module Requests
path = "/"
path = "/posts/#{id}.json" if id
path = "/posts.json?md5=#{md5}" if md5
r = self.class.get(path, Websites.config.httparty_options)
r = self.class.get(path, options)
return nil if r.code != 200
JSON.parse(r.body)["post"]
end
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
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?"
@ -48,7 +69,7 @@ module Requests
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)
r = self.class.get(path, options)
JSON.parse(r.body)["posts"] || []
end
@ -70,7 +91,7 @@ module Requests
end
def find_replacement(md5:)
r = self.class.get("/post_replacements.json?search[md5]=#{md5}", Websites.config.httparty_options)
r = self.class.get("/post_replacements.json?search[md5]=#{md5}", options)
JSON.parse(r.body)&.first
end
@ -89,5 +110,17 @@ module Requests
def self.get_all_posts(**)
new.get_all_posts(**)
end
def self.get_posts_by_tags(**)
new.get_posts_by_tags(**)
end
def self.get_all_posts_by_tags(**)
new.get_all_posts_by_tags(**)
end
def self.find_replacement(**)
new.find_replacement(**)
end
end
end

View File

@ -254,6 +254,12 @@ module Websites
base_url: yiffy2_cdn_url,
)
end
def e621_username
end
def e621_apikey
end
end
class EnvironmentConfiguration