diff --git a/app/logical/mastodon_api_client.rb b/app/logical/mastodon_api_client.rb new file mode 100644 index 000000000..e11f7d98e --- /dev/null +++ b/app/logical/mastodon_api_client.rb @@ -0,0 +1,80 @@ +class MastodonApiClient + extend Memoist + attr_reader :json + + def initialize(site_name, id) + @site_name = site_name + + return if access_token.blank? + + begin + @json = JSON.parse(access_token.get("/api/v1/statuses/#{id}").body) + rescue StandardError + @json = {} + end + end + + def profile_url + json.dig("account", "url") + end + + def account_name + json.dig("account", "username") + end + + def display_name + json.dig("account", "display_name") + end + + def account_id + json.dig("account", "id") + end + + def image_url + image_urls.first + end + + def image_urls + json["media_attachments"].to_a.map {|x| x["url"]} + end + + def tags + json["tags"].to_a.map { |tag| [tag["name"], tag["url"]] } + end + + def commentary + commentary = "" + commentary << "
#{json["spoiler_text"]}
" if json["spoiler_text"].present? + commentary << json["content"] + commentary + end + + def to_h + json + end + + def fetch_access_token + Cache.get("#{@site_name}-token") do + result = client.client_credentials.get_token + result.token + end + end + + def access_token + return if client.blank? + OAuth2::AccessToken.new(client, fetch_access_token) + end + + def client + if @site_name == "pawoo.net" + client_id = Danbooru.config.pawoo_client_id + client_secret = Danbooru.config.pawoo_client_secret + end + + return unless client_id && client_secret + + OAuth2::Client.new(client_id, client_secret, :site => "https://#{@site_name}") + end + + memoize :client +end diff --git a/app/logical/pawoo_api_client.rb b/app/logical/pawoo_api_client.rb deleted file mode 100644 index f74d42e38..000000000 --- a/app/logical/pawoo_api_client.rb +++ /dev/null @@ -1,156 +0,0 @@ -class PawooApiClient - extend Memoist - - PROFILE1 = %r!\Ahttps?://pawoo\.net/web/accounts/(\d+)! - PROFILE2 = %r!\Ahttps?://pawoo\.net/@([^/]+)! - STATUS1 = %r!\Ahttps?://pawoo\.net/web/statuses/(\d+)! - STATUS2 = %r!\Ahttps?://pawoo\.net/@.+?/([^/]+)! - - class MissingConfigurationError < StandardError; end - - class Account - attr_reader :json - - def self.is_match?(url) - if url =~ PROFILE1 - return $1 - end - - if url =~ PROFILE2 - return $1 - end - - false - end - - def initialize(json) - @json = json - end - - def profile_url - json["url"] - end - - def account_name - json["username"] - end - - def image_url - nil - end - - def image_urls - [] - end - - def tags - [] - end - - def commentary - nil - end - - def to_h - json - end - end - - class Status - attr_reader :json - - def self.is_match?(url) - if url =~ STATUS1 - return $1 - end - - if url =~ STATUS2 - return $1 - end - - false - end - - def initialize(json) - @json = json - end - - def profile_url - json["account"]["url"] - end - - def account_name - json["account"]["username"] - end - - def image_url - image_urls.first - end - - def image_urls - json["media_attachments"].map {|x| x["url"]} - end - - def tags - json["tags"].map { |tag| [tag["name"], tag["url"]] } - end - - def commentary - commentary = "" - commentary << "#{json["spoiler_text"]}
" if json["spoiler_text"].present? - commentary << json["content"] - commentary - end - - def to_h - json - end - end - - def get(url) - if id = Status.is_match?(url) - begin - data = JSON.parse(access_token.get("/api/v1/statuses/#{id}").body) - rescue - data = { - "account" => {}, - "media_attachments" => [], - "tags" => [], - "content" => "", - } - end - return Status.new(data) - end - - if id = Account.is_match?(url) - begin - data = JSON.parse(access_token.get("/api/v1/accounts/#{id}").body) - rescue - data = {} - end - Account.new(data) - end - end - - private - - def fetch_access_token - raise MissingConfigurationError, "missing pawoo client id" if Danbooru.config.pawoo_client_id.nil? - raise MissingConfigurationError, "missing pawoo client secret" if Danbooru.config.pawoo_client_secret.nil? - - Cache.get("pawoo-token") do - result = client.client_credentials.get_token - result.token - end - end - - def access_token - OAuth2::AccessToken.new(client, fetch_access_token) - end - - def client - OAuth2::Client.new(Danbooru.config.pawoo_client_id, Danbooru.config.pawoo_client_secret, :site => "https://pawoo.net") - end - - memoize :client -end diff --git a/app/logical/sources/strategies.rb b/app/logical/sources/strategies.rb index fddc1fdd3..895a9a6bd 100644 --- a/app/logical/sources/strategies.rb +++ b/app/logical/sources/strategies.rb @@ -11,7 +11,7 @@ module Sources Strategies::Tumblr, Strategies::ArtStation, Strategies::Nijie, - Strategies::Pawoo, + Strategies::Mastodon, Strategies::Moebooru, Strategies::HentaiFoundry, Strategies::Weibo, diff --git a/app/logical/sources/strategies/pawoo.rb b/app/logical/sources/strategies/mastodon.rb similarity index 64% rename from app/logical/sources/strategies/pawoo.rb rename to app/logical/sources/strategies/mastodon.rb index f170eb100..a5b807f85 100644 --- a/app/logical/sources/strategies/pawoo.rb +++ b/app/logical/sources/strategies/mastodon.rb @@ -15,25 +15,28 @@ # * https://pawoo.net/oauth_authentications/17230064 module Sources::Strategies - class Pawoo < Base - HOST = %r{\Ahttps?://(www\.)?pawoo\.net}i + class Mastodon < Base + HOST = %r{\Ahttps?://(?:www\.)?(?