From a45ae09d723269d52a88193fa0d07e9b33caaef4 Mon Sep 17 00:00:00 2001 From: BrokenEagle Date: Sun, 29 Mar 2020 19:23:25 +0000 Subject: [PATCH] Account for additional Twitter video image links --- app/logical/sources/strategies/twitter.rb | 14 +++++++++++--- test/unit/sources/twitter_test.rb | 12 ++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/logical/sources/strategies/twitter.rb b/app/logical/sources/strategies/twitter.rb index 5f4e33d3a..2fcfb5e89 100644 --- a/app/logical/sources/strategies/twitter.rb +++ b/app/logical/sources/strategies/twitter.rb @@ -5,10 +5,16 @@ module Sources::Strategies # https://pbs.twimg.com/media/EBGbJe_U8AA4Ekb.jpg # https://pbs.twimg.com/media/EBGbJe_U8AA4Ekb?format=jpg&name=900x900 - BASE_IMAGE_URL = %r!\Ahttps?://pbs\.twimg\.com/(?media|tweet_video_thumb)!i + # https://pbs.twimg.com/tweet_video_thumb/ETkN_L3X0AMy1aT.jpg + # https://pbs.twimg.com/ext_tw_video_thumb/1243725361986375680/pu/img/JDA7g7lcw7wK-PIv.jpg + # https://pbs.twimg.com/amplify_video_thumb/1215590775364259840/img/lolCkEEioFZTb5dl.jpg + BASE_IMAGE_URL = %r!\Ahttps?://pbs\.twimg\.com/(?media|tweet_video_thumb|ext_tw_video_thumb|amplify_video_thumb)!i FILENAME1 = %r!(?[a-zA-Z0-9_-]+)\.(?\w+)!i FILENAME2 = %r!(?[a-zA-Z0-9_-]+)\?.*format=(?\w+)!i - IMAGE_URL = %r!#{BASE_IMAGE_URL}/#{Regexp.union(FILENAME1, FILENAME2)}!i + FILEPATH1 = %r!(?\d+/[\w_-]+/img)!i + FILEPATH2 = %r!(?\d+/img)!i + IMAGE_URL1 = %r!#{BASE_IMAGE_URL}/#{Regexp.union(FILENAME1, FILENAME2)}!i + IMAGE_URL2 = %r!#{BASE_IMAGE_URL}/#{Regexp.union(FILEPATH1, FILEPATH2)}/#{FILENAME1}!i # Twitter provides a list but it's inaccurate; some names ('intent') aren't # included and other names in the list aren't actually reserved. @@ -61,8 +67,10 @@ module Sources::Strategies end def image_urls - if url =~ IMAGE_URL + if url =~ IMAGE_URL1 ["https://pbs.twimg.com/#{$~[:media_type]}/#{$~[:file_name]}.#{$~[:file_ext]}:orig"] + elsif url =~ IMAGE_URL2 + ["https://pbs.twimg.com/#{$~[:media_type]}/#{$~[:file_path]}/#{$~[:file_name]}.#{$~[:file_ext]}:orig"] elsif api_response.present? api_response.dig(:extended_entities, :media).to_a.map do |media| if media[:type] == "photo" diff --git a/test/unit/sources/twitter_test.rb b/test/unit/sources/twitter_test.rb index 82aea7d47..5d061d669 100644 --- a/test/unit/sources/twitter_test.rb +++ b/test/unit/sources/twitter_test.rb @@ -38,6 +38,18 @@ module Sources @site = Sources::Strategies.find("https://pbs.twimg.com/tweet_video_thumb/ETkN_L3X0AMy1aT.jpg:small") assert_equal("https://pbs.twimg.com/tweet_video_thumb/ETkN_L3X0AMy1aT.jpg:orig", @site.image_url) end + + should "work when given an external video thumbnail" do + # https://twitter.com/chivedips/status/1243850897056133121 + @site = Sources::Strategies.find("https://pbs.twimg.com/ext_tw_video_thumb/1243725361986375680/pu/img/JDA7g7lcw7wK-PIv.jpg:small") + assert_equal("https://pbs.twimg.com/ext_tw_video_thumb/1243725361986375680/pu/img/JDA7g7lcw7wK-PIv.jpg:orig", @site.image_url) + end + + should "work when given an amplify video thumbnail" do + # https://twitter.com/UNITED_CINEMAS/status/1223138847417978881 + @site = Sources::Strategies.find("https://pbs.twimg.com/amplify_video_thumb/1215590775364259840/img/lolCkEEioFZTb5dl.jpg:small") + assert_equal("https://pbs.twimg.com/amplify_video_thumb/1215590775364259840/img/lolCkEEioFZTb5dl.jpg:orig", @site.image_url) + end end context "An animated gif" do