Lofter: support another theme and rewrite tests

This commit is contained in:
nonamethanks
2022-09-30 22:04:40 +02:00
parent 559bf1ae0a
commit 1d7caf703c
2 changed files with 50 additions and 59 deletions

View File

@@ -44,7 +44,13 @@ module Source
end end
def artist_commentary_desc def artist_commentary_desc
page&.search(".ct .text, .content .text, .posts .photo .text").to_a.compact.first&.to_html commentary_selectors = [
".ct .text",
".content .text",
".posts .photo .text",
"#post .description",
]
page&.search(commentary_selectors.join(", ")).to_a.compact.first&.to_html
end end
def illust_id def illust_id

View File

@@ -2,71 +2,56 @@ require "test_helper"
module Sources module Sources
class LofterTest < ActiveSupport::TestCase class LofterTest < ActiveSupport::TestCase
context "A lofter post" do context "A lofter post with commentary under <.ct .text>" do
setup do image_urls = %w[
@img = "https://imglf4.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJUFczb2RKSVlpMHJkNy9kc3BSQVQvQm5DNzB4eVhxay9nPT0.png?imageView&thumbnail=1680x0&quality=96&stripmeta=0" https://imglf3.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJQ1RxY0lYaU1UUE9tQ0NvUE9rVXFpOFFEVzMwbnQ4aEFnPT0.jpg
@ref = "https://gengar563.lofter.com/post/1e82da8c_1c98dae1b" https://imglf3.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJRWlXYTRVOEpXTU9TSGt3TjBDQ0JFZVpZMEJtWjFneVNBPT0.png
@source = Source::Extractor.find(@img, @ref) https://imglf6.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJR1d3Y2VvbTNTQlIvdFU1WWlqZHEzbjI4MFVNZVdoN3VBPT0.png
@source2 = Source::Extractor.find(@ref) https://imglf6.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJTi83NDRDUjNvd3hySGxEZFovd2hwbi9oaG9NQ1hOUkZ3PT0.png
end https://imglf4.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJUFczb2RKSVlpMHJkNy9kc3BSQVQvQm5DNzB4eVhxay9nPT0.png
https://imglf4.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJSStJZE9RYnJURktHazdIVHNNMjQ5eFJldHVTQy9XbDB3PT0.png
should "get the artist name" do https://imglf3.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJSzFCWFlnUWgzb01DcUdpT1lreG5yQjJVMkhGS09HNGR3PT0.png
assert_equal("gengar563", @source.artist_name) ]
end strategy_should_work(
"https://gengar563.lofter.com/post/1e82da8c_1c98dae1b",
should "get the commentary" do image_urls: image_urls,
assert_match(/发了三次发不出有毒…… \n.*\n失去耐心.jpg/, @source.dtext_artist_commentary_desc) artist_name: "gengar563",
end profile_url: "https://gengar563.lofter.com",
should "get profile url" do dtext_artist_commentary_desc: /发了三次发不出有毒…… \n.*\n失去耐心.jpg/
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, @source2.image_urls)
end
should "download the full-size image" do
assert_downloaded(2_739_443, @source.image_urls.sole)
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 end
context "A different CSS schema" do context "A lofter direct image url" do
should "still find all the data" do strategy_should_work(
source1 = Source::Extractor.find("https://yuli031458.lofter.com/post/3163d871_1cbdc5f6d") "https://imglf4.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJUFczb2RKSVlpMHJkNy9kc3BSQVQvQm5DNzB4eVhxay9nPT0.png?imageView&thumbnail=1680x0&quality=96&stripmeta=0",
image_urls: ["https://imglf4.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJUFczb2RKSVlpMHJkNy9kc3BSQVQvQm5DNzB4eVhxay9nPT0.png"],
assert_equal(["https://imglf5.lf127.net/img/Mm55d3lNK2tJUWpNTjVLN0MvaTRDc1UvQUFLMGszOHRvSjV6S3VSa1lwa3BDWUtVOWpBTHBnPT0.jpg"], source1.image_urls) profile_url: nil,
assert_not_empty(source1.tags) download_size: 2_739_443
end )
end end
context "A bad link" do context "A lofter post with commentary under <.content .text>" do
should "correctly get the full size" do strategy_should_work(
source = Source::Extractor.find("https://imglf4.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJUFczb2RKSVlpMHJkNy9kc3BSQVQvQm5DNzB4eVhxay9nPT0.png?imageView&thumbnail=1680x0&quality=96&stripmeta=0") "https://yuli031458.lofter.com/post/3163d871_1cbdc5f6d",
assert_equal(["https://imglf4.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJUFczb2RKSVlpMHJkNy9kc3BSQVQvQm5DNzB4eVhxay9nPT0.png"], source.image_urls) image_urls: ["https://imglf5.lf127.net/img/Mm55d3lNK2tJUWpNTjVLN0MvaTRDc1UvQUFLMGszOHRvSjV6S3VSa1lwa3BDWUtVOWpBTHBnPT0.jpg"],
assert_nothing_raised { source.to_h } tags: ["明日方舟", "阿米娅"],
end dtext_artist_commentary_desc: "Amiya"
)
end
context "A lofter post with commentary under <#post .description>" do
strategy_should_work(
"https://chengyeliuli.lofter.com/post/1d127639_2b6e850c8",
image_urls: ["https://imglf3.lf127.net/img/d28aeb098a69b1d2/ZmltbmVjOU9BRzFHVTVnTkNmc0V0NDlSRnNrdENIWWwyZkFreTJJd0duRT0.jpg"],
dtext_artist_commentary_desc: /練習\s+画画卡姐~/
)
end end
context "A dead link" do context "A dead link" do
should "not raise anything" do strategy_should_work(
source = Source::Extractor.find("https://gxszdddd.lofter.com/post/322595b1_1ca5e6f66") "https://gxszdddd.lofter.com/post/322595b1_1ca5e6f66",
assert_nothing_raised { source.to_h } deleted: true
end )
end end
should "Parse Lofter URLs correctly" do should "Parse Lofter URLs correctly" do