sources: factor out Source::URL::Mastodon.
This commit is contained in:
@@ -21,6 +21,7 @@ module Source
|
||||
Source::URL::Twitter,
|
||||
Source::URL::HentaiFoundry,
|
||||
Source::URL::Lofter,
|
||||
Source::URL::Mastodon,
|
||||
Source::URL::Newgrounds,
|
||||
Source::URL::Plurk,
|
||||
Source::URL::Skeb,
|
||||
|
||||
89
app/logical/source/url/mastodon.rb
Normal file
89
app/logical/source/url/mastodon.rb
Normal file
@@ -0,0 +1,89 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Image URLs
|
||||
#
|
||||
# * https://img.pawoo.net/media_attachments/files/001/297/997/small/c4272a09570757c2.png (page: https://pawoo.net/@evazion/19451018)
|
||||
# * https://img.pawoo.net/media_attachments/files/001/297/997/original/c4272a09570757c2.png
|
||||
#
|
||||
# * https://pawoo.net/media/lU2uV7C1MMQSb1czwvg (=> https://img.pawoo.net/media_attachments/files/001/300/923/original/cd18271f0077e789.png)
|
||||
#
|
||||
# Page URLs
|
||||
#
|
||||
# * https://pawoo.net/@evazion/19451018
|
||||
# * https://pawoo.net/web/statuses/19451018
|
||||
#
|
||||
# Account URLs
|
||||
#
|
||||
# * https://pawoo.net/@evazion
|
||||
# * https://pawoo.net/web/accounts/47806
|
||||
#
|
||||
# OAuth URL: (Note: ID is different from account URL ID)
|
||||
#
|
||||
# * https://pawoo.net/oauth_authentications/17230064
|
||||
#
|
||||
class Source::URL::Mastodon < Source::URL
|
||||
attr_reader :username, :user_id, :work_id, :full_image_url
|
||||
|
||||
def self.match?(url)
|
||||
url.domain.in?(%w[pawoo.net baraag.net])
|
||||
end
|
||||
|
||||
def parse
|
||||
case [host, *path_segments]
|
||||
|
||||
# https://pawoo.net/@evazion
|
||||
# https://baraag.net/@danbooru
|
||||
in _, /^@/ => username
|
||||
@username = username.delete_prefix("@")
|
||||
|
||||
# https://pawoo.net/@evazion/19451018
|
||||
# https://baraag.net/@curator/102270656480174153
|
||||
in _, /^@/ => username, /^\d+$/ => work_id, *rest
|
||||
@username = username.delete_prefix("@")
|
||||
@work_id = work_id
|
||||
|
||||
# https://pawoo.net/web/statuses/19451018
|
||||
# https://pawoo.net/web/statuses/19451018/favorites
|
||||
# https://baraag.net/web/statuses/102270656480174153
|
||||
in _, "web", "statuses", work_id, *rest
|
||||
@work_id = work_id
|
||||
|
||||
# https://pawoo.net/web/accounts/47806
|
||||
# https://baraag.net/web/accounts/107862785324786980
|
||||
in _, "web", "accounts", user_id
|
||||
@user_id = user_id
|
||||
|
||||
# Page: https://pawoo.net/@evazion/19451018
|
||||
# https://img.pawoo.net/media_attachments/files/001/297/997/small/c4272a09570757c2.png
|
||||
# https://img.pawoo.net/media_attachments/files/001/297/997/original/c4272a09570757c2.png
|
||||
in "img.pawoo.net", "media_attachments", "files", *subdirs, file_size, filename
|
||||
@file_size = file_size
|
||||
@full_image_url = "#{site}/media_attachments/files/#{subdirs.join("/")}/original/#{filename}"
|
||||
|
||||
# Page: https://baraag.net/@danbooru/107866090743238456
|
||||
# https://baraag.net/system/media_attachments/files/107/866/084/749/942/932/original/a9e0f553e332f303.mp4
|
||||
# https://baraag.net/system/media_attachments/files/107/866/084/754/127/256/original/3895a14ce3736f13.mp4
|
||||
# https://baraag.net/system/media_attachments/files/107/866/084/754/651/925/original/8f3df857681a1639.png
|
||||
in "baraag.net", "system", "media_attachments", "files", *subdirs, file_size, filename
|
||||
@file_size = file_size
|
||||
@full_image_url = "#{site}/system/media_attachments/files/#{subdirs.join("/")}/original/#{filename}"
|
||||
|
||||
# https://pawoo.net/media/lU2uV7C1MMQSb1czwvg
|
||||
in "pawoo.net", "media", media_hash
|
||||
@media_hash = media_hash
|
||||
|
||||
else
|
||||
end
|
||||
end
|
||||
|
||||
def site_name
|
||||
case domain
|
||||
when "pawoo.net" then "Pawoo"
|
||||
when "baraag.net" then "Baraag"
|
||||
end
|
||||
end
|
||||
|
||||
def image_url?
|
||||
full_image_url.present?
|
||||
end
|
||||
end
|
||||
@@ -1,44 +1,20 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Image URLS:
|
||||
# * https://img.pawoo.net/media_attachments/files/001/297/997/small/c4272a09570757c2.png
|
||||
# * https://img.pawoo.net/media_attachments/files/001/297/997/original/c4272a09570757c2.png
|
||||
# * https://pawoo.net/media/lU2uV7C1MMQSb1czwvg
|
||||
#
|
||||
# Page URLS:
|
||||
# * https://pawoo.net/@evazion/19451018
|
||||
# * https://pawoo.net/web/statuses/19451018
|
||||
#
|
||||
# Account URLS:
|
||||
# * https://pawoo.net/@evazion
|
||||
# * https://pawoo.net/web/accounts/47806
|
||||
#
|
||||
# OAUTH URLS: (NOTE: ID IS DIFFERENT FROM ACCOUNT URL ID)
|
||||
# * https://pawoo.net/oauth_authentications/17230064
|
||||
|
||||
# @see Source::URL::Mastodon
|
||||
module Sources::Strategies
|
||||
class Mastodon < Base
|
||||
HOST = %r{\Ahttps?://(?:www\.)?(?<domain>pawoo\.net|baraag\.net)}i
|
||||
IMAGE = %r{\Ahttps?://(?:img\.pawoo\.net|baraag\.net(?:/system(?:/cache)?)?)/media_attachments/files/((?:\d+/)+\d+)}
|
||||
NAMED_PROFILE = %r{#{HOST}/@(?<artist_name>\w+)}i
|
||||
ID_PROFILE = %r{#{HOST}/web/accounts/(?<account_id>\d+)}
|
||||
|
||||
STATUS1 = %r{\A#{HOST}/web/statuses/(?<status_id>\d+)}
|
||||
STATUS2 = %r{\A#{NAMED_PROFILE}/(?<status_id>\d+)}
|
||||
|
||||
def domains
|
||||
["pawoo.net", "baraag.net"]
|
||||
def match?
|
||||
Source::URL::Mastodon === parsed_url
|
||||
end
|
||||
|
||||
def site_name
|
||||
parsed_url.domain
|
||||
parsed_url.site_name
|
||||
end
|
||||
|
||||
def file_host
|
||||
def domain
|
||||
case site_name
|
||||
when "pawoo.net" then "img.pawoo.net"
|
||||
when "baraag.net" then "baraag.net/system"
|
||||
else site_name
|
||||
when "Pawoo" then "pawoo.net"
|
||||
when "Baraag" then "baraag.net"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -47,8 +23,8 @@ module Sources::Strategies
|
||||
end
|
||||
|
||||
def image_urls
|
||||
if url =~ %r{#{IMAGE}/(?:small|original)/([a-z0-9]+\.\w+)\z}i
|
||||
["https://#{file_host}/media_attachments/files/#{$1}/original/#{$2}"]
|
||||
if parsed_url.image_url?
|
||||
[parsed_url.full_image_url]
|
||||
else
|
||||
api_response.image_urls
|
||||
end
|
||||
@@ -60,15 +36,15 @@ module Sources::Strategies
|
||||
return if status_id.blank?
|
||||
|
||||
if artist_name.present?
|
||||
"https://#{site_name}/@#{artist_name}/#{status_id}"
|
||||
"https://#{domain}/@#{artist_name}/#{status_id}"
|
||||
else
|
||||
"https://#{site_name}/web/statuses/#{status_id}"
|
||||
"https://#{domain}/web/statuses/#{status_id}"
|
||||
end
|
||||
end
|
||||
|
||||
def profile_url
|
||||
if artist_name_from_url.present?
|
||||
"https://#{site_name}/@#{artist_name_from_url}"
|
||||
"https://#{domain}/@#{artist_name_from_url}"
|
||||
elsif api_response.present? && api_response.profile_url.present?
|
||||
api_response.profile_url
|
||||
end
|
||||
@@ -76,7 +52,7 @@ module Sources::Strategies
|
||||
|
||||
def account_url
|
||||
return if account_id.blank?
|
||||
"https://#{site_name}/web/accounts/#{account_id}"
|
||||
"https://#{domain}/web/accounts/#{account_id}"
|
||||
end
|
||||
|
||||
def profile_urls
|
||||
@@ -88,7 +64,7 @@ module Sources::Strategies
|
||||
end
|
||||
|
||||
def artist_name_from_url
|
||||
urls.map { |url| url[NAMED_PROFILE, :artist_name] }.compact.first
|
||||
parsed_url.username || parsed_referer&.username
|
||||
end
|
||||
|
||||
def other_names
|
||||
@@ -96,11 +72,11 @@ module Sources::Strategies
|
||||
end
|
||||
|
||||
def account_id
|
||||
urls.map { |url| url[ID_PROFILE, :account_id] }.compact.first || api_response.account_id
|
||||
parsed_url.user_id || parsed_referer&.user_id || api_response.account_id
|
||||
end
|
||||
|
||||
def status_id_from_url
|
||||
urls.map { |url| url[STATUS1, :status_id] || url[STATUS2, :status_id] }.compact.first
|
||||
parsed_url.work_id || parsed_referer&.work_id
|
||||
end
|
||||
|
||||
def artist_commentary_desc
|
||||
@@ -126,7 +102,7 @@ module Sources::Strategies
|
||||
end
|
||||
|
||||
def api_response
|
||||
MastodonApiClient.new(site_name, status_id_from_url)
|
||||
MastodonApiClient.new(domain, status_id_from_url)
|
||||
end
|
||||
memoize :api_response
|
||||
end
|
||||
|
||||
@@ -311,6 +311,9 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest
|
||||
should_upload_successfully("https://pawoo.net/web/statuses/1202176") if Danbooru.config.pawoo_client_id.present? # XXX
|
||||
should_upload_successfully("https://img.pawoo.net/media_attachments/files/000/128/953/original/4c0a06087b03343f.png") if Danbooru.config.pawoo_client_id.present? # XXX
|
||||
|
||||
should_upload_successfully("https://baraag.net/@danbooru/107866090743238456")
|
||||
should_upload_successfully("https://baraag.net/system/media_attachments/files/107/866/084/749/942/932/original/a9e0f553e332f303.mp4")
|
||||
|
||||
should_upload_successfully("https://www.pixiv.net/en/artworks/64476642")
|
||||
should_upload_successfully("https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247364")
|
||||
should_upload_successfully("https://i.pximg.net/img-original/img/2017/08/18/00/09/21/64476642_p0.jpg")
|
||||
|
||||
Reference in New Issue
Block a user