1
0
Fork 0
oldhaven/public/javascripts/coffee/vkontakte.js

127 lines
3.8 KiB
JavaScript

var Vkontakte;
Vkontakte = (function() {
Vkontakte.prototype.qr = null;
function Vkontakte(api_id) {
this.api_id = api_id;
}
Vkontakte.prototype.init = function() {
this.qr = [];
VK.init({
apiId: this.api_id
});
return VK.Auth.getLoginStatus(this.authInfo);
};
Vkontakte.prototype.authInfo = function(response) {
var _session;
if (typeof response !== 'undefined' && response.session) {
_session = new Session(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) {
return _session.query('/user/update', {
name: r.response
}, function(ar2) {
_session.setUser(ar2.user);
return $('.header-container .hello .greating').text('Hi there, ' + (_session.getUser().name ? _session.getUser().name : '%username%') + '!');
});
});
} else {
_session.setUser(ar.user);
}
return $('.header-container .hello .greating').text('Hi there, ' + (_session.getUser().name ? _session.getUser().name : '%username%') + '!');
});
} else {
_session = new Session({});
$('#vk_login, .auth_notice').css({
display: 'block'
});
$('#vk_logout').hide();
}
return window._session = _session;
};
Vkontakte.prototype.loadTracksData = function(artist, track, duration, callback) {
var query, track_prepared, url;
track_prepared = track.replace(/\(.*\)/i, '').split('/')[0];
query = artist + ' ' + track_prepared;
if (url = _vkontakte.getQR(query)) {
return callback(url);
} else {
return VK.Api.call('audio.search', {
q: query
}, function(r) {
r.response.splice(0, 1);
url = _vkontakte.matchPerfectResult(r.response, artist, track, duration);
_vkontakte.addQR(query, url);
return callback(url);
});
}
};
Vkontakte.prototype.matchPerfectResult = function(data, artist, track, duration) {
var best_result, best_score, delta, item, score, _i, _len;
duration = duration.split(':');
duration = parseInt(duration[0], 10) * 60 + parseInt(duration[1], 10);
best_score = 0;
best_result = null;
for (_i = 0, _len = data.length; _i < _len; _i++) {
item = data[_i];
score = 0;
item.artist = item.artist.trim();
item.title = item.title.trim();
if (item.artist === artist) {
score += 10;
} else if (item.artist.split(artist).length === 2) {
score += 5;
} else if (item.title.split(artist).length === 2) {
score += 4;
}
if (item.title === track) {
score += 10;
} else if (item.title.split(track).length === 2) {
score += 5;
}
if (parseInt(item.duration, 10) === duration) {
score += 15;
} else {
delta = Math.abs(parseInt(item.duration, 10) - duration);
if (delta < 10) {
score += 10 - delta;
}
}
if (score > best_score) {
best_score = score;
best_result = item;
}
if (score === 35) {
return best_result.url;
}
}
return best_result.url;
};
Vkontakte.prototype.addQR = function(query, url) {
return this.qr[query] = url;
};
Vkontakte.prototype.getQR = function(query) {
if (this.qr[query] != null) {
this.qr[query];
}
return false;
};
return Vkontakte;
})();
$(function() {
$('#vk_login').click(function() {
return VK.Auth.login(_vkontakte.authInfo(), 8);
});
return $('#vk_logout').click(function() {
return VK.Auth.logout(_vkontakte.authInfo());
});
});