Files
danbooru/test/unit/sources/weibo_test.rb
evazion 3aa5cab2aa sources: refactor normalize_for_source.
`normalize_for_source` was used to convert image URLs to page URLs when displaying sources
on the post show page. Move all the code for converting image URLs to page URLs from
`Sources::Strategies#normalize_for_source` to `Source::URL#page_url`.

Before we had to be very careful in source strategies not to make any network calls in
`normalize_for_source`, since it was used in the view for the post show page. Now all the
code for generating page URLs is isolated in Source::URL, which makes source strategies
simpler. It also makes it easier to check if a source is an image URL or page URL, and if
the image URL is convertible to a page URL, which will make autotagging bad_link or
bad_source feasible.

Finally, this fixes it to generate better page URLs in a handful of cases:

* https://www.artstation.com/artwork/qPVGP instead of https://anubis1982918.artstation.com/projects/qPVGP
* https://yande.re/post/show?md5=b4b1d11facd1700544554e4805d47bb6s instead of https://yande.re/post?tags=md5:b4b1d11facd1700544554e4805d47bb6
* http://gallery.minitokyo.net/view/365677 instead of http://gallery.minitokyo.net/download/365677
* https://valkyriecrusade.fandom.com/wiki/File:Crimson_Hatsune_H.png instead of https://valkyriecrusade.wikia.com/wiki/File:Crimson_Hatsune_H.png
* https://rule34.paheal.net/post/view/852405 instead of https://rule34.paheal.net/post/list/md5:854806addcd3b1246424e7cea49afe31/1
2022-03-23 01:34:04 -05:00

133 lines
5.1 KiB
Ruby

require "test_helper"
module Sources
class WeiboTest < ActiveSupport::TestCase
setup do
# Skip in CI to work around test failures due to rate limiting by Weibo.
skip if ENV["CI"].present?
end
context "A post with multiple pictures" do
setup do
@site = Sources::Strategies.find("https://www.weibo.com/5501756072/J2UNKfbqV?type=comment#_rnd1590548401855")
end
should "extract all the image urls" do
urls = %w[
https://wx1.sinaimg.cn/large/0060kO5aly1gezsyt5xvhj30ok0sgtc9.jpg
https://wx3.sinaimg.cn/large/0060kO5aly1gezsyuaas1j30go0sgjtj.jpg
https://wx3.sinaimg.cn/large/0060kO5aly1gezsys1ai9j30gi0sg0v9.jpg
]
assert_equal(urls, @site.image_urls)
end
should "get the correct commentary" do
assert_not_nil(@site.artist_commentary_desc)
end
should "get the profile url" do
assert_equal("https://www.weibo.com/u/5501756072", @site.profile_url)
end
should "set the right source" do
assert_equal("https://www.weibo.com/5501756072/J2UNKfbqV", @site.canonical_url)
end
should "download an image" do
assert_downloaded(134_721, @site.image_urls[0])
assert_downloaded(84_124, @site.image_urls[1])
assert_downloaded(97_878, @site.image_urls[2])
end
should "get the tags" do
tags = [
%w[fgo https://s.weibo.com/weibo/fgo],
%w[Alter组 https://s.weibo.com/weibo/Alter组],
]
assert_equal(tags, @site.tags)
end
should "find the correct artist" do
@artist = FactoryBot.create(:artist, name: "nipi27", url_string: "https://www.weibo.com/u/5501756072")
assert_equal([@artist], @site.artists)
end
end
context "A deleted or not existing picture" do
should "still find the artist name" do
site = Sources::Strategies.find("https://www.weibo.com/5501756072/AsdAsdAsd")
artist = FactoryBot.create(:artist, name: "nipi27", url_string: "https://www.weibo.com/u/5501756072")
assert_equal([artist], site.artists)
end
end
context "A post with video" do
should "get the correct video" do
site = Sources::Strategies.find("https://www.weibo.com/5501756072/IF9fugHzj")
assert_downloaded(7_676_656, site.image_urls.sole)
end
end
context "A direct image sample upload" do
should "get the largest version" do
sample = Sources::Strategies.find("https://wx3.sinaimg.cn/mw690/a00fa34cly1gf62g2n8z3j21yu2jo1ky.jpg")
assert_equal(["https://wx3.sinaimg.cn/large/a00fa34cly1gf62g2n8z3j21yu2jo1ky.jpg"], sample.image_urls)
end
end
context "A multi-page upload" do
should "set the right source" do
url = "https://wx1.sinaimg.cn/large/7eb64558gy1fnbryriihwj20dw104wtu.jpg"
ref = "https://photo.weibo.com/2125874520/wbphotos/large/mid/4194742441135220/pid/7eb64558gy1fnbryb5nzoj20dw10419t"
site = Sources::Strategies.find(url, ref)
assert_equal("https://www.weibo.com/2125874520/FDKGo4Lk0", site.canonical_url)
end
end
context "A deleted url" do
should "not raise errors" do
url = "https://weibo.com/5265069929/LiLnMENgs"
assert_nothing_raised { Sources::Strategies.find(url).to_h }
end
end
context "A m.weibo.cn/detail url" do
should "work" do
@site = Sources::Strategies.find("https://m.weibo.cn/detail/4506950043618873")
assert_equal(%w[
https://wx1.sinaimg.cn/large/0060kO5aly1gezsyt5xvhj30ok0sgtc9.jpg
https://wx3.sinaimg.cn/large/0060kO5aly1gezsyuaas1j30go0sgjtj.jpg
https://wx3.sinaimg.cn/large/0060kO5aly1gezsys1ai9j30gi0sg0v9.jpg
], @site.image_urls)
assert_equal("https://www.weibo.com/5501756072/J2UNKfbqV", @site.page_url)
assert_equal("https://www.weibo.com/5501756072/J2UNKfbqV", @site.canonical_url)
assert_equal("https://www.weibo.com/u/5501756072", @site.profile_url)
assert_equal(%w[fgo Alter组], @site.tags.map(&:first))
assert_equal("阿尔托莉雅厨", @site.artist_name)
end
end
context "generating page urls" do
should "work" do
source1 = "https://www.weibo.com/3150932560/H4cFbeKKA?from=page_1005053150932560_profile&wvr=6&mod=weibotime"
source2 = "https://photo.weibo.com/2125874520/wbphotos/large/mid/4242129997905387/pid/7eb64558ly1friyzhj44lj20dw2qxe81"
source3 = "https://m.weibo.cn/status/4173757483008088?luicode=20000061&lfid=4170879204256635"
source4 = "https://tw.weibo.com/SEINEN/4098035921690224"
assert_equal("https://www.weibo.com/3150932560/H4cFbeKKA", Source::URL.page_url(source1))
assert_equal("https://m.weibo.cn/detail/4242129997905387", Source::URL.page_url(source2))
assert_equal("https://m.weibo.cn/status/4173757483008088", Source::URL.page_url(source3))
assert_equal("https://m.weibo.cn/detail/4098035921690224", Source::URL.page_url(source4))
assert_nil(Source::URL.page_url("https://weibo.com/u/"))
assert_nil(Source::URL.page_url("https://www.weibo.com/4ubergine/photos"))
end
end
end
end