Add support for a couple more URL types: * https://foundation.app/@asuka111art/dinner-with-cats-82426 * https://f8n-production-collection-assets.imgix.net/0x3B3ee1931Dc30C1957379FAc9aba94D1C48a5405/128711/QmcBfbeCMSxqYB3L1owPAxFencFx3jLzCPFx6xUBxgSCkH/nft.png Also include these URLs in the list of profile URLs: * https://foundation.app/0x7E2ef75C0C09b2fc6BCd1C68B6D409720CcD58d2 (for https://foundation.app/@mochiiimo) These URLs should be stable even if the user changes their name.
69 lines
2.1 KiB
Ruby
69 lines
2.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# A Source::URL is a URL from a source site, such as Twitter, Pixiv, etc. Each site has a
|
|
# subclass responsible for parsing and extracting information from URLs for that site.
|
|
#
|
|
# To add a new site, create a subclass of Source::URL and implement `#match?` to define
|
|
# which URLs belong to the site, and `#parse` to parse and extract information from the URL.
|
|
#
|
|
# Source::URL is a subclass of Danbooru::URL, so it inherits some common utility methods
|
|
# from there.
|
|
#
|
|
# @example
|
|
# url = Source::URL.parse("https://twitter.com/yasunavert/status/1496123903290314755")
|
|
# url.site_name # => "Twitter"
|
|
# url.status_id # => "1496123903290314755"
|
|
# url.twitter_username # => "yasunavert"
|
|
#
|
|
module Source
|
|
class URL < Danbooru::URL
|
|
SUBCLASSES = [
|
|
Source::URL::Twitter,
|
|
Source::URL::TwitPic,
|
|
Source::URL::Foundation,
|
|
]
|
|
|
|
# Parse a URL into a subclass of Source::URL, or raise an exception if the URL is not a valid HTTP or HTTPS URL.
|
|
#
|
|
# @param url [String, Danbooru::URL]
|
|
# @return [Source::URL]
|
|
def self.parse!(url)
|
|
url = Danbooru::URL.new(url)
|
|
subclass = SUBCLASSES.find { |c| c.match?(url) } || Source::URL
|
|
subclass.new(url)
|
|
end
|
|
|
|
# Parse a string into a URL, or return nil if the string is not a valid HTTP or HTTPS URL.
|
|
#
|
|
# @param url [String, Danbooru::URL]
|
|
# @return [Danbooru::URL]
|
|
def self.parse(url)
|
|
parse!(url)
|
|
rescue Error
|
|
nil
|
|
end
|
|
|
|
# Subclasses should implement this to return true for URLs that should be handled by the subclass.
|
|
#
|
|
# @param url [Danbooru::URL] The source URL.
|
|
def self.match?(url)
|
|
raise NotImplementedError
|
|
end
|
|
|
|
# @return [String, nil] The name of the site this URL belongs to, or possibly nil if unknown.
|
|
def site_name
|
|
self.class.name.demodulize
|
|
end
|
|
|
|
protected def initialize(...)
|
|
super(...)
|
|
parse
|
|
end
|
|
|
|
# Subclasses should implement this to parse and extract any useful information from
|
|
# the URL. This is called when the URL is initialized.
|
|
protected def parse
|
|
end
|
|
end
|
|
end
|