From 4a2f793635a9bf1e148242715c1fffcd0535e545 Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Wed, 29 Jun 2011 17:54:55 +0400 Subject: [PATCH] Vkontakte session battle --- app/coffeescripts/session.coffee | 8 +-- app/coffeescripts/vkontakte.coffee | 74 ++++++++++++---------- app/views/layouts/application.html.erb | 14 ++--- public/javascripts/coffee/session.js | 11 ++-- public/javascripts/coffee/vkontakte.js | 86 +++++++++++++++----------- public/stylesheets/_sass/player.scss | 32 +++++----- public/stylesheets/player.css | 22 +++---- 7 files changed, 137 insertions(+), 110 deletions(-) diff --git a/app/coffeescripts/session.coffee b/app/coffeescripts/session.coffee index 3b59cd6..9f155f6 100644 --- a/app/coffeescripts/session.coffee +++ b/app/coffeescripts/session.coffee @@ -19,12 +19,12 @@ class Session @user query: (url, params, callback) -> - q_params = @vk_params - $.each params, (key, val) -> - q_params[key] = val + q_params = $.extend {}, @vk_params, params $.post url, q_params, callback false reloadSession: -> _session.query '/user/auth', {}, (ar) -> - _session.setUser ar.user \ No newline at end of file + _session.setUser ar.user + false + false diff --git a/app/coffeescripts/vkontakte.coffee b/app/coffeescripts/vkontakte.coffee index 7dffcf7..8555130 100644 --- a/app/coffeescripts/vkontakte.coffee +++ b/app/coffeescripts/vkontakte.coffee @@ -13,7 +13,8 @@ class Vkontakte window.vkAsyncInit = -> VK.init apiId: _vkontakte.getApiId() - VK.Auth.getLoginStatus _vkontakte.authInfo + VK.Auth.getLoginStatus (response) -> + _vkontakte.authInfo(response) setTimeout -> @@ -42,6 +43,12 @@ class Vkontakte $('.header-container .hello').show() $('.header-container .hello .greating') .html 'Tēnā koe, ' +(if _session.getUser().name then _session.getUser().name else '%username%')+ '!' + + if response.session.expire? + setTimeout -> + _vkontakte.auth() + false + , response.session.expire * 1000 - new Date().getTime() + 1000 else _session = new Session({}) _session.setUser {} @@ -49,6 +56,13 @@ class Vkontakte $('.header-container .hello').hide() window._session = _session + auth: -> + VK.Auth.login (response) -> + _vkontakte.authInfo(response) + false + , 8 + false + loadTracksData: (artist, track, duration, callback) -> track_prepared = track.replace(/\(.*\)/i, '').split('/')[0]; query = artist+' '+track_prepared; @@ -56,46 +70,45 @@ class Vkontakte callback url else VK.Api.call 'audio.search', q: query, (r) -> - r.response.splice 0, 1 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 - score = 0; - item.artist = item.artist.trim(); - item.title = item.title.trim(); + 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.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 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 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 > best_score + best_score = score + best_result = item - if score is 35 - return best_result.url + if score is 35 + return best_result.url return best_result.url @@ -108,12 +121,11 @@ class Vkontakte false $('#vk_login').live 'click', -> - VK.Auth.login (response) -> - _vkontakte.authInfo(response) - false - , 8 + _vkontakte.auth() false $('#vk_logout').live 'click', -> _ajax.go '/search/'; - VK.Auth.logout _vkontakte.authInfo + VK.Auth.logout (response) -> + _vkontakte.authInfo(response) + false false diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 1589aec..b7f5255 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -5,12 +5,12 @@ - <%= javascript_include_tag "jquery/jquery.min.js" %> - <%= javascript_include_tag "jquery/jquery-ui/js/jquery-ui-1.8.13.custom.min.js" %> - <%= javascript_include_tag "jquery/jquery.autocomplete.js" %> - <%= javascript_include_tag "jquery/jquery.contentchange.js" %> - <%= javascript_include_tag "jquery/jquery.jplayer.js" %> - <%= javascript_include_tag "jquery/jquery.scroll.js" %> + <%= javascript_include_tag "jquery/jquery.min.js", :type => "text/javascript", :charset => "utf-8" %> + <%= javascript_include_tag "jquery/jquery-ui/js/jquery-ui-1.8.13.custom.min.js", :type => "text/javascript", :charset => "utf-8" %> + <%= javascript_include_tag "jquery/jquery.autocomplete.js", :type => "text/javascript", :charset => "utf-8" %> + <%= javascript_include_tag "jquery/jquery.contentchange.js", :type => "text/javascript", :charset => "utf-8" %> + <%= javascript_include_tag "jquery/jquery.jplayer.js", :type => "text/javascript", :charset => "utf-8" %> + <%= javascript_include_tag "jquery/jquery.scroll.js", :type => "text/javascript", :charset => "utf-8" %> <%= javascript_include_tag "coffee/beathaven.js", :type => "text/javascript", :charset => "utf-8" %> <%= javascript_include_tag "coffee/vkontakte.js", :type => "text/javascript", :charset => "utf-8" %> <%= javascript_include_tag "coffee/session.js", :type => "text/javascript", :charset => "utf-8" %> @@ -61,7 +61,7 @@