add e621 auth
This commit is contained in:
parent
0f2acc808c
commit
1acaa8114e
|
@ -5,15 +5,19 @@ module Requests
|
||||||
include HTTParty
|
include HTTParty
|
||||||
base_uri "https://e621.net"
|
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
|
def status
|
||||||
r = self.class.get("/posts.json?limit=0", {
|
r = self.class.get("/posts.json?limit=0", options.deep_merge({
|
||||||
**Websites.config.httparty_options,
|
|
||||||
headers: {
|
headers: {
|
||||||
**Websites.config.http_headers,
|
|
||||||
"User-Agent" => "E621Status/1.0.0 (https://status.e621.ws; \"donovan_dmc\")",
|
"User-Agent" => "E621Status/1.0.0 (https://status.e621.ws; \"donovan_dmc\")",
|
||||||
},
|
},
|
||||||
timeout: 5,
|
timeout: 5,
|
||||||
})
|
}))
|
||||||
status = r.code
|
status = r.code
|
||||||
if r.code == 503 && r.headers["Content-Type"]&.start_with?("text/html")
|
if r.code == 503 && r.headers["Content-Type"]&.start_with?("text/html")
|
||||||
status = 1
|
status = 1
|
||||||
|
@ -35,11 +39,28 @@ module Requests
|
||||||
path = "/"
|
path = "/"
|
||||||
path = "/posts/#{id}.json" if id
|
path = "/posts/#{id}.json" if id
|
||||||
path = "/posts.json?md5=#{md5}" if md5
|
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
|
return nil if r.code != 200
|
||||||
JSON.parse(r.body)["post"]
|
JSON.parse(r.body)["post"]
|
||||||
end
|
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)
|
def get_posts(ids: nil, md5s: nil, status: nil)
|
||||||
raise(ArgumentError, "ids or md5s must be given") if ids.nil? && md5s.nil?
|
raise(ArgumentError, "ids or md5s must be given") if ids.nil? && md5s.nil?
|
||||||
path = "/posts.json?"
|
path = "/posts.json?"
|
||||||
|
@ -48,7 +69,7 @@ module Requests
|
||||||
path += "%20status:#{status}" if path.include?("tags=") && !status.nil?
|
path += "%20status:#{status}" if path.include?("tags=") && !status.nil?
|
||||||
path += "tags=status:#{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?
|
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"] || []
|
JSON.parse(r.body)["posts"] || []
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -70,7 +91,7 @@ module Requests
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_replacement(md5:)
|
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
|
JSON.parse(r.body)&.first
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -89,5 +110,17 @@ module Requests
|
||||||
def self.get_all_posts(**)
|
def self.get_all_posts(**)
|
||||||
new.get_all_posts(**)
|
new.get_all_posts(**)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -254,6 +254,12 @@ module Websites
|
||||||
base_url: yiffy2_cdn_url,
|
base_url: yiffy2_cdn_url,
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def e621_username
|
||||||
|
end
|
||||||
|
|
||||||
|
def e621_apikey
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class EnvironmentConfiguration
|
class EnvironmentConfiguration
|
||||||
|
|
Loading…
Reference in New Issue
Block a user