Files
danbooru/app/logical/source/extractor/lofter.rb
evazion d9d3c1dfe4 sources: rename Sources::Strategies to Source::Extractor.
Rename Sources::Strategies to Source::Extractor. A Source::Extractor
represents a thing that extracts information from a given URL.
2022-03-24 03:49:44 -05:00

62 lines
1.5 KiB
Ruby

# frozen_string_literal: true
# @see Source::URL::Lofter
module Source
class Extractor
class Lofter < Source::Extractor
def match?
Source::URL::Lofter === parsed_url
end
def image_urls
if parsed_url.image_url?
[parsed_url.full_image_url]
else
images = page&.search(".imgclasstag img")
images.to_a.pluck("src").map { |url| Source::URL.parse(url).full_image_url }
end
end
def profile_url
return nil if artist_name.blank?
"https://#{artist_name}.lofter.com"
end
def page_url
return nil if illust_id.blank? || profile_url.blank?
"#{profile_url}/post/#{illust_id}"
end
def page
return nil if page_url.blank?
response = http.cache(1.minute).get(page_url)
response.parse if response.status == 200
end
def tags
return [] if artist_name.blank?
page&.search("[href*='#{artist_name}.lofter.com/tag/']").to_a.map do |tag|
href = tag.attr("href")
[Source::URL.parse(href).unescaped_tag, href]
end
end
def artist_commentary_desc
page&.search(".ct .text, .content .text, .posts .photo .text").to_a.compact.first&.to_html
end
def illust_id
parsed_url.work_id || parsed_referer&.work_id
end
def artist_name
parsed_url.username || parsed_referer&.username
end
memoize :page
end
end
end