1
0
Fork 0
oldhaven/public/js/beathaven/vkontakte.js

102 lines
2.9 KiB
JavaScript

var Vkontakte = {
qr: [],
authInfo: function(response) {
if (typeof response != 'undefined' && response.session) {
Session.setVkParams(response.session);
$('#vk_login, .auth_notice').hide();
$('#vk_logout').css('display', 'block');
if ($('#search_field').length > 0) {
$('#search_field').focus();
}
Session.query('/user/auth', {}, function(ar){
if (ar.newbie) {
VK.Api.call('getVariable', {key: 1281}, function(r) {
Session.query('/user/update', {'name': r.response}, function(ar2) {
Settings.user = ar2.user;
$('#header-container .hello .greating').text('Hi there, '+ (Settings.user.name ? Settings.user.name : '%username%') +'!');
});
});
} else {
Settings.user = ar.user;
}
$('#header-container .hello .greating').text('Hi there, '+ (Settings.user.name ? Settings.user.name : '%username%') +'!');
});
} else {
$('#vk_login, .auth_notice').css('display', 'block');
$('#vk_logout').hide();
}
},
loadTracksData: function(artist, track, duration, callback) {
var track_prepared = track.replace(/\(.*\)/i, '').split('/')[0];
var query = artist +' '+ track_prepared;
if (typeof Vkontakte.qr[query] !== 'undefined') {
callback(Vkontakte.qr[query]);
} else {
VK.Api.call('audio.search', {q: query}, function(r){
var url = Vkontakte.matchPerfectResult(r.response, artist, track, duration);
Vkontakte.qr[query] = url;
callback(url);
});
}
},
matchPerfectResult: function(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;
data[i].artist = data[i].artist.trim();
data[i].title = data[i].title.trim();
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;
}
}
$(function(){
VK.init({
apiId: (document.location.host == 'beathaven.org' ? 2335068 : 2383163),
nameTransportPath: "/xd_receiver.html"
});
VK.Auth.getLoginStatus(Vkontakte.authInfo);
$('#vk_login').click(function(){
VK.Auth.login(Vkontakte.authInfo, 8);
});
$('#vk_logout').click(function(){
VK.Auth.logout(Vkontakte.authInfo);
});
})