From 14196f36d89a5150c6c126fab792998891e6b421 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 11 Jun 2017 00:00:25 -0500 Subject: [PATCH] pawoo: convert commentary to dtext. --- app/logical/sources/strategies/pawoo.rb | 35 ++++++++++++++++++++++++- test/unit/sources/pawoo_test.rb | 10 +++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/app/logical/sources/strategies/pawoo.rb b/app/logical/sources/strategies/pawoo.rb index 33c478453..1605332e0 100644 --- a/app/logical/sources/strategies/pawoo.rb +++ b/app/logical/sources/strategies/pawoo.rb @@ -27,8 +27,12 @@ module Sources::Strategies "Pawoo" end + def api_response + @response ||= PawooApiClient.new.get_status(normalized_url) + end + def get - response = PawooApiClient.new.get_status(normalized_url) + response = api_response @artist_name = response.account_name @profile_url = response.account_profile_url @image_url = response.image_urls.first @@ -49,5 +53,34 @@ module Sources::Strategies def normalizable_for_artist_finder? true end + + def dtext_artist_commentary_desc + to_dtext(artist_commentary_desc) + end + + def to_dtext(text) + html = Nokogiri::HTML.fragment(text) + + dtext = html.children.map do |element| + case element.name + when "text" + element.content + when "p" + to_dtext(element.inner_html) + "\n\n" + when "a" + # don't include links to the toot itself. + media_urls = api_response.json["media_attachments"].map { |attr| attr["text_url"] } + next if element.attribute("href").value.in?(media_urls) + + title = to_dtext(element.inner_html) + url = element.attributes["href"].value + %("#{title}":[#{url}]) + else + to_dtext(element.inner_html) + end + end.join.strip + + dtext + end end end diff --git a/test/unit/sources/pawoo_test.rb b/test/unit/sources/pawoo_test.rb index 9e0338b32..bfa34a3cd 100644 --- a/test/unit/sources/pawoo_test.rb +++ b/test/unit/sources/pawoo_test.rb @@ -19,6 +19,16 @@ module Sources should "get the image url" do assert_equal("https://img.pawoo.net/media_attachments/files/000/128/953/original/4c0a06087b03343f.png", @site.image_url) end + + should "get the commentary" do + desc = '

a mind forever voyaging through strange seas of thought alone pawoo.net/media/9hJzXvwxVl1Cez

' + assert_equal(desc, @site.artist_commentary_desc) + end + + should "get the dtext-ified commentary" do + desc = 'a mind forever voyaging through strange seas of thought alone' + assert_equal(desc, @site.dtext_artist_commentary_desc) + end end context "The source site for a https://pawoo.net/$user/$id url" do