From 0ec0b141724ae765e09e777caee249fa67288a2f Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 12 Oct 2011 13:24:07 +0400 Subject: [PATCH] Update track length in db if empty. Closes #107 --- Gemfile | 1 - Gemfile.lock | 12 ++-- app/assets/javascripts/player.coffee | 12 +++- app/assets/javascripts/vksearch/LICENCE | 20 ------ app/assets/javascripts/vksearch/README.md | 28 -------- .../vksearch/lib/vkontakte_music.min.js | 9 --- .../vksearch/src/vkontakte_music.coffee | 70 ------------------- .../vksearch/vkontakte_search.coffee | 21 +++--- app/controllers/track_controller.rb | 18 +++++ config/routes.rb | 2 + nginx/nginx.conf | 47 ------------- nginx/proxy.conf | 43 ------------ 12 files changed, 47 insertions(+), 236 deletions(-) delete mode 100644 app/assets/javascripts/vksearch/LICENCE delete mode 100644 app/assets/javascripts/vksearch/README.md delete mode 100644 app/assets/javascripts/vksearch/lib/vkontakte_music.min.js delete mode 100644 app/assets/javascripts/vksearch/src/vkontakte_music.coffee create mode 100644 app/controllers/track_controller.rb delete mode 100644 nginx/nginx.conf delete mode 100755 nginx/proxy.conf diff --git a/Gemfile b/Gemfile index 3b130c4..10e83c7 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,5 @@ source 'http://rubygems.org' -gem 'rack', '1.3.3' gem 'rails', '3.1.0' gem 'unicorn' diff --git a/Gemfile.lock b/Gemfile.lock index e54ae1a..51a4d38 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -66,13 +66,13 @@ GEM treetop (~> 1.4.8) mime-types (1.16) multi_json (1.0.3) - musicbrainz (0.4.10) + musicbrainz (0.5.0) nokogiri mysql2 (0.3.7) nokogiri (1.5.0) pg (0.11.0) polyglot (0.3.2) - rack (1.3.3) + rack (1.3.4) rack-cache (1.0.3) rack (>= 0.4) rack-mount (0.8.3) @@ -98,13 +98,14 @@ GEM thor (~> 0.14.6) raindrops (0.7.0) rake (0.9.2) - rdoc (3.9.4) - sass (3.1.8) + rdoc (3.10) + json (~> 1.4) + sass (3.1.10) sprockets (2.0.2) hike (~> 1.2) rack (~> 1.0) tilt (~> 1.1, != 1.3.0) - therubyracer (0.9.5) + therubyracer (0.9.8) libv8 (~> 3.3.10) thor (0.14.6) tilt (1.3.3) @@ -135,7 +136,6 @@ DEPENDENCIES mysql2 (= 0.3.7) nokogiri pg - rack (= 1.3.3) rails (= 3.1.0) sass therubyracer diff --git a/app/assets/javascripts/player.coffee b/app/assets/javascripts/player.coffee index dc49fa9..5e2bc8c 100644 --- a/app/assets/javascripts/player.coffee +++ b/app/assets/javascripts/player.coffee @@ -81,8 +81,16 @@ class window.Player $('.player .now-playing').html query $('.playlist li').removeClass 'now' $obj.addClass 'now' - _vk_music.search track.artist, track.name, track.duration, (url) -> - _player.playSource url + _vk_music.search track.artist, track.name, track.duration, (audio) -> + _player.playSource audio.url + if track.length == 0 + len = parseInt(audio.duration, 10) + duration = Math.floor(len / 60)+':'+(len - Math.floor(len / 60) * 60) + _session.query '/track/update_length', { track_id: track.id, length: len }, (r) -> + if r.result is 'success' + $('.playlist li[data-id="'+track.id+'"] .song-duration').text(duration) + $($('.album div[data-id="'+track.id+'"]').siblings()[0]).text(duration) + this.updateNowListening track false diff --git a/app/assets/javascripts/vksearch/LICENCE b/app/assets/javascripts/vksearch/LICENCE deleted file mode 100644 index 7e2519e..0000000 --- a/app/assets/javascripts/vksearch/LICENCE +++ /dev/null @@ -1,20 +0,0 @@ -Copyright (c) 2011 Gregory Eremin - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/app/assets/javascripts/vksearch/README.md b/app/assets/javascripts/vksearch/README.md deleted file mode 100644 index 45bd814..0000000 --- a/app/assets/javascripts/vksearch/README.md +++ /dev/null @@ -1,28 +0,0 @@ -# Vkontakte (VK.com) API music search tool - -Don't forget to log in to [vk.com API](http://vk.com/developers.php?oid=-17680044&p=Open_API) - -### CoffeeScript example -```coffeescript -vk_music = new VkontakteMusic -vk_music.search "Kasabian", "L.S.F. (Lost Souls Forever)", "2:17", (url) -> - audio = document.createElement "audio" - audio.setAttribute "src", url - document.getElementsByTagName("body")[0].appendChild audio - audio.play() - false -``` - -### JavaScript example -```javascript -var vk_music; -vk_music = new VkontakteMusic; -vk_music.search("Kasabian", "L.S.F. (Lost Souls Forever)", "2:17", function(url) { - var audio; - audio = document.createElement("audio"); - audio.setAttribute("src", url); - document.getElementsByTagName("body")[0].appendChild(audio); - audio.play(); - return false; -}); -``` \ No newline at end of file diff --git a/app/assets/javascripts/vksearch/lib/vkontakte_music.min.js b/app/assets/javascripts/vksearch/lib/vkontakte_music.min.js deleted file mode 100644 index f4d4440..0000000 --- a/app/assets/javascripts/vksearch/lib/vkontakte_music.min.js +++ /dev/null @@ -1,9 +0,0 @@ -/* - * Vkontakte (VK.com) API music search tool - * https://github.com/magnolia-fan/vkontakte_music_search - * - * Copyright 2011, Gregory Eremin - * Licensed under the MIT license. - * https://raw.github.com/magnolia-fan/vkontakte_music_search/master/LICENSE - */ -var VkontakteMusic;VkontakteMusic=function(){function a(){}return a.prototype.query_results={},a.prototype.search=function(a,b,c,d,e){var f,g;return e==null&&(e=!1),f=this.prepareQuery(a,b),this.query_results[f]!=null&&!e&&d(this.query_results[f]),g=this,VK.Api.call("audio.search",{q:f},function(h){var i,j;return i=g.range(h.response,a,b,c),j=null,i.length>0&&(j=i[0].url),g.query_results[f]=i,d(e?i:j)})},a.prototype.range=function(a,b,c,d){var e,f,g,h,i;typeof d=="string"&&(d=d.split(":"),d=parseInt(d[0],10)*60+parseInt(d[1],10));for(f=0,i=a.length;f0&&(g.artist===b?h+=10:g.artist.split(b).length===2?h+=5:g.title.split(b).length===2&&(h+=4)),g.artist.length>0&&(g.title===c?h+=10:g.title.split(c).length===2&&(h+=5)),parseInt(g.duration,10)===d?h+=15:(e=Math.abs(parseInt(g.duration,10)-d),e<10&&(h+=10-e)),a[f].score=h}return a.length>0&&typeof a[0]!="object"&&(a.splice(0,1),a.sort(function(a,b){return b.score-a.score})),a},a.prototype.prepareQuery=function(a,b){return a+" "+b.replace(/\(.*\)/i,"").split("/")[0]},a.prototype.trim=function(a){while(a.indexOf(" ")!==-1)a=a.replace(" "," ");return a.charAt(0)===" "&&(a=a.substring(1)),a.charAt(a.length-1)===" "&&(a=a.substring(0,a.length-1)),a},a}() \ No newline at end of file diff --git a/app/assets/javascripts/vksearch/src/vkontakte_music.coffee b/app/assets/javascripts/vksearch/src/vkontakte_music.coffee deleted file mode 100644 index 14e4ec0..0000000 --- a/app/assets/javascripts/vksearch/src/vkontakte_music.coffee +++ /dev/null @@ -1,70 +0,0 @@ -### - * Vkontakte (VK.com) API music search tool - * https://github.com/magnolia-fan/vkontakte_music_search - * - * Copyright 2011, Gregory Eremin - * Licensed under the MIT license. - * https://raw.github.com/magnolia-fan/vkontakte_music_search/master/LICENSE - ### -class window.VkontakteMusic - query_results: {} - - search: (artist, track, duration, callback, return_all = false) -> - query = this.prepareQuery artist, track - if @query_results[query]? and not return_all - callback @query_results[query] - that = this - VK.Api.call 'audio.search', q: query, (r) -> - results = that.range r.response, artist, track, duration - top_result = null - if results.length > 0 - top_result = results[0].url - that.query_results[query] = results - callback if return_all then results else top_result - - range: (data, artist, track, duration) -> - if typeof duration is 'string' - duration = duration.split ':' - duration = parseInt(duration[0], 10) * 60 + parseInt(duration[1], 10) - for item, i in data - if typeof item isnt 'object' - continue - item.score = 0; - item.artist = this.trim(item.artist); - item.title = this.trim(item.title); - score = 0 - if item.artist.length > 0 - 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.length > 0 - 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 - data[i].score = score - if data.length > 0 and typeof data[0] isnt 'object' - data.splice 0, 1 - data.sort (a, b) -> - b.score - a.score - data - - prepareQuery: (artist, track) -> - artist+" "+track.replace(/\(.*\)/i, '').split('/')[0] - - trim: (str) -> - while str.indexOf(' ') isnt -1 - str = str.replace ' ', ' ' - if str.charAt(0) is ' ' - str = str.substring 1 - if str.charAt(str.length - 1) is ' ' - str = str.substring 0, str.length - 1 - str diff --git a/app/assets/javascripts/vksearch/vkontakte_search.coffee b/app/assets/javascripts/vksearch/vkontakte_search.coffee index 14e4ec0..e2a0d87 100644 --- a/app/assets/javascripts/vksearch/vkontakte_search.coffee +++ b/app/assets/javascripts/vksearch/vkontakte_search.coffee @@ -18,7 +18,7 @@ class window.VkontakteMusic results = that.range r.response, artist, track, duration top_result = null if results.length > 0 - top_result = results[0].url + top_result = results[0] that.query_results[query] = results callback if return_all then results else top_result @@ -29,9 +29,9 @@ class window.VkontakteMusic for item, i in data if typeof item isnt 'object' continue - item.score = 0; - item.artist = this.trim(item.artist); - item.title = this.trim(item.title); + item.score = 0 + item.artist = this.trim(item.artist) + item.title = this.trim(item.title) score = 0 if item.artist.length > 0 if item.artist == artist @@ -45,14 +45,15 @@ class window.VkontakteMusic score += 10 else if item.title.split(track).length is 2 score += 5 - if parseInt(item.duration, 10) == duration + if duration != 0 and parseInt(item.duration, 10) == duration score += 15 else delta = Math.abs parseInt(item.duration, 10) - duration score += (10 - delta) if delta < 10 data[i].score = score - if data.length > 0 and typeof data[0] isnt 'object' - data.splice 0, 1 + if data.length > 0 + if typeof data[0] isnt 'object' + data.splice(0, 1) data.sort (a, b) -> b.score - a.score data @@ -62,9 +63,9 @@ class window.VkontakteMusic trim: (str) -> while str.indexOf(' ') isnt -1 - str = str.replace ' ', ' ' + str = str.replace(' ', ' ') if str.charAt(0) is ' ' - str = str.substring 1 + str = str.substring(1) if str.charAt(str.length - 1) is ' ' - str = str.substring 0, str.length - 1 + str = str.substring(0, str.length - 1) str diff --git a/app/controllers/track_controller.rb b/app/controllers/track_controller.rb new file mode 100644 index 0000000..98601f6 --- /dev/null +++ b/app/controllers/track_controller.rb @@ -0,0 +1,18 @@ +class TrackController < ApplicationController + before_filter :authorize + + def update_length + unless params[:track_id].nil? + track = Track.find(params[:track_id]) + unless track.nil? or params[:length].nil? + if track.length == nil or track.length.to_i == 0 and params[:length].to_i > 0 + track.length = params[:length].to_i + track.save + render :json => { :result => :success } + return + end + end + end + render :json => { :result => :failed } + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index ba3142f..2af6b7c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -68,6 +68,8 @@ Beathaven::Application.routes.draw do match 'lastfm/listening' => 'last_fm#listening' match 'lastfm/scrobble' => 'last_fm#scrobble' + match 'track/update_length' => 'track#update_length' + match 'settings' => 'user#settings' match 'artist/autocomplete' => 'artist#autocomplete' diff --git a/nginx/nginx.conf b/nginx/nginx.conf deleted file mode 100644 index 26fc009..0000000 --- a/nginx/nginx.conf +++ /dev/null @@ -1,47 +0,0 @@ -worker_processes 1; -error_log logs/error.log debug; - -events { - worker_connections 256; -} - -http { - default_type application/octet-stream; - sendfile on; - #tcp_nopush on; - keepalive_timeout 65; - tcp_nodelay on; - - gzip on; - gzip_min_length 1100; - gzip_buffers 4 8k; - gzip_types text/plain; - - upstream mongrel { - server 127.0.0.1:3000; - } - - server { - listen 80; - server_name bh.vim.me localhost susanna brooklyn; - #root /Users/chez/Sites; - #index index.html index.htm; - - try_files $uri @mongrel; - - location @mongrel { - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header Host $http_host; - proxy_redirect off; - proxy_pass http://mongrel; - } - - include proxy.conf; - - #error_page 500 502 503 504 /50x.html; - location = /50x.html { - root html; - } - } -} diff --git a/nginx/proxy.conf b/nginx/proxy.conf deleted file mode 100755 index 4d8dedf..0000000 --- a/nginx/proxy.conf +++ /dev/null @@ -1,43 +0,0 @@ -# Proxy download -location ~* ^/proxy-stream/(.*?)/(.*?)/(.*?)/(.*?)/(.*) { - # Do not allow people to mess with this location directly - # Only internal redirects are allowed - internal; - - # Location-specific logging - #access_log logs/proxy_stream.access.log debug; - #error_log logs/proxy_stream.error.log debug; - - # Extract download url from the request - set $token $1; - set $remixsid $2; - set $remixchk $3; - set $download_host $4; - set $download_uri $5; - - # Compose download url - set $download_url http://$download_host/$download_uri; - - # Compose cookie string - set $cookie "remixsid=$remixsid;remixchk=$remixchk"; - - # Set download request headers - proxy_set_header Host $download_host; - proxy_set_header Cookie $cookie; - proxy_set_header Referer "http://vkontakte.ru/audio?album_id=0"; - - # The next two lines could be used if your storage - # backend does not support Content-Disposition - # headers used to specify file name browsers use - # when save content to the disk - proxy_ignore_headers Cache-Control Expires; - - - # Do not touch local disks when proxying - # content to clients - proxy_max_temp_file_size 0; - - # Download the file and send it to client - resolver 192.168.0.1; - proxy_pass $download_url; -}