rubocop: fix various style issues.
This commit is contained in:
@@ -10,11 +10,11 @@ module Sources
|
||||
Strategies::ArtStation,
|
||||
Strategies::Nijie,
|
||||
Strategies::Pawoo,
|
||||
Strategies::Moebooru,
|
||||
Strategies::Moebooru
|
||||
]
|
||||
end
|
||||
|
||||
def self.find(url, referer=nil, default: Strategies::Null)
|
||||
def self.find(url, referer = nil, default: Strategies::Null)
|
||||
strategy = all.map { |strategy| strategy.new(url, referer) }.detect(&:match?)
|
||||
strategy || default&.new(url, referer)
|
||||
end
|
||||
|
||||
@@ -27,7 +27,7 @@ module Sources::Strategies
|
||||
|
||||
ASSET = %r!\Ahttps?://cdn\w*\.artstation\.com/p/assets/images/images/\d+/\d+/\d+/(?:medium|small|large)/!i
|
||||
|
||||
attr_reader :json, :image_urls
|
||||
attr_reader :json
|
||||
|
||||
def domains
|
||||
["artstation.com"]
|
||||
@@ -38,13 +38,11 @@ module Sources::Strategies
|
||||
end
|
||||
|
||||
def image_urls
|
||||
image_urls_sub
|
||||
.map { |asset| original_asset_url(asset) }
|
||||
@image_urls ||= image_urls_sub.map { |asset| original_asset_url(asset) }
|
||||
end
|
||||
memoize :image_urls
|
||||
|
||||
def page_url
|
||||
return nil unless project_id.present?
|
||||
return nil if project_id.blank?
|
||||
|
||||
if artist_name.present?
|
||||
"https://#{artist_name}.artstation.com/projects/#{project_id}"
|
||||
@@ -54,7 +52,7 @@ module Sources::Strategies
|
||||
end
|
||||
|
||||
def profile_url
|
||||
return nil unless artist_name.present?
|
||||
return nil if artist_name.blank?
|
||||
"https://www.artstation.com/#{artist_name}"
|
||||
end
|
||||
|
||||
@@ -84,14 +82,13 @@ module Sources::Strategies
|
||||
profile_url.present? && url == profile_url
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
def image_urls_sub
|
||||
if url.match?(ASSET)
|
||||
return [url]
|
||||
end
|
||||
|
||||
api_response[:assets].to_a
|
||||
api_response[:assets]
|
||||
.to_a
|
||||
.select { |asset| asset[:asset_type] == "image" }
|
||||
.map { |asset| asset[:image_url] }
|
||||
end
|
||||
@@ -100,7 +97,7 @@ module Sources::Strategies
|
||||
# purposes
|
||||
|
||||
def artist_name_from_url
|
||||
urls.map { |url| url[PROJECT, :artist_name] || url[ARTIST, :artist_name] }.compact.first
|
||||
urls.map { |url| url[PROJECT, :artist_name] || url[ARTIST, :artist_name] }.compact.first
|
||||
end
|
||||
|
||||
def project_id
|
||||
@@ -108,7 +105,7 @@ module Sources::Strategies
|
||||
end
|
||||
|
||||
def api_response
|
||||
return {} unless project_id.present?
|
||||
return {} if project_id.blank?
|
||||
|
||||
resp = Danbooru::Http.cache(1.minute).get("https://www.artstation.com/projects/#{project_id}.json")
|
||||
return {} if resp.code != 200
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# This is a collection of strategies for extracting information about a
|
||||
# resource. At a minimum it tries to extract the artist name and a canonical
|
||||
# URL to download the image from. But it can also be used to normalize a URL
|
||||
# for use with the artist finder.
|
||||
# This is a collection of strategies for extracting information about a
|
||||
# resource. At a minimum it tries to extract the artist name and a canonical
|
||||
# URL to download the image from. But it can also be used to normalize a URL
|
||||
# for use with the artist finder.
|
||||
#
|
||||
# Design Principles
|
||||
#
|
||||
@@ -24,11 +24,11 @@ module Sources
|
||||
true
|
||||
end
|
||||
|
||||
# * <tt>url</tt> - Should point to a resource suitable for
|
||||
# downloading. This may sometimes point to the binary file.
|
||||
# * <tt>url</tt> - Should point to a resource suitable for
|
||||
# downloading. This may sometimes point to the binary file.
|
||||
# It may also point to the artist's profile page, in cases
|
||||
# where this class is being used to normalize artist urls.
|
||||
# Implementations should be smart enough to detect this and
|
||||
# Implementations should be smart enough to detect this and
|
||||
# behave accordingly.
|
||||
# * <tt>referer_url</tt> - Sometimes the HTML page cannot be
|
||||
# determined from <tt>url</tt>. You should generally pass in a
|
||||
@@ -63,9 +63,9 @@ module Sources
|
||||
nil
|
||||
end
|
||||
|
||||
# Whatever <tt>url</tt> is, this method should return the direct links
|
||||
# to the canonical binary files. It should not be an HTML page. It should
|
||||
# be a list of JPEG, PNG, GIF, WEBM, MP4, ZIP, etc. It is what the
|
||||
# Whatever <tt>url</tt> is, this method should return the direct links
|
||||
# to the canonical binary files. It should not be an HTML page. It should
|
||||
# be a list of JPEG, PNG, GIF, WEBM, MP4, ZIP, etc. It is what the
|
||||
# downloader will fetch and save to disk.
|
||||
def image_urls
|
||||
raise NotImplementedError
|
||||
@@ -269,7 +269,7 @@ module Sources
|
||||
:tag_name => tag_name,
|
||||
:other_names => other_names,
|
||||
:profile_url => profile_url,
|
||||
:profile_urls => profile_urls,
|
||||
:profile_urls => profile_urls
|
||||
},
|
||||
:artists => artists.as_json(include: :sorted_urls),
|
||||
:image_url => image_url,
|
||||
@@ -284,18 +284,16 @@ module Sources
|
||||
:title => artist_commentary_title,
|
||||
:description => artist_commentary_desc,
|
||||
:dtext_title => dtext_artist_commentary_title,
|
||||
:dtext_description => dtext_artist_commentary_desc,
|
||||
:dtext_description => dtext_artist_commentary_desc
|
||||
},
|
||||
:api_response => api_response.to_h,
|
||||
:api_response => api_response.to_h
|
||||
}
|
||||
end
|
||||
|
||||
def to_json
|
||||
def to_json(*_args)
|
||||
to_h.to_json
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def http_exists?(url, headers)
|
||||
res = HTTParty.head(url, Danbooru.config.httparty_options.deep_merge(headers: headers))
|
||||
res.success?
|
||||
@@ -306,7 +304,7 @@ module Sources
|
||||
def self.to_dtext(text)
|
||||
text = text.to_s
|
||||
text = Rails::Html::FullSanitizer.new.sanitize(text, encode_special_chars: false)
|
||||
text = CGI::unescapeHTML(text)
|
||||
text = CGI.unescapeHTML(text)
|
||||
text
|
||||
end
|
||||
end
|
||||
|
||||
@@ -57,8 +57,6 @@ module Sources
|
||||
PATH_PROFILE = %r{\Ahttps?://(www\.)?deviantart\.com/#{ARTIST}/?\z}i
|
||||
SUBDOMAIN_PROFILE = %r{\Ahttps?://#{ARTIST}\.deviantart\.com/?\z}i
|
||||
|
||||
attr_accessor :api_client
|
||||
|
||||
def domains
|
||||
["deviantart.net", "deviantart.com"]
|
||||
end
|
||||
@@ -211,8 +209,6 @@ module Sources
|
||||
end.gsub(/\A[[:space:]]+|[[:space:]]+\z/, "")
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
def self.deviation_id_from_url(url)
|
||||
if url =~ ASSET
|
||||
$~[:base36_deviation_id].try(:to_i, 36)
|
||||
|
||||
@@ -130,8 +130,6 @@ module Sources
|
||||
end
|
||||
memoize :tags
|
||||
|
||||
public
|
||||
|
||||
def api_client
|
||||
if illust_id
|
||||
NicoSeigaApiClient.new(illust_id: illust_id)
|
||||
|
||||
@@ -120,9 +120,11 @@ module Sources
|
||||
def tags
|
||||
links = page&.search("div#view-tag a") || []
|
||||
|
||||
links.select do |node|
|
||||
search_links = links.select do |node|
|
||||
node["href"] =~ /search\.php/
|
||||
end.map do |node|
|
||||
end
|
||||
|
||||
search_links.map do |node|
|
||||
[node.inner_text, "https://nijie.info" + node.attr("href")]
|
||||
end
|
||||
end
|
||||
@@ -131,8 +133,6 @@ module Sources
|
||||
"nijie" + artist_id.to_s
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
def self.to_dtext(text)
|
||||
text = text.to_s.gsub(/\r\n|\r/, "<br>")
|
||||
DText.from_html(text).strip
|
||||
@@ -196,7 +196,7 @@ module Sources
|
||||
form['password'] = Danbooru.config.nijie_password
|
||||
end.click_button
|
||||
end
|
||||
session = mech.cookie_jar.cookies.select{|c| c.name == "NIJIEIJIEID"}.first
|
||||
session = mech.cookie_jar.cookies.select {|c| c.name == "NIJIEIJIEID"}.first
|
||||
Cache.put("nijie-session", session.value, 1.day) if session
|
||||
end
|
||||
|
||||
@@ -207,7 +207,6 @@ module Sources
|
||||
mech.cookie_jar.add(cookie)
|
||||
|
||||
mech
|
||||
|
||||
rescue Mechanize::ResponseCodeError => x
|
||||
if x.response_code.to_i == 429
|
||||
sleep(5)
|
||||
|
||||
@@ -85,8 +85,6 @@ module Sources::Strategies
|
||||
end.strip
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
def api_response
|
||||
[url, referer_url].each do |x|
|
||||
if client = PawooApiClient.new.get(x)
|
||||
|
||||
@@ -69,7 +69,7 @@ module Sources
|
||||
if text.nil?
|
||||
return nil
|
||||
end
|
||||
|
||||
|
||||
text = text.gsub(%r!https?://www\.pixiv\.net/member_illust\.php\?mode=medium&illust_id=([0-9]+)!i) do |match|
|
||||
pixiv_id = $1
|
||||
%(pixiv ##{pixiv_id} "»":[/posts?tags=pixiv:#{pixiv_id}])
|
||||
@@ -132,11 +132,10 @@ module Sources
|
||||
end
|
||||
|
||||
return url
|
||||
|
||||
rescue PixivApiClient::BadIDError
|
||||
nil
|
||||
end
|
||||
|
||||
|
||||
def canonical_url
|
||||
return image_url
|
||||
end
|
||||
@@ -183,7 +182,7 @@ module Sources
|
||||
rescue PixivApiClient::BadIDError
|
||||
nil
|
||||
end
|
||||
|
||||
|
||||
def headers
|
||||
if fanbox_id.present?
|
||||
# need the session to download fanbox images
|
||||
@@ -236,8 +235,6 @@ module Sources
|
||||
illust_id.present? ? "pixiv:#{illust_id}" : "source:#{canonical_url}"
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
def image_urls_sub
|
||||
if url =~ FANBOX_IMAGE
|
||||
return [url]
|
||||
|
||||
@@ -131,8 +131,6 @@ module Sources::Strategies
|
||||
DText.from_html(artist_commentary_desc).strip
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
# Look for the biggest available version on media.tumblr.com. A bigger
|
||||
# version may or may not exist.
|
||||
#
|
||||
@@ -181,7 +179,7 @@ module Sources::Strategies
|
||||
|
||||
response = Danbooru::Http.cache(1.minute).get(
|
||||
"https://api.tumblr.com/v2/blog/#{blog_name}/posts",
|
||||
params: { id: post_id, api_key: Danbooru.config.tumblr_consumer_key },
|
||||
params: { id: post_id, api_key: Danbooru.config.tumblr_consumer_key }
|
||||
)
|
||||
|
||||
return {} if response.code != 200
|
||||
|
||||
@@ -134,15 +134,13 @@ module Sources::Strategies
|
||||
url_replacements = url_replacements.to_h
|
||||
|
||||
desc = artist_commentary_desc.unicode_normalize(:nfkc)
|
||||
desc = CGI::unescapeHTML(desc)
|
||||
desc = CGI.unescapeHTML(desc)
|
||||
desc = desc.gsub(%r!https?://t\.co/[a-zA-Z0-9]+!i, url_replacements)
|
||||
desc = desc.gsub(%r!#([^[:space:]]+)!, '"#\\1":[https://twitter.com/hashtag/\\1]')
|
||||
desc = desc.gsub(%r!@([a-zA-Z0-9_]+)!, '"@\\1":[https://twitter.com/\\1]')
|
||||
desc.strip
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
def api_client
|
||||
TwitterApiClient.new(Danbooru.config.twitter_api_key, Danbooru.config.twitter_api_secret)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user