VRFS-2701 updated site validator lib to extract recording title for YouTube and SoundCloud URLs
This commit is contained in:
parent
a87c08afe3
commit
2f8d048dde
|
|
@ -149,9 +149,12 @@ context.JK.RecordingSourceValidator = class RecordingSourceValidator extends Sit
|
|||
@add_btn.removeClass('disabled')
|
||||
|
||||
if @site_status
|
||||
@recording_sources.push({ url: response.data, recording_id: response.recording_id })
|
||||
@recording_sources.push({ url: response.data, recording_id: response.recording_id, recording_title: response.recording_title })
|
||||
if @site_success_callback
|
||||
@site_success_callback(@input_div)
|
||||
else
|
||||
if @site_fail_callback
|
||||
@site_fail_callback(@input_div)
|
||||
|
||||
processSiteCheckFail: (response) =>
|
||||
super(response)
|
||||
|
|
|
|||
|
|
@ -760,9 +760,10 @@ class ApiUsersController < ApiController
|
|||
if site.blank? || 'url'==site
|
||||
url = data
|
||||
elsif Utils.recording_source?(site)
|
||||
rec_id = Utils.extract_recording_id(site, data)
|
||||
if rec_id
|
||||
render json: { message: 'Valid Site', recording_id: rec_id, data: data }, status: 200
|
||||
rec_data = Utils.extract_recording_data(site, data)
|
||||
binding.pry
|
||||
if rec_data
|
||||
render json: { message: 'Valid Site', recording_id: rec_data["id"], recording_title: rec_data["title"], data: data }, status: 200
|
||||
return
|
||||
else
|
||||
render json: { message: 'Invalid Site', data: data, errors: { site: ["Could not detect recording identifier"] } }, status: 200
|
||||
|
|
|
|||
|
|
@ -172,10 +172,10 @@
|
|||
window.twitterValidator = new JK.SiteValidator('twitter', userNameSuccessCallback, userNameFailCallback);
|
||||
twitterValidator.init();
|
||||
|
||||
window.soundCloudRecordingValidator = new JK.RecordingSourceValidator('rec_soundcloud', siteSuccessCallback, siteFailCallback);
|
||||
window.soundCloudRecordingValidator = new JK.RecordingSourceValidator('rec_soundcloud', soundCloudSuccessCallback, siteFailCallback);
|
||||
soundCloudRecordingValidator.init();
|
||||
|
||||
window.youTubeRecordingValidator = new JK.RecordingSourceValidator('rec_youtube', siteSuccessCallback, siteFailCallback);
|
||||
window.youTubeRecordingValidator = new JK.RecordingSourceValidator('rec_youtube', youTubeSuccessCallback, siteFailCallback);
|
||||
youTubeRecordingValidator.init();
|
||||
}, 1);
|
||||
|
||||
|
|
@ -190,7 +190,7 @@
|
|||
$inputDiv.append("<span class='error-text'>Invalid username</span>").show();
|
||||
}
|
||||
|
||||
function siteSuccessCallback($inputDiv) {
|
||||
function soundCloudSuccessCallback($inputDiv) {
|
||||
$inputDiv.removeClass('error');
|
||||
$inputDiv.find('.error-text').remove();
|
||||
|
||||
|
|
@ -200,7 +200,24 @@
|
|||
var $sampleList = $soundCloudSampleList.find('.sample-list');
|
||||
var addedRecording = recordingSources[recordingSources.length-1];
|
||||
$sampleList.append('<div class="recording-row left" data-recording-id="' + addedRecording.recording_id + '">');
|
||||
$sampleList.append(addedRecording.url);
|
||||
$sampleList.append(addedRecording.recording_title);
|
||||
$sampleList.append('</div>');
|
||||
}
|
||||
|
||||
$inputDiv.find('input').val('');
|
||||
}
|
||||
|
||||
function youTubeSuccessCallback($inputDiv) {
|
||||
$inputDiv.removeClass('error');
|
||||
$inputDiv.find('.error-text').remove();
|
||||
|
||||
var recordingSources = window.youTubeRecordingValidator.recordingSources();
|
||||
if (recordingSources && recordingSources.length > 0) {
|
||||
console.log('recordingSources=%o', recordingSources);
|
||||
var $sampleList = $youTubeSampleList.find('.sample-list');
|
||||
var addedRecording = recordingSources[recordingSources.length-1];
|
||||
$sampleList.append('<div class="recording-row left" data-recording-id="' + addedRecording.recording_id + '">');
|
||||
$sampleList.append(addedRecording.recording_title);
|
||||
$sampleList.append('</div>');
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,14 +12,26 @@ class Utils
|
|||
RECORDING_SOURCES.include?(site)
|
||||
end
|
||||
|
||||
def self.extract_recording_id(site, recording_url)
|
||||
def self.extract_recording_data(site, recording_url)
|
||||
recording_url.strip!
|
||||
rec_data = {}
|
||||
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)\/))([^\?&\"\'>]+)/
|
||||
return $5
|
||||
rec_data["id"] = $5
|
||||
end
|
||||
|
||||
uri = URI.parse("https://gdata.youtube.com/feeds/api/videos/#{$5}?v=2&alt=json")
|
||||
|
||||
http = Net::HTTP.new(uri.host, uri.port)
|
||||
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?
|
||||
|
||||
when 'rec_soundcloud'
|
||||
if recording_url =~ /^https?:\/\/.*soundcloud.com\/.+/
|
||||
tmpfile = Tempfile.new(site)
|
||||
|
|
@ -29,8 +41,13 @@ class Utils
|
|||
result = File.read(tmpfile.path)
|
||||
File.delete(tmpfile.path)
|
||||
if result =~ /"soundcloud:\/\/sounds:(\d+)"/
|
||||
return $1
|
||||
rec_data["id"] = $1
|
||||
end
|
||||
|
||||
if result =~ /property=\"og:title\" content=\"([\w\W]+)\"><meta property=\"og:image\"/
|
||||
rec_data["title"] = $1
|
||||
end
|
||||
return rec_data unless rec_data.empty?
|
||||
end
|
||||
end
|
||||
nil
|
||||
|
|
|
|||
Loading…
Reference in New Issue