Vkontakte api work optimized
This commit is contained in:
parent
295811c6de
commit
c95fb3514a
|
@ -63,4 +63,18 @@ String.prototype.htmlsafe = function() {
|
|||
str = str.replace(replaces[i][0], replaces[i][1]);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
String.prototype.trim = function() {
|
||||
var str = this;
|
||||
while (str.indexOf(' ') !== -1) {
|
||||
str = str.replace(' ', ' ');
|
||||
}
|
||||
if (str.charAt(0) == ' ') {
|
||||
str = str.substring(1);
|
||||
}
|
||||
if (str.charAt(str.length - 1) == ' ') {
|
||||
str = str.substring(0, str.length - 1);
|
||||
}
|
||||
return str;
|
||||
}
|
|
@ -54,7 +54,7 @@ var Player = {
|
|||
}
|
||||
var initial_count = $('.playlist-tracks li').length;
|
||||
$('.playlist-tracks').append('\
|
||||
<li id="i'+ Math.round(Math.random() * 999999) +'" data-artist="'+ artist +'" data-album="'+ album +'" data-track="'+ track +'" data-length="'+ length +'">\
|
||||
<li id="i'+ Math.round(Math.random() * 999999) +'" data-artist="'+ artist.trim() +'" data-album="'+ album.trim() +'" data-track="'+ track.trim() +'" data-length="'+ length +'">\
|
||||
<div class="item">\
|
||||
<div class="fade"></div>\
|
||||
<span class="title" title="'+ artist.htmlsafe() +' — '+ track.htmlsafe() +'">'+ artist +' — '+ track +'</span>\
|
||||
|
@ -83,7 +83,7 @@ var Player = {
|
|||
$obj.addClass('now');
|
||||
$('#player .loaded, #player .played').width(0);
|
||||
|
||||
loadTracksData($obj.attr('data-artist'), $obj.attr('data-track'), $obj.attr('data-length'), Player.playSource);
|
||||
Vkontakte.loadTracksData($obj.attr('data-artist'), $obj.attr('data-track'), $obj.attr('data-length'), Player.playSource);
|
||||
Player.updateNowListening($obj.attr('data-artist'), $obj.attr('data-album'), $obj.attr('data-track'));
|
||||
},
|
||||
|
||||
|
|
|
@ -1,87 +1,102 @@
|
|||
function authInfo(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;
|
||||
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();
|
||||
}
|
||||
$('#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();
|
||||
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(authInfo);
|
||||
VK.Auth.getLoginStatus(Vkontakte.authInfo);
|
||||
$('#vk_login').click(function(){
|
||||
VK.Auth.login(authInfo, 8);
|
||||
VK.Auth.login(Vkontakte.authInfo, 8);
|
||||
});
|
||||
$('#vk_logout').click(function(){
|
||||
VK.Auth.logout(authInfo);
|
||||
VK.Auth.logout(Vkontakte.authInfo);
|
||||
});
|
||||
})
|
||||
|
||||
function loadTracksData(artist, track, duration, callback) {
|
||||
track_prepared = track.replace(/\(.*\)/i, '').split('/')[0];
|
||||
VK.Api.call('audio.search', {q: artist +' '+ track_prepared}, 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;
|
||||
}
|
||||
})
|
Loading…
Reference in New Issue