refactored source code, work on pixiv integration
This commit is contained in:
@@ -1,18 +1,36 @@
|
||||
module Sources
|
||||
class Site
|
||||
attr_reader :url, :strategy
|
||||
delegate :artist_name, :profile_url, :image_url, :tags, :to => :strategy
|
||||
delegate :get, :site_name, :artist_name, :artist_alias, :profile_url, :image_url, :tags, :artist_record, :unique_id, :to => :strategy
|
||||
|
||||
def self.strategies
|
||||
[Strategies::Fc2, Strategies::NicoSeiga, Strategies::Pixa, Strategies::Pixiv, Strategies::Tinami, Strategies::Default]
|
||||
end
|
||||
|
||||
def initialize(url)
|
||||
@url = url
|
||||
|
||||
case url
|
||||
when /pixiv\.net/
|
||||
@strategy = Strategies::Pixiv.new(url)
|
||||
|
||||
else
|
||||
@strategy = Strategies::Default.new(url)
|
||||
Site.strategies.each do |strategy|
|
||||
if strategy.url_match?(url)
|
||||
@strategy = strategy.new(url)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def to_json
|
||||
return {
|
||||
:artist_name => artist_name,
|
||||
:profile_url => profile_url,
|
||||
:tags => tags,
|
||||
:danbooru_name => artist_record.first.try(:name),
|
||||
:danbooru_id => artist_record.first.try(:id),
|
||||
:unique_id => unique_id
|
||||
}.to_json
|
||||
end
|
||||
|
||||
def available?
|
||||
strategy.present?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,11 +3,23 @@ module Sources
|
||||
class Base
|
||||
attr_reader :url, :agent
|
||||
|
||||
def self.url_match?(url)
|
||||
false
|
||||
end
|
||||
|
||||
def initialize(url)
|
||||
@url = url
|
||||
@agent = create_agent
|
||||
end
|
||||
|
||||
def get
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def site_name
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def artist_name
|
||||
raise NotImplementedError
|
||||
end
|
||||
@@ -24,6 +36,18 @@ module Sources
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def artist_alias
|
||||
nil
|
||||
end
|
||||
|
||||
def unique_id
|
||||
artist_name
|
||||
end
|
||||
|
||||
def artist_record
|
||||
Artist.other_names_match(artist_name)
|
||||
end
|
||||
|
||||
protected
|
||||
def create_agent
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
module Sources
|
||||
module Strategies
|
||||
class Default < Base
|
||||
def site_name
|
||||
"?"
|
||||
end
|
||||
|
||||
def artist_name
|
||||
"?"
|
||||
end
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
module Sources
|
||||
module Strategies
|
||||
class Fc2 < Base
|
||||
def site_name
|
||||
"FC2"
|
||||
end
|
||||
|
||||
def artist_name
|
||||
"?"
|
||||
end
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
module Sources
|
||||
module Strategies
|
||||
class NicoSeiga < Base
|
||||
def site_name
|
||||
"Nico Seiga"
|
||||
end
|
||||
|
||||
def artist_name
|
||||
"?"
|
||||
end
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
module Sources
|
||||
module Strategies
|
||||
class Pixa < Base
|
||||
def site_name
|
||||
"Pixa"
|
||||
end
|
||||
|
||||
def artist_name
|
||||
"?"
|
||||
end
|
||||
|
||||
@@ -3,13 +3,21 @@ module Sources
|
||||
class Pixiv < Base
|
||||
attr_reader :artist_name, :profile_url, :image_url, :tags
|
||||
|
||||
def initialize(url)
|
||||
super
|
||||
get
|
||||
def self.url_match?(url)
|
||||
url =~ /^https?:\/\/(?:\w+\.)?pixiv\.net/
|
||||
end
|
||||
|
||||
def is_pixiv?
|
||||
url =~ /pixiv\.net/
|
||||
def initialize(url)
|
||||
super
|
||||
end
|
||||
|
||||
def site_name
|
||||
"Pixiv"
|
||||
end
|
||||
|
||||
def unique_id
|
||||
image_url =~ /\/img\/([^\/]+)/
|
||||
$1
|
||||
end
|
||||
|
||||
def get
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
module Sources
|
||||
module Strategies
|
||||
class Tinami < Base
|
||||
def site_name
|
||||
"Tinami"
|
||||
end
|
||||
|
||||
def artist_name
|
||||
"?"
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user