Add Lofter support
This commit is contained in:
@@ -16,7 +16,8 @@ module Sources
|
||||
Strategies::Mastodon,
|
||||
Strategies::Weibo,
|
||||
Strategies::Newgrounds,
|
||||
Strategies::Skeb
|
||||
Strategies::Skeb,
|
||||
Strategies::Lofter
|
||||
]
|
||||
end
|
||||
|
||||
|
||||
100
app/logical/sources/strategies/lofter.rb
Normal file
100
app/logical/sources/strategies/lofter.rb
Normal file
@@ -0,0 +1,100 @@
|
||||
# Image URLs
|
||||
#
|
||||
# # sample
|
||||
# * https://imglf3.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJSzFCWFlnUWgzb01DcUdpT1lreG5yQjJVMkhGS09HNGR3PT0.png?imageView&thumbnail=1680x0&quality=96&stripmeta=0
|
||||
#
|
||||
# # full size
|
||||
# * https://imglf3.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJSzFCWFlnUWgzb01DcUdpT1lreG5yQjJVMkhGS09HNGR3PT0.png
|
||||
# * http://imglf0.nosdn.127.net/img/cHl3bXNZdDRaaHBnNWJuN1Y4OXBqR01CeVBZSVNmU2FWZWtHc1h4ZTZiUGxlRzMwZnFDM1JnPT0.jpg (404)
|
||||
#
|
||||
# Page URLs
|
||||
#
|
||||
# * https://gengar563.lofter.com/post/1e82da8c_1c98dae1b
|
||||
#
|
||||
# Profile URLs
|
||||
#
|
||||
# * http://gengar563.lofter.com/
|
||||
|
||||
module Sources
|
||||
module Strategies
|
||||
class Lofter < Base
|
||||
PROFILE_URL = %r{\Ahttps?://(?<artist_name>[\w-]+).lofter.com}i
|
||||
PAGE_URL = %r{#{PROFILE_URL}/post/(?<illust_id>[\w-]+)}i
|
||||
IMAGE_HOST = %r{\Ahttps?://imglf\d\.(?:nosdn\d?\.12\d|lf127)\.net}i
|
||||
IMAGE_URL = %r{#{IMAGE_HOST}/img/\w+\.\w+}i
|
||||
|
||||
def domains
|
||||
["lofter.com", "lf127.net"]
|
||||
end
|
||||
|
||||
def site_name
|
||||
"Lofter"
|
||||
end
|
||||
|
||||
def match?
|
||||
return false if parsed_url.nil?
|
||||
parsed_url.domain.in?(domains) || parsed_url.host =~ IMAGE_HOST
|
||||
end
|
||||
|
||||
def image_url
|
||||
if url =~ IMAGE_URL
|
||||
get_full_version(url)
|
||||
else
|
||||
image_urls.first
|
||||
end
|
||||
end
|
||||
|
||||
def image_urls
|
||||
images = page&.search(".pic img")
|
||||
images.to_a.map { |img| get_full_version(img["src"]) }
|
||||
end
|
||||
|
||||
def get_full_version(url)
|
||||
parsed = URI.parse(url)
|
||||
"https://#{parsed.host}#{parsed.path}"
|
||||
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
|
||||
tags = page&.search(".info .tag")
|
||||
|
||||
tags.to_a.map do |tag|
|
||||
[tag.text, tag.attr("href")]
|
||||
end
|
||||
end
|
||||
|
||||
def artist_commentary_desc
|
||||
page&.search(".ct .text")&.to_html
|
||||
end
|
||||
|
||||
def normalize_for_source
|
||||
page_url
|
||||
end
|
||||
|
||||
def illust_id
|
||||
urls.map { |u| u[PAGE_URL, :illust_id] }.compact.first
|
||||
end
|
||||
|
||||
def artist_name
|
||||
urls.map { |u| u[PROFILE_URL, :artist_name] || u[PAGE_URL, :artist_name] }.compact.first
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
62
test/unit/sources/lofter_test.rb
Normal file
62
test/unit/sources/lofter_test.rb
Normal file
@@ -0,0 +1,62 @@
|
||||
require 'test_helper'
|
||||
|
||||
module Sources
|
||||
class LofterTest < ActiveSupport::TestCase
|
||||
context "A lofter post" do
|
||||
setup do
|
||||
@img = "https://imglf4.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJUFczb2RKSVlpMHJkNy9kc3BSQVQvQm5DNzB4eVhxay9nPT0.png?imageView&thumbnail=1680x0&quality=96&stripmeta=0"
|
||||
@ref = "https://gengar563.lofter.com/post/1e82da8c_1c98dae1b"
|
||||
@source = Sources::Strategies.find(@img, @ref)
|
||||
end
|
||||
|
||||
should "get the artist name" do
|
||||
assert_equal("gengar563", @source.artist_name)
|
||||
end
|
||||
|
||||
should "get the commentary" do
|
||||
assert_match(/发了三次发不出有毒…… \n.*\n失去耐心.jpg/, @source.dtext_artist_commentary_desc)
|
||||
end
|
||||
should "get profile url" do
|
||||
assert_equal("https://gengar563.lofter.com", @source.profile_url)
|
||||
end
|
||||
|
||||
should "get the image urls" do
|
||||
images = %w[
|
||||
https://imglf3.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJQ1RxY0lYaU1UUE9tQ0NvUE9rVXFpOFFEVzMwbnQ4aEFnPT0.jpg
|
||||
https://imglf3.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJRWlXYTRVOEpXTU9TSGt3TjBDQ0JFZVpZMEJtWjFneVNBPT0.png
|
||||
https://imglf6.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJR1d3Y2VvbTNTQlIvdFU1WWlqZHEzbjI4MFVNZVdoN3VBPT0.png
|
||||
https://imglf6.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJTi83NDRDUjNvd3hySGxEZFovd2hwbi9oaG9NQ1hOUkZ3PT0.png
|
||||
https://imglf4.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJUFczb2RKSVlpMHJkNy9kc3BSQVQvQm5DNzB4eVhxay9nPT0.png
|
||||
https://imglf4.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJSStJZE9RYnJURktHazdIVHNNMjQ5eFJldHVTQy9XbDB3PT0.png
|
||||
https://imglf3.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJSzFCWFlnUWgzb01DcUdpT1lreG5yQjJVMkhGS09HNGR3PT0.png
|
||||
]
|
||||
|
||||
assert_equal(images, @source.image_urls)
|
||||
end
|
||||
|
||||
should "download the full-size image" do
|
||||
assert_downloaded(2_739_443, @source.image_url)
|
||||
end
|
||||
|
||||
should "find the correct artist" do
|
||||
@artist = FactoryBot.create(:artist, name: "gengar563", url_string: "https://gengar563.lofter.com")
|
||||
assert_equal([@artist], @source.artists)
|
||||
end
|
||||
end
|
||||
|
||||
context "A bad link" do
|
||||
should "correctly get the full size" do
|
||||
source = Sources::Strategies.find("https://imglf4.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJUFczb2RKSVlpMHJkNy9kc3BSQVQvQm5DNzB4eVhxay9nPT0.png?imageView&thumbnail=1680x0&quality=96&stripmeta=0")
|
||||
assert_equal("https://imglf4.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJUFczb2RKSVlpMHJkNy9kc3BSQVQvQm5DNzB4eVhxay9nPT0.png", source.image_url)
|
||||
assert_nothing_raised { source.to_h }
|
||||
end
|
||||
end
|
||||
|
||||
context "A dead link" do
|
||||
should "not raise anything" do
|
||||
source = Sources::Strategies.find("https://gxszdddd.lofter.com/post/322595b1_1ca5e6f66")
|
||||
assert_nothing_raised { source.to_h }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user