From 94e48aa6f39b49b1d0bf48a4c97c92f0d9afe086 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 16 Jun 2017 18:37:57 -0500 Subject: [PATCH] pixiv: convert commentary to dtext. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Convert pixiv links to dtext in this manner: * https://www.pixiv.net/member_illust.php?mode=medium&illust_id=1234 => pixiv #1234 "»":[/posts?tags=pixiv:1234] * https://www.pixiv.net/member.php?id=1234 => "user/1234":[https://www.pixiv.net/member.php?id=1234] "»":[/artists?search[url_matches]=https://www.pixiv.net/member.php?id=1234] --- app/logical/sources/strategies/pixiv.rb | 17 +++++++++++++++++ test/unit/sources/pixiv_test.rb | 13 +++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/app/logical/sources/strategies/pixiv.rb b/app/logical/sources/strategies/pixiv.rb index c7b4b1fc7..cd0b3c3c7 100644 --- a/app/logical/sources/strategies/pixiv.rb +++ b/app/logical/sources/strategies/pixiv.rb @@ -107,6 +107,23 @@ module Sources @metadata.pages end + def self.to_dtext(text) + text = text.gsub(%r!https?://www\.pixiv\.net/member_illust\.php\?mode=medium&illust_id=([0-9]+)!i) do |match| + pixiv_id = $1 + %(pixiv ##{pixiv_id} "»":[/posts?tags=pixiv:#{pixiv_id}]) + end + + text = text.gsub(%r!https?://www\.pixiv\.net/member\.php\?id=([0-9]+)!i) do |match| + member_id = $1 + profile_url = "https://www.pixiv.net/member.php?id=#{member_id}" + search_params = {"search[url_matches]" => profile_url}.to_param + + %("user/#{member_id}":[#{profile_url}] "»":[/artists?#{search_params}]) + end + + text + end + def illust_id_from_url if sample_image? || full_image? || work_page? illust_id_from_url! diff --git a/test/unit/sources/pixiv_test.rb b/test/unit/sources/pixiv_test.rb index c803a0c78..a96540ca5 100644 --- a/test/unit/sources/pixiv_test.rb +++ b/test/unit/sources/pixiv_test.rb @@ -117,14 +117,19 @@ module Sources end context "fetching the commentary" do - setup do - get_source("https://www.pixiv.net/member_illust.php?mode=medium&illust_id=46337015") - end - should "work when the description is blank" do + get_source("https://www.pixiv.net/member_illust.php?mode=medium&illust_id=46337015") + assert_equal("Illustration (PNG) - foo & bar", @site.dtext_artist_commentary_title) assert_equal("", @site.dtext_artist_commentary_desc) end + + should "convert illust links and member links to dtext" do + get_source("https://www.pixiv.net/member_illust.php?mode=medium&illust_id=63421642") + + dtext_desc = %(foo 【pixiv #46337015 "»":[/posts?tags=pixiv:46337015]】bar 【pixiv #14901720 "»":[/posts?tags=pixiv:14901720]】\r\n\r\nbaz【"user/83739":[https://www.pixiv.net/member.php?id=83739] "»":[/artists?search%5Burl_matches%5D=https%3A%2F%2Fwww.pixiv.net%2Fmember.php%3Fid%3D83739]】) + assert_equal(dtext_desc, @site.dtext_artist_commentary_desc) + end end end end