Audio response perfect match, auth improvements

This commit is contained in:
magnolia-fan
2011-06-16 06:33:08 +04:00
parent 5bd2852894
commit a024f13088
7 changed files with 111 additions and 17 deletions
-1
View File
@@ -2,7 +2,6 @@ var Ajax = {
loadArtistData: function(name) {
$('#search-container input').attr('disabled', 'disabled').blur();
$('#search-container img').show();
console.log(1);
name = name.replace(' ', '+');
Ajax.setArchor('/artist/'+ name +'/');
$.get('/artist/'+ name +'/', function(data){
+6
View File
@@ -60,6 +60,12 @@ var Audio = {
return false;
},
skipToPercent: function(val) {
try {
this.audio.currentTime = this.audio.duration * val;
} catch(e) {}
},
startListener: function() {
Audio.tid = window.setTimeout(Audio.startListener, 100);
Player.updateUI(
+8 -3
View File
@@ -26,14 +26,15 @@ var Player = {
} else {
var id = $(obj).attr('id').split('i')[1];
}
var query = $(obj).find('.title').html();
var length = $(obj).find('.duration').html();
var query = $(obj).find('.title').text();
var length = $(obj).find('.duration').text();
var tmp = query.split(' — ');
$('#player .now-playing').html(query);
$('.playlist-tracks li').removeClass('now');
$('#i'+ id).addClass('now');
loadTracksData(query, this.playSource);
loadTracksData(tmp[0], tmp[1], length, this.playSource);
},
playSource: function(url) {
@@ -109,4 +110,8 @@ $('.tracklist li').live('mouseover mouseout', function(e){
$('.playlist-tracks li').live('dblclick', function(){
Player.setTrack(this);
});
$('#player .progress').live('click', function(e){
Audio.skipToPercent(e.offsetX / Player.bar_width);
});
+53 -6
View File
@@ -1,9 +1,15 @@
function authInfo(response) {
if (typeof response != 'undefined' && response.session) {
$('#vk_login').hide();
$('#vk_logout').show();
$('#vk_login, .auth_notice').hide();
$('#vk_logout').css('display', 'block');
if ($('#search_field').length > 0) {
$('#search_field').focus();
}
VK.Api.call('getVariable', {key: 1281}, function(r) {
$('#header-container .hello').text('Hi there, '+ r.response +'!');
});
} else {
$('#vk_login').show();
$('#vk_login, .auth_notice').css('display', 'block');
$('#vk_logout').hide();
}
}
@@ -21,8 +27,49 @@ $(function(){
});
})
function loadTracksData(query, callback) {
VK.Api.call('audio.search', {q:query}, function(r){
callback(r.response[1].url);
function loadTracksData(artist, track, duration, callback) {
VK.Api.call('audio.search', {q: artist +' '+ track}, function(r){
callback(matchPerfectResult(r.response, artist, track, duration));
})
}
function matchPerfectResult(data, artist, track, duration) {
var duration = duration.split(':');
if (duration[1].charAt(0) === '0') {
duration[1] = duration[1].substring(1);
}
duration = parseInt(duration[0]) * 60 + parseInt(duration[1]);
var best_score = 0;
var best_result = null;
for (var i = 1; i < data.length; i++) {
var score = 0;
if (data[i].artist === artist) {
score += 10;
} else if (data[i].artist.split(artist).length === 2) {
score += 5;
} else if (data[i].title.split(artist).length === 2) {
score += 4;
}
if (data[i].title === track) {
score += 10;
} else if (data[i].title.split(track).length === 2) {
score += 5;
}
if (parseInt(data[i].duration) === duration) {
score += 15;
} else {
var delta = Math.abs(parseInt(data[i].duration) - duration);
if (delta < 10) {
score += (10 - delta);
}
}
if (score > best_score) {
best_score = score;
best_result = data[i];
}
if (score === 35) {
return best_result.url;
}
}
return best_result.url;
}