Refactor sources

This commit is contained in:
Albert Yi
2018-08-06 17:39:25 -07:00
parent 54363ffecf
commit 762dc3da24
71 changed files with 2340 additions and 2430 deletions

View File

@@ -152,7 +152,7 @@ class Artist < ApplicationRecord
url = ArtistUrl.normalize(url)
artists = []
# return [] unless Sources::Site.new(url).normalized_for_artist_finder?
# return [] unless Sources::Strategies.find(url).normalized_for_artist_finder?
while artists.empty? && url.size > 10
u = url.sub(/\/+$/, "") + "/"
@@ -481,13 +481,8 @@ class Artist < ApplicationRecord
end
def search_for_profile(url)
source = Sources::Site.new(url)
if source.strategy
source.get
find_all_by_url(source.profile_url)
else
nil
end
source = Sources::Strategies.find(url)
find_all_by_url(source.profile_url)
rescue Net::OpenTimeout, PixivApiClient::Error
raise if Rails.env.test?
nil

View File

@@ -22,7 +22,7 @@ class ArtistUrl < ApplicationRecord
url = url.sub(%r!^http://blog\d+\.fc2!, "http://blog.fc2")
url = url.sub(%r!^http://blog-imgs-\d+\.fc2!, "http://blog.fc2")
url = url.sub(%r!^http://blog-imgs-\d+-\w+\.fc2!, "http://blog.fc2")
url = url.sub(%r!^(http://seiga.nicovideo.jp/user/illust/\d+)\?.+!, '\1/')
# url = url.sub(%r!^(http://seiga.nicovideo.jp/user/illust/\d+)\?.+!, '\1/')
url = url.sub(%r!^http://pictures.hentai-foundry.com//!, "http://pictures.hentai-foundry.com/")
if url !~ %r{\Ahttps?://(?:fc|th|pre|orig|img|www)\.}
url = url.sub(%r{\Ahttps?://(.+?)\.deviantart\.com(.*)}, 'http://www.deviantart.com/\1\2')
@@ -30,13 +30,17 @@ class ArtistUrl < ApplicationRecord
# the strategy won't always work for twitter because it looks for a status
url = url.downcase if url =~ %r!^https?://(?:mobile\.)?twitter\.com!
begin
url = Sources::Site.new(url).normalize_for_artist_finder!
source = Sources::Strategies.find(url)
if !source.normalized_for_artist_finder? && source.normalizable_for_artist_finder?
url = source.normalize_for_artist_finder
end
rescue Net::OpenTimeout, PixivApiClient::Error
raise if Rails.env.test?
rescue Sources::Site::NoStrategyError
end
url = url.gsub(/\/+\Z/, "")
url = url.gsub(%r!^https://!, "http://")
url + "/"
@@ -102,10 +106,6 @@ class ArtistUrl < ApplicationRecord
end
def normalize
if !Sources::Site.new(normalized_url).normalized_for_artist_finder?
self.normalized_url = self.class.normalize(url)
end
rescue Sources::Site::NoStrategyError
self.normalized_url = self.class.normalize(url)
end

View File

@@ -1682,7 +1682,11 @@ class Post < ApplicationRecord
module PixivMethods
def parse_pixiv_id
self.pixiv_id = Sources::Strategies::Pixiv.new(source).illust_id_from_url
self.pixiv_id = nil
if Sources::Strategies::Pixiv.match?(source)
self.pixiv_id = Sources::Strategies::Pixiv.new(source).illust_id
end
end
end
@@ -1790,10 +1794,8 @@ class Post < ApplicationRecord
return if has_tag?("artist_request") || has_tag?("official_art")
return if tags.any? { |t| t.category == Tag.categories.artist }
site = Sources::Site.new(source)
site = Sources::Strategies.find(source)
self.warnings[:base] << "Artist tag is required. Create a new tag with [[artist:<artist_name>]]. Ask on the forum if you need naming help"
rescue Sources::Site::NoStrategyError => e
# unrecognized source; do nothing.
end
def has_copyright_tag

View File

@@ -47,8 +47,7 @@ class Upload < ApplicationRecord
end
attr_accessor :as_pending,
:referer_url, :downloaded_source, :replaced_post, :file
attr_accessor :as_pending, :replaced_post, :file
belongs_to :uploader, :class_name => "User"
belongs_to :post, optional: true
@@ -63,6 +62,7 @@ class Upload < ApplicationRecord
validates :file_ext, format: { with: /jpg|gif|png|swf|webm|mp4|zip/ }, allow_nil: true
validates_with Validator
serialize :context, JSON
scope :preprocessed, -> { where(status: "preprocessed") }
def initialize_attributes
self.uploader_id = CurrentUser.id