140 lines
3.9 KiB
CoffeeScript
140 lines
3.9 KiB
CoffeeScript
class window.Vkontakte
|
|
|
|
qr: null
|
|
api_id: null
|
|
|
|
constructor: (@api_id) ->
|
|
|
|
getApiId: ->
|
|
@api_id
|
|
|
|
init: ->
|
|
@qr = {}
|
|
|
|
window.vkAsyncInit = ->
|
|
VK.init apiId: _vkontakte.getApiId()
|
|
VK.Auth.getLoginStatus (response) ->
|
|
_vkontakte.authInfo(response)
|
|
|
|
|
|
setTimeout ->
|
|
$('#vk_api_transport').append('<script async="async" type="text/javascript" src="http://vkontakte.ru/js/api/openapi.js"></script>')
|
|
, 0
|
|
|
|
authInfo: (response) ->
|
|
if typeof response isnt 'undefined' and response.session
|
|
_session = new Session(response.session)
|
|
_session.query '/user/auth', {}, (ar) ->
|
|
if ar.ok_reload
|
|
window.location.reload()
|
|
false
|
|
if ar.newbie
|
|
VK.Api.call 'getVariable', key: 1281, (r) ->
|
|
_session.query '/user/update', name: r.response, (ar2) ->
|
|
_session.setUser ar2.user
|
|
$('#username')
|
|
.html (if _session.getUser().name then _session.getUser().name else '%username%')
|
|
window._session = _session
|
|
_ajax.detectPage()
|
|
_session.displayAuthorizedContent()
|
|
$('#authorized').css display: 'block'
|
|
else
|
|
_session.setUser ar.user
|
|
|
|
$('#username')
|
|
.html (if _session.getUser().name then _session.getUser().name else '%username%')
|
|
_session.displayAuthorizedContent()
|
|
window._session = _session
|
|
_ajax.detectPage()
|
|
$('.fullscreen').hide();
|
|
|
|
if response.session.expire?
|
|
setTimeout ->
|
|
_vkontakte.auth()
|
|
false
|
|
, response.session.expire * 1000 - new Date().getTime() + 1000
|
|
else
|
|
_session = new Session({})
|
|
_session.setUser {}
|
|
_session.hideAuthorizedContent()
|
|
window._session = _session
|
|
_ajax.detectPage()
|
|
$('.fullscreen').hide();
|
|
|
|
auth: ->
|
|
VK.Auth.getLoginStatus (response) ->
|
|
_vkontakte.authInfo(response)
|
|
false
|
|
, 8
|
|
false
|
|
|
|
loadTracksData: (artist, track, duration, callback) ->
|
|
track_prepared = track.replace(/\(.*\)/i, '').split('/')[0];
|
|
query = artist+' '+track_prepared;
|
|
if url = _vkontakte.getQR query
|
|
callback url
|
|
else
|
|
VK.Api.call 'audio.search', q: query, (r) ->
|
|
url = _vkontakte.matchPerfectResult r.response, artist, track, duration
|
|
_vkontakte.addQR query, url
|
|
callback url
|
|
|
|
matchPerfectResult: (data, artist, track, duration) ->
|
|
duration = duration.split ':'
|
|
duration = parseInt(duration[0], 10) * 60 + parseInt(duration[1], 10)
|
|
best_score = 0;
|
|
best_result = null;
|
|
for item in data
|
|
if typeof item is '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 is 2
|
|
score += 5
|
|
else if item.title.split(artist).length is 2
|
|
score += 4
|
|
|
|
if item.title == track
|
|
score += 10
|
|
else if item.title.split(track).length is 2
|
|
score += 5
|
|
|
|
if parseInt(item.duration, 10) == duration
|
|
score += 15
|
|
else
|
|
delta = Math.abs parseInt(item.duration, 10) - duration
|
|
score += (10 - delta) if delta < 10
|
|
|
|
if score > best_score
|
|
best_score = score
|
|
best_result = item
|
|
|
|
if score is 35
|
|
return best_result.url
|
|
|
|
return best_result.url
|
|
|
|
addQR: (query, url) ->
|
|
_vkontakte.qr[query] = url;
|
|
|
|
getQR: (query) ->
|
|
if _vkontakte.qr[query]?
|
|
_vkontakte.qr[query]
|
|
false
|
|
|
|
$('#login').live 'click', ->
|
|
VK.Auth.login (response) ->
|
|
_vkontakte.authInfo(response)
|
|
false
|
|
, 8
|
|
false
|
|
$('#logout').live 'click', ->
|
|
_ajax.go '/search/';
|
|
VK.Auth.logout (response) ->
|
|
_vkontakte.authInfo(response)
|
|
false
|
|
false
|