Files
danbooru/app/models/artist_url.rb
Toks c601253553 Don't make pixiv http requests when displaying artist history
The reason the urls need to be normalized at all here is because legacy
urls from Danbooru 1 don't have their unnormalized counterparts stored
anywhere and thus can't be accurately compared with unnormalized urls
from Danbooru 2.
2014-12-09 17:23:44 -05:00

64 lines
2.0 KiB
Ruby

class ArtistUrl < ActiveRecord::Base
before_save :initialize_normalized_url, on: [ :create ]
before_save :normalize
validates_presence_of :url
belongs_to :artist
attr_accessible :url, :artist_id, :normalized_url
def self.normalize(url)
if url.nil?
nil
else
url = url.gsub(/^https:\/\//, "http://")
url = url.gsub(/^http:\/\/blog\d+\.fc2/, "http://blog.fc2")
url = url.gsub(/^http:\/\/blog-imgs-\d+\.fc2/, "http://blog.fc2")
url = url.gsub(/^http:\/\/blog-imgs-\d+-\w+\.fc2/, "http://blog.fc2")
url = Sources::Site.new(url).normalize_for_artist_finder!
url = url.gsub(/\/+\Z/, "")
url + "/"
end
end
def self.legacy_normalize(url)
if url.nil?
nil
else
url = url.gsub(/^https:\/\//, "http://")
url = url.gsub(/^http:\/\/blog\d+\.fc2/, "http://blog.fc2")
url = url.gsub(/^http:\/\/blog-imgs-\d+\.fc2/, "http://blog.fc2")
url = url.gsub(/^http:\/\/blog-imgs-\d+-\w+\.fc2/, "http://blog.fc2")
url = url.gsub(/^http:\/\/img\d+\.pixiv\.net/, "http://img.pixiv.net")
url = url.gsub(/^http:\/\/i\d+\.pixiv\.net\/img\d+/, "http://img.pixiv.net")
url = url.gsub(/\/+\Z/, "")
url + "/"
end
end
def self.normalize_for_search(url)
if url =~ /\.\w+\Z/ && url =~ /\w\/\w/
url = File.dirname(url)
end
url = url.gsub(/^https:\/\//, "http://")
url = url.gsub(/^http:\/\/blog\d+\.fc2/, "http://blog*.fc2")
url = url.gsub(/^http:\/\/blog-imgs-\d+\.fc2/, "http://blog*.fc2")
url = url.gsub(/^http:\/\/blog-imgs-\d+-\w+\.fc2/, "http://blog*.fc2")
url = url.gsub(/^http:\/\/img\d+\.pixiv\.net/, "http://img*.pixiv.net")
url = url.gsub(/^http:\/\/i\d+\.pixiv\.net\/img\d+/, "http://*.pixiv.net/img*")
end
def normalize
if !Sources::Site.new(normalized_url).normalized_for_artist_finder?
self.normalized_url = self.class.normalize(url)
end
end
def initialize_normalized_url
self.normalized_url = url
end
def to_s
url
end
end