diff --git a/app/logical/source/url.rb b/app/logical/source/url.rb index d72d3f696..05a1a9519 100644 --- a/app/logical/source/url.rb +++ b/app/logical/source/url.rb @@ -19,6 +19,7 @@ module Source class URL < Danbooru::URL SUBCLASSES = [ Source::URL::Twitter, + Source::URL::TwitPic, ] # Parse a URL into a subclass of Source::URL, or raise an exception if the URL is not a valid HTTP or HTTPS URL. diff --git a/app/logical/source/url/twit_pic.rb b/app/logical/source/url/twit_pic.rb new file mode 100644 index 000000000..b292d759d --- /dev/null +++ b/app/logical/source/url/twit_pic.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true +# +# Page URLs: +# +# * https://twitpic.com/dvitq3 (live) +# * https://twitpic.com/9tgik8 (dead) +# +# Image URLs: +# +# # Live. Image for https://twitpic.com/dvitq3. `dvitq3` is base36 for 839006715. +# * https://dn3pm25xmtlyu.cloudfront.net/photos/large/839006715.jpg?Expires=1646850828&Signature=d60CmLlmNqZJvOTteTOan13QWZ8gY3C4rUWCkh-IUoRr012vYtUYtip74GslGwCG0dxV5mpUpVFkaVZf16PiY7CsTdpAlA8Pmu2tN98D2dmC5FuW9KhhygDv6eFC8faoaGEyj~ArLuwz-8lC6Y05TVf0FgweeWwsRxFOfD5JHgCeIB0iZqzUx1t~eb6UMAWvbaKpfgvcp2oaDuCdZlMNi9T5OUBFoTh2DfnGy8t5COys1nOYYfZ9l69TDvVb2PKBaV8lsKK9xMwjoJNaWa1HL5S4MgODS5hiNDvycoBpu9KUvQ7q~rhC8cV6ZNctB5H9u~MmvBPoTKfy4w37cSc5uw__&Key-Pair-Id=APKAJROXZ7FN26MABHYA +# +# # Live. Image for https://twitpic.com/dks0tb. +# * http://d3j5vwomefv46c.cloudfront.net/photos/large/820960031.jpg?1384107199 +# +# # Dead. Old images for http://twitpic/dvitq3. +# * https://twitpic.com/show/large/dvitq3.jpg +# * https://o.twimg.com/2/proxy.jpg?t=HBgpaHR0cHM6Ly90d2l0cGljLmNvbS9zaG93L2xhcmdlL2R2aXRxMy5qcGcUsAkUsg0AFgASAA&s=NeY89zVAEpDjLcxZ_8KOoF7VGr2dm1Vc3HIozPy__Ng +# +# Profile URLs: +# +# * http://twitpic.com/photos/Type10TK (dead) +# +class Source::URL::TwitPic < Source::URL + attr_reader :base36_id + + def self.match?(url) + url.host.in?(%w[twitpic.com o.twimg.com dn3pm25xmtlyu.cloudfront.net d3j5vwomefv46c.cloudfront.net]) + end + + def parse + case [domain, *path_segments] + + # https://twitpic.com/carwkf + in "twitpic.com", base36_id + @base36_id = base36_id + + # https://twitpic.com/show/large/carwkf.jpg + in "twitpic.com", "show", size, filename + @filename, @file_ext = filename.split(".") + @base36_id = @filename + + # https://o.twimg.com/1/proxy.jpg?t=FQQVBBgpaHR0cHM6Ly90d2l0cGljLmNvbS9zaG93L2xhcmdlL2NhcndrZi5qcGcUBBYAEgA&s=y8haxddqxJYpWql9uVnP3aoFFS7rA10vOGPdTO5HXvk + # https://o.twimg.com/2/proxy.jpg?t=HBgpaHR0cHM6Ly90d2l0cGljLmNvbS9zaG93L2xhcmdlL2R0bnVydS5qcGcUsAkU0ggAFgASAA&s=dnN4DHCdnojC-iCJWdvZ-UZinrlWqAP7k7lmll2fTxs + in "twimg.com", subdir, "proxy.jpg" if params[:t].present? + # FQQVBBgpaHR0cHM6Ly90d2l0cGljLmNvbS9zaG93L2xhcmdlL2NhcndrZi5qcGcUBBYAEgA + @base64_id = params[:t] + + # "\x15\x04\x15\x04\x18)https://twitpic.com/show/large/carwkf.jpg\x14\x04\x16\x00\x12\x00" + @decoded_base64_id = Base64.decode64(@base64_id) + + # https://twitpic.com/show/large/carwkf.jpg + @decoded_url = URI.extract(@decoded_base64_id, %w[http https]).first + + # carwkf + @base36_id = Source::URL.parse(@decoded_url).base36_id if @decoded_url.present? + + # http://d3j5vwomefv46c.cloudfront.net/photos/large/820960031.jpg?1384107199 + # https://dn3pm25xmtlyu.cloudfront.net/photos/large/839006715.jpg?Expires=1646850828&Signature=d60CmLlmNqZJvOTteTOan13QWZ8gY3C4rUWCkh-IUoRr012vYtUYtip74GslGwCG0dxV5mpUpVFkaVZf16PiY7CsTdpAlA8Pmu2tN98D2dmC5FuW9KhhygDv6eFC8faoaGEyj~ArLuwz-8lC6Y05TVf0FgweeWwsRxFOfD5JHgCeIB0iZqzUx1t~eb6UMAWvbaKpfgvcp2oaDuCdZlMNi9T5OUBFoTh2DfnGy8t5COys1nOYYfZ9l69TDvVb2PKBaV8lsKK9xMwjoJNaWa1HL5S4MgODS5hiNDvycoBpu9KUvQ7q~rhC8cV6ZNctB5H9u~MmvBPoTKfy4w37cSc5uw__&Key-Pair-Id=APKAJROXZ7FN26MABHYA + in /cloudfront\.net/, "photos", size, filename + @filename, @file_ext = filename.split(".") + @base36_id = @filename.to_i.to_s(36) + + else + end + end + + def page_url + return nil unless base36_id.present? + "https://twitpic.com/#{base36_id}" + end +end diff --git a/app/logical/sources/strategies.rb b/app/logical/sources/strategies.rb index f2699738e..105fb6062 100644 --- a/app/logical/sources/strategies.rb +++ b/app/logical/sources/strategies.rb @@ -22,6 +22,7 @@ module Sources Strategies::Lofter, Strategies::Foundation, Strategies::Plurk, + Strategies::TwitPic, ] end diff --git a/app/logical/sources/strategies/null.rb b/app/logical/sources/strategies/null.rb index 7197a285e..fd53912f0 100644 --- a/app/logical/sources/strategies/null.rb +++ b/app/logical/sources/strategies/null.rb @@ -17,11 +17,6 @@ module Sources def normalize_for_source case url - when %r{\Ahttps?://(?:d3j5vwomefv46c|dn3pm25xmtlyu)\.cloudfront\.net/photos/large/(\d+)\.}i - base_10_id = $1.to_i - base_36_id = base_10_id.to_s(36) - "https://twitpic.com/#{base_36_id}" - when %r{\Ahttp://www\.karabako\.net/images(?:ub)?/karabako_(\d+)(?:_\d+)?\.}i "http://www.karabako.net/post/view/#{$1}" diff --git a/app/logical/sources/strategies/twit_pic.rb b/app/logical/sources/strategies/twit_pic.rb new file mode 100644 index 000000000..eef260b7d --- /dev/null +++ b/app/logical/sources/strategies/twit_pic.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +# @see Source::URL::TwitPic +module Sources::Strategies + class TwitPic < Base + def match? + parsed_url&.site_name == "TwitPic" + end + + def site_name + parsed_url.site_name + end + + def normalize_for_source + parsed_url.page_url || url + end + end +end diff --git a/app/logical/sources/strategies/twitter.rb b/app/logical/sources/strategies/twitter.rb index 0234add57..393e2606f 100644 --- a/app/logical/sources/strategies/twitter.rb +++ b/app/logical/sources/strategies/twitter.rb @@ -23,12 +23,12 @@ module Sources::Strategies Danbooru.config.twitter_api_key.present? && Danbooru.config.twitter_api_secret.present? end - def domains - ["twitter.com", "twimg.com"] + def match? + parsed_url&.site_name == "Twitter" end def site_name - "Twitter" + parsed_url.site_name end def image_urls @@ -118,18 +118,6 @@ module Sources::Strategies "https://twitter.com/#{tag_name_from_url}/status/#{status_id}" elsif status_id.present? "https://twitter.com/i/web/status/#{status_id}" - elsif url =~ %r{\Ahttps?://(?:o|image-proxy-origin)\.twimg\.com/\d/proxy\.jpg\?t=(\w+)&}i - str = Base64.decode64($1) - source = URI.extract(str, %w[http https]) - if source.any? - source = source[0] - if source =~ %r{^https?://twitpic.com/show/large/[a-z0-9]+}i - source.gsub!(%r{show/large/}, "") - index = source.rindex(".") - source = source[0..index - 1] - end - source - end end end diff --git a/test/unit/sources/null_test.rb b/test/unit/sources/null_test.rb index c2ff5a07e..fc6496267 100644 --- a/test/unit/sources/null_test.rb +++ b/test/unit/sources/null_test.rb @@ -29,11 +29,6 @@ module Sources end context "normalizing for source" do - should "normalize twitpic links" do - source = "http://d3j5vwomefv46c.cloudfront.net/photos/large/820960031.jpg?1384107199" - assert_equal("https://twitpic.com/dks0tb", Sources::Strategies.normalize_source(source)) - end - should "normalize karabako links" do source = "http://www.karabako.net/images/karabako_38835.jpg" assert_equal("http://www.karabako.net/post/view/38835", Sources::Strategies.normalize_source(source)) diff --git a/test/unit/sources/twit_pic_test.rb b/test/unit/sources/twit_pic_test.rb new file mode 100644 index 000000000..6a6e8b603 --- /dev/null +++ b/test/unit/sources/twit_pic_test.rb @@ -0,0 +1,22 @@ +require 'test_helper' + +module Sources + class TwitPicTest < ActiveSupport::TestCase + context "normalizing for source" do + should "normalize d3j5vwomefv46c.cloudfront.net links" do + source = "http://d3j5vwomefv46c.cloudfront.net/photos/large/820960031.jpg?1384107199" + assert_equal("https://twitpic.com/dks0tb", Sources::Strategies.normalize_source(source)) + end + + should "normalize dn3pm25xmtlyu.cloudfront.net links" do + source = "https://dn3pm25xmtlyu.cloudfront.net/photos/large/839006715.jpg?Expires=1646850828&Signature=d60CmLlmNqZJvOTteTOan13QWZ8gY3C4rUWCkh-IUoRr012vYtUYtip74GslGwCG0dxV5mpUpVFkaVZf16PiY7CsTdpAlA8Pmu2tN98D2dmC5FuW9KhhygDv6eFC8faoaGEyj~ArLuwz-8lC6Y05TVf0FgweeWwsRxFOfD5JHgCeIB0iZqzUx1t~eb6UMAWvbaKpfgvcp2oaDuCdZlMNi9T5OUBFoTh2DfnGy8t5COys1nOYYfZ9l69TDvVb2PKBaV8lsKK9xMwjoJNaWa1HL5S4MgODS5hiNDvycoBpu9KUvQ7q~rhC8cV6ZNctB5H9u~MmvBPoTKfy4w37cSc5uw__&Key-Pair-Id=APKAJROXZ7FN26MABHYA" + assert_equal("https://twitpic.com/dvitq3", Sources::Strategies.normalize_source(source)) + end + + should "normalize o.twimg.com links" do + source = "https://o.twimg.com/2/proxy.jpg?t=HBgpaHR0cHM6Ly90d2l0cGljLmNvbS9zaG93L2xhcmdlL2R0bnVydS5qcGcUsAkU0ggAFgASAA&s=dnN4DHCdnojC-iCJWdvZ-UZinrlWqAP7k7lmll2fTxs" + assert_equal("https://twitpic.com/dtnuru", Sources::Strategies.normalize_source(source)) + end + end + end +end diff --git a/test/unit/sources/twitter_test.rb b/test/unit/sources/twitter_test.rb index 050dc6ddc..574cce3ea 100644 --- a/test/unit/sources/twitter_test.rb +++ b/test/unit/sources/twitter_test.rb @@ -321,11 +321,6 @@ module Sources assert_equal(source2, Sources::Strategies.normalize_source(source3)) assert_equal(source2, Sources::Strategies.normalize_source(source4)) end - - should "normalize twimg twitpic correctly" do - source = "https://o.twimg.com/2/proxy.jpg?t=HBgpaHR0cHM6Ly90d2l0cGljLmNvbS9zaG93L2xhcmdlL2R0bnVydS5qcGcUsAkU0ggAFgASAA&s=dnN4DHCdnojC-iCJWdvZ-UZinrlWqAP7k7lmll2fTxs" - assert_equal("https://twitpic.com/dtnuru", Sources::Strategies.normalize_source(source)) - end end end end