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

170 lines
5.4 KiB
JavaScript

var Vkontakte;
Vkontakte = (function() {
Vkontakte.prototype.qr = null;
Vkontakte.prototype.api_id = null;
function Vkontakte(api_id) {
this.api_id = api_id;
}
Vkontakte.prototype.getApiId = function() {
return this.api_id;
};
Vkontakte.prototype.init = function() {
this.qr = [];
window.vkAsyncInit = function() {
VK.init({
apiId: _vkontakte.getApiId()
});
return VK.Auth.getLoginStatus(function(response) {
return _vkontakte.authInfo(response);
});
};
return setTimeout(function() {
return $('#vk_api_transport').append('<script async="async" type="text/javascript" src="http://vkontakte.ru/js/api/openapi.js"></script>');
}, 0);
};
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);
$('.header-container .hello .greating').html(_beathaven.ls('HELLO') + ', <span class="settings">' + (_session.getUser().name ? _session.getUser().name : '%username%') + '</span>!');
window._session = _session;
_ajax.detectPage();
return $('.fullscreen').hide();
});
});
} else {
_session.setUser(ar.user);
}
$('.header-container .hello').show();
$('.header-container .hello .greating').html(_beathaven.ls('HELLO') + ', <span class="settings">' + (_session.getUser().name ? _session.getUser().name : '%username%') + '</span>!');
window._session = _session;
_ajax.detectPage();
return $('.fullscreen').hide();
});
if (response.session.expire != null) {
return setTimeout(function() {
_vkontakte.auth();
return false;
}, response.session.expire * 1000 - new Date().getTime() + 1000);
}
} else {
_session = new Session({});
_session.setUser({});
$('#vk_login').css({
display: 'block'
});
$('.auth-notice').css('left', $('#vk_login').offset().left).show();
$('.header-container .hello').hide();
window._session = _session;
_ajax.detectPage();
return $('.fullscreen').hide();
}
};
Vkontakte.prototype.auth = function() {
VK.Auth.getLoginStatus(function(response) {
_vkontakte.authInfo(response);
return false;
}, 8);
return false;
};
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) {
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];
if (typeof item === 'object') {
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;
})();
$('#vk_login, .auth-notice').live('click', function() {
VK.Auth.login(function(response) {
_vkontakte.authInfo(response);
return false;
}, 8);
return false;
});
$('#vk_logout').live('click', function() {
_ajax.go('/search/');
VK.Auth.logout(function(response) {
_vkontakte.authInfo(response);
return false;
});
return false;
});