From d4d8489ef1c2dc073fa51fba92319dedc2a0d0df Mon Sep 17 00:00:00 2001 From: Steven Miers Date: Thu, 30 Apr 2015 19:31:44 -0500 Subject: [PATCH] VRFS-3007 : Fix faulty logic when extracting recording data. Addresses a few test failures. --- web/lib/utils.rb | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/web/lib/utils.rb b/web/lib/utils.rb index 0659b3dcf..13732db48 100644 --- a/web/lib/utils.rb +++ b/web/lib/utils.rb @@ -18,9 +18,8 @@ class Utils case site when 'rec_youtube' # regex derived from: https://gist.github.com/afeld/1254889 - if recording_url =~ /(youtu.be\/|youtube.com\/(watch\?(.*&)?v=|(embed|v)\/))([^\?&\"\'>]+)/ - rec_data["id"] = $5 - end + watch = (recording_url =~ /(youtu.be\/|youtube.com\/(watch\?(.*&)?v=|(embed|v)\/))([^\?&\"\'>]+)/) + rec_data["id"] = $5 if watch uri = URI.parse("https://gdata.youtube.com/feeds/api/videos/#{$5}?v=2&alt=json") @@ -28,9 +27,14 @@ class Utils http.use_ssl = true if uri.scheme == 'https' req = Net::HTTP::Get.new(uri) response = http.request(req) - json = JSON.parse(response.body) if response - rec_data["title"] = json["entry"]["title"]["$t"] - return rec_data unless rec_data.empty? + if response && response.body.length > 2 + json = response.body.force_encoding("UTF-8")# .encode('UTF-8', :invalid => :replace, :replace => '') + json_hash = JSON.parse(json) + + if watch && json_hash["entry"] && json_hash["entry"]["title"] + rec_data["title"] = json_hash["entry"]["title"]["$t"] + end + end when 'rec_soundcloud' if recording_url =~ /^https?:\/\/.*soundcloud.com\/.+/ @@ -46,11 +50,10 @@ class Utils if result =~ /property=\"og:title\" content=\"([\w\W]+)\">