From 5dd3151d5bb3af43df175eb07c39586422e4ce17 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 16 Jun 2017 10:49:36 -0500 Subject: [PATCH] twitter: convert commentary to dtext. * Convert hashtags and mentions to dtext links. * Replace http://t.co urls to the actual url. * Strip the http://t.co url linking to the tweet itself. --- app/logical/sources/strategies/twitter.rb | 17 +++++++++++++++++ test/unit/sources/twitter_test.rb | 12 ++++++++++++ 2 files changed, 29 insertions(+) diff --git a/app/logical/sources/strategies/twitter.rb b/app/logical/sources/strategies/twitter.rb index d1c14bc61..4d284bdbb 100644 --- a/app/logical/sources/strategies/twitter.rb +++ b/app/logical/sources/strategies/twitter.rb @@ -46,6 +46,23 @@ module Sources::Strategies true end + def dtext_artist_commentary_desc + url_replacements = Array(api_response.attrs[:entities][:urls]).map do |url:, expanded_url:, **attrs| + [url, expanded_url] + end + url_replacements += Array(api_response.attrs[:entities][:media]).map do |url:, expanded_url:, **attrs| + [url, ""] + end + url_replacements = url_replacements.to_h + + desc = artist_commentary_desc + desc = CGI::unescapeHTML(desc) + desc = desc.gsub(%r!https?://t\.co/[^[:space:]]+!i, url_replacements) + desc = desc.gsub(%r!#([^[:space:]]+)!, '"#\\1":[https://twitter.com/hashtag/\\1]') + desc = desc.gsub(%r!@([a-zA-Z0-9_]+)!, '"@\\1":[https://twitter.com/\\1]') + desc.strip + end + def status_id_from_url(url) if url =~ %r{^https?://(?:mobile\.)?twitter\.com/\w+/status/(\d+)} $1.to_i diff --git a/test/unit/sources/twitter_test.rb b/test/unit/sources/twitter_test.rb index 2ac30e3d3..41e8543dc 100644 --- a/test/unit/sources/twitter_test.rb +++ b/test/unit/sources/twitter_test.rb @@ -89,5 +89,17 @@ module Sources end end end + + context "A tweet" do + setup do + @site = Sources::Site.new("https://twitter.com/noizave/status/875768175136317440") + @site.get + end + + should "convert urls, hashtags, and mentions to dtext" do + desc = 'test "#foo":[https://twitter.com/hashtag/foo] "#ホワイトデー":[https://twitter.com/hashtag/ホワイトデー] "@noizave":[https://twitter.com/noizave]\'s blah http://www.example.com <>& 😀' + assert_equal(desc, @site.dtext_artist_commentary_desc) + end + end end end