PERF: Stop using oembed data for YouTube Onebox

PERF: Stop using oembed data for YouTube Onebox

All the data we need is now available in the opengraph tags, so there is no need to make a second request to fetch the oembed JSON

diff --git a/lib/onebox/engine/youtube_onebox.rb b/lib/onebox/engine/youtube_onebox.rb
index 8ee3ad1..b21ad0a 100644
--- a/lib/onebox/engine/youtube_onebox.rb
+++ b/lib/onebox/engine/youtube_onebox.rb
@@ -1,7 +1,5 @@
 # frozen_string_literal: true
 
-require 'onebox/oembed'
-
 module Onebox
   module Engine
     class YoutubeOnebox
@@ -15,10 +13,10 @@ module Onebox
       HEIGHT ||= 360
 
       def placeholder_html
-        if video_id
-          "<img src='https://i.ytimg.com/vi/#{video_id}/hqdefault.jpg' width='#{WIDTH}' height='#{HEIGHT}' #{video_oembed_data.title_attr}>"
-        elsif list_id
-          "<img src='#{list_thumbnail_url}' width='#{WIDTH}' height='#{HEIGHT}' #{list_oembed_data.title_attr}>"
+        og = get_opengraph.data
+
+        if video_id || list_id
+          "<img src='#{og[:image]}' width='#{WIDTH}' height='#{HEIGHT}' title='#{og[:title]}'>"
         else
           to_html
         end
@@ -53,7 +51,7 @@ module Onebox
       end
 
       def video_title
-        @video_title ||= video_oembed_data.title
+        @video_title ||= get_opengraph.data[:title]
       end
 
       private
@@ -81,29 +79,6 @@ module Onebox
         @list_id ||= params['list']
       end
 
-      def list_thumbnail_url
-        @list_thumbnail_url ||= begin
-          url = "https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/playlist?list=#{list_id}"
-          response = Onebox::Helpers.fetch_response(url) rescue "{}"
-          data = Onebox::Oembed.new(response)
-          data.thumbnail_url
-        rescue
-          nil
-        end
-      end
-
-      def video_oembed_data
-        url = "https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=#{video_id}"
-        response = Onebox::Helpers.fetch_response(url) rescue "{}"
-        Onebox::Oembed.new(response)
-      end
-
-      def list_oembed_data
-        url = "https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/playlist?list=#{list_id}"
-        response = Onebox::Helpers.fetch_response(url) rescue "{}"
-        Onebox::Oembed.new(response)
-      end
-
       def embed_params
         p = { 'feature' => 'oembed', 'wmode' => 'opaque' }
 
diff --git a/spec/fixtures/youtube-json.response b/spec/fixtures/youtube-json.response
deleted file mode 100644
index dda30a3..0000000
--- a/spec/fixtures/youtube-json.response
+++ /dev/null
@@ -1 +0,0 @@
-{"type":"video","provider_url":"https:\/\/www.youtube.com\/","thumbnail_url":"https:\/\/i.ytimg.com\/vi\/21Lk4YiASMo\/hqdefault.jpg","author_name":"korotto5810","title":"96neko - orange","thumbnail_width":480,"width":480,"version":"1.0","html":"\u003ciframe width=\"480\" height=\"270\" src=\"https:\/\/www.youtube.com\/embed\/21Lk4YiASMo?feature=oembed\" frameborder=\"0\" allowfullscreen\u003e\u003c\/iframe\u003e","thumbnail_height":360,"height":270,"author_url":"https:\/\/www.youtube.com\/user\/korotto5810","provider_name":"YouTube"}
\ No newline at end of file
diff --git a/spec/fixtures/youtube-list-json.response b/spec/fixtures/youtube-list-json.response
deleted file mode 100644
index 84c18d3..0000000
--- a/spec/fixtures/youtube-list-json.response
+++ /dev/null
@@ -1 +0,0 @@
-{"version":"1.0","title":"The web is what you make of it","thumbnail_url":"https:\/\/i.ytimg.com\/vi\/R4vkVHijdQk\/hqdefault.jpg","type":"video","height":270,"thumbnail_height":360,"provider_name":"YouTube","provider_url":"https:\/\/www.youtube.com\/","author_name":"Google Chrome","width":480,"author_url":"https:\/\/www.youtube.com\/user\/googlechrome","html":"\u003ciframe width=\"480\" height=\"270\" src=\"https:\/\/www.youtube.com\/embed\/videoseries?list=PL5308B2E5749D1696\" frameborder=\"0\" allowfullscreen\u003e\u003c\/iframe\u003e","thumbnail_width":480}
\ No newline at end of file
diff --git a/spec/fixtures/youtube-playlist.response b/spec/fixtures/youtube-playlist.response
new file mode 100644
index 0000000..1b67e5f
--- /dev/null
+++ b/spec/fixtures/youtube-playlist.response
@@ -0,0 +1,221 @@
+
+<!doctype html><html  style="font-size: 10px;font-family: Roboto, Arial, sans-serif;" lang="en-GB" dir="ltr" gl="GB"><head><meta http-equiv="X-UA-Compatible" content="IE=edge" />
+
+    <link rel="alternate" media="handheld" href="https://m.youtube.com/playlist?list=PL5308B2E5749D1696">
+    <link rel="alternate" media="only screen and (max-width: 640px)" href="https://m.youtube.com/playlist?list=PL5308B2E5749D1696">
+  <meta name="theme-color" content="#ff0000">
+
+        <title>The web is what you make of it - YouTube</title>
+
+    <link rel="canonical" href="http://www.youtube.com/playlist?list=PL5308B2E5749D1696">
+
+    <meta property="og:site_name" content="YouTube">
+    <meta property="og:url" content="http://www.youtube.com/playlist?list=PL5308B2E5749D1696">
+    <meta property="og:title" content="The web is what you make of it">
+    <meta property="og:description" content="More and more, the web is simply just part of your life and helps you get things done. See how people are using the web to do amazing things. The web is what...">
+
+      <meta property="og:image" content="https://i.ytimg.com/vi/3lp3RpC-60U/hqdefault.jpg?sqp=-oaymwEWCKgBEF5IWvKriqkDCQgBFQAAiEIYAQ==&amp;rs=AOn4CLDttPySHo_45IiyB4ryRNFk-LgvHA">
+        <meta property="og:image:width" content="168">
+        <meta property="og:image:height" content="94">
+      <meta property="og:image" content="https://i.ytimg.com/vi/3lp3RpC-60U/hqdefault.jpg?sqp=-oaymwEWCMQBEG5IWvKriqkDCQgBFQAAiEIYAQ==&amp;rs=AOn4CLCj6seFiytqxH-qknMcTT2SjEcZ5A">
+        <meta property="og:image:width" content="196">
+        <meta property="og:image:height" content="110">
+      <meta property="og:image" content="https://i.ytimg.com/vi/3lp3RpC-60U/hqdefault.jpg?sqp=-oaymwEXCPYBEIoBSFryq4qpAwkIARUAAIhCGAE=&amp;rs=AOn4CLBDEBLXBEboQgszge_7pkirireZ9g">
+        <meta property="og:image:width" content="246">
+        <meta property="og:image:height" content="138">
+      <meta property="og:image" content="https://i.ytimg.com/vi/3lp3RpC-60U/hqdefault.jpg?sqp=-oaymwEXCNACELwBSFryq4qpAwkIARUAAIhCGAE=&amp;rs=AOn4CLDnDW--csTM1BYh7lt8nCGOmuJg3A">
+        <meta property="og:image:width" content="336">
+        <meta property="og:image:height" content="188">
+
+    <meta name="apple-itunes-app" content="app-id=544007664" app-argument="http://www.youtube.com/playlist?list=PL5308B2E5749D1696">
+
+    <meta property="al:ios:app_store_id" content="544007664">
+    <meta property="al:ios:app_name" content="YouTube">
+      <meta property="al:ios:url" content="http://www.youtube.com/playlist?list=PL5308B2E5749D1696&amp;feature=applinks">
+    <meta property="al:android:url" content="http://www.youtube.com/playlist?list=PL5308B2E5749D1696&amp;feature=applinks">
+    <meta property="al:android:app_name" content="YouTube">
+    <meta property="al:android:package" content="com.google.android.youtube">
+      <meta property="al:web:url" content="http://www.youtube.com/playlist?list=PL5308B2E5749D1696&amp;feature=applinks">
+      <meta property="al:web:should_fallback" content="true">
+
+    <meta property="og:type" content="website">
+
+
+    <meta property="fb:app_id" content="87741124305">
+      <meta property="og:restrictions:age" content="18+">
+
+    <meta name="twitter:card" content="summary">
+    <meta name="twitter:site" content="@YouTube">
+    <meta name="twitter:url" content="http://www.youtube.com/playlist?list=PL5308B2E5749D1696">
+    <meta name="twitter:title" content="The web is what you make of it">
+    <meta name="twitter:description" content="More and more, the web is simply just part of your life and helps you get things done. See how people are using the web to do amazing things. The web is what...">
+      <meta name="twitter:image" content="https://i.ytimg.com/vi/3lp3RpC-60U/hqdefault.jpg?sqp=-oaymwEWCKgBEF5IWvKriqkDCQgBFQAAiEIYAQ==&amp;rs=AOn4CLDttPySHo_45IiyB4ryRNFk-LgvHA">

[... diff too long, it was truncated ...]

GitHub sha: c40e6f49