diff --git a/app/controllers/import_controller.rb b/app/controllers/import_controller.rb index f723f55..72e6b5d 100644 --- a/app/controllers/import_controller.rb +++ b/app/controllers/import_controller.rb @@ -18,7 +18,7 @@ class ImportController < ApplicationController lastfm = Lastfm.new(@@lastfm_api_key, @@lastfm_secret) artist = Artist.find_by_name(name) - + begin # Get artist info @@ -38,130 +38,154 @@ class ImportController < ApplicationController artist.mbid = artist_mb_data[:mbid] # Get albums from MB - release_groups_mb = brainz.release_group(nil, :artist => artist_mb_data[:mbid])['release_group_list']['release_group'] - release_groups_mb.each do |mb_album| + release_groups_mb = brainz.release_group(nil, :artist => artist_mb_data[:mbid]) - # Get album releases from MB - releases_mb = brainz.release(nil, {'release-group' => mb_album['id']})['release_list']['release'] + unless release_groups_mb['release_group_list']['release_group'].nil? - unless releases_mb.nil? + release_groups_mb['release_group_list']['release_group'].each do |mb_album| - if releases_mb.is_a? Hash - releases_mb = [releases_mb] - end - - releases_mb.delete_if { |a| a['date'].nil? } - releases_mb.each do |release| - if release['date'].length == 4 - release['date'] += '-12-28' - elsif release['date'].length == 7 - release['date'] += '-28' - end - end + album_name = mb_album['title'] + (mb_album['disambiguation'].nil? ? '' : ' ('+ mb_album['disambiguation'] +')') + + # Checking if there is already an album with the same name + albums_db = Album.where(:artist_id => artist.id, :name => album_name) + + # Get album releases from MB + releases_mb = brainz.release(nil, {'release-group' => mb_album['id']}) - # Sorting releases by date - releases_mb.sort! { |a, b| a['date'].to_date <=> b['date'].to_date } - main_release = releases_mb.shift - - # Get tracks from the first release and then exclude release - tracks_mb = [] - tracks_mb_r = brainz.release(main_release['id'], {'inc' => 'recordings'})['release']['medium_list']['medium'] - if tracks_mb_r.is_a? Hash - if tracks_mb_r['track_list']['track'].is_a? Hash - tracks_mb_r['track_list']['track'] = [tracks_mb_r['track_list']['track']] - end - tracks_mb = tracks_mb_r['track_list']['track'] - elsif tracks_mb_r.is_a? Array - tracks_mb = tracks_mb_r.shift['track_list']['track'] - tracks_mb_r.each do |cd| - if cd['track_list']['track'].is_a? Hash - cd['track_list']['track'] = [cd['track_list']['track']] - end - cd['track_list']['track'].each { |mb_track| mb_track['bonus'] = 1 } - tracks_mb |= cd['track_list']['track'] - end - end - - unless tracks_mb.empty? - - # Saving album - begin - album_lastfm = lastfm.album.get_info(artist_lastfm['name'], mb_album['title']) - album_image = album_lastfm['image'][3]['content'] - rescue - album_image = '' - end - - album = Album.new - album.name = mb_album['title'] - album.year = main_release['date'].to_date.year - album.artist_id = artist.id - album.mbid = mb_album['id'] - album.album_type = mb_album['type'] - album.pic_url = album_image - album.save - - # Creating hashed array for matching - mb_main_tracks_hashed = [] - tracks_mb.each do |mb_track| - unless mb_main_tracks_hashed.include? mb_track['recording']['title'].downcase.scan(/[a-z0-9]*/).join - mb_main_tracks_hashed << mb_track['recording']['title'].downcase.scan(/[a-z0-9]*/).join - mb_track['country'] = main_release['country'] unless main_release['country'].nil? - mb_track['bonus'] = 0 if mb_track['bonus'].nil? - end - end - - # Searching for tracks in other releases - releases_mb.each do |mb_release| + unless releases_mb['release_list']['release'].nil? or not albums_db.empty? - other_tracks_mb = [] - other_tracks_mb_r = brainz.release(mb_release['id'], {'inc' => 'recordings'})['release']['medium_list']['medium'] - if other_tracks_mb_r.is_a? Hash - if other_tracks_mb_r['track_list']['track'].is_a? Hash - other_tracks_mb_r['track_list']['track'] = [other_tracks_mb_r['track_list']['track']] + releases_mb = releases_mb['release_list']['release'] + + if releases_mb.is_a? Hash + releases_mb = [releases_mb] + end + + releases_mb.delete_if { |a| a['date'].nil? } + + unless releases_mb.empty? + + releases_mb.each do |release| + if release['date'].length == 4 + release['date'] += '-12-28' + elsif release['date'].length == 7 + release['date'] += '-28' end - other_tracks_mb = other_tracks_mb_r['track_list']['track'] - elsif other_tracks_mb_r.is_a? Array - other_tracks_mb_r.each do |cd| - if cd['track_list']['track'].is_a? Hash - cd['track_list']['track'] = [cd['track_list']['track']] + end + + # Sorting releases by date + releases_mb.sort! { |a, b| a['date'].to_date <=> b['date'].to_date } + main_release = releases_mb.shift + + # Get tracks from the first release and then exclude release + tracks_mb = [] + tracks_mb_r = brainz.release(main_release['id'], {'inc' => 'recordings'}) + + unless tracks_mb_r['release']['medium_list']['medium'].nil? + + tracks_mb_r = tracks_mb_r['release']['medium_list']['medium'] + + if tracks_mb_r.is_a? Hash + if tracks_mb_r['track_list']['track'].is_a? Hash + tracks_mb_r['track_list']['track'] = [tracks_mb_r['track_list']['track']] + end + tracks_mb = tracks_mb_r['track_list']['track'] + elsif tracks_mb_r.is_a? Array + tracks_mb = tracks_mb_r.shift['track_list']['track'] + tracks_mb_r.each do |cd| + if cd['track_list']['track'].is_a? Hash + cd['track_list']['track'] = [cd['track_list']['track']] + end + cd['track_list']['track'].each { |mb_track| mb_track['bonus'] = 1 } + tracks_mb |= cd['track_list']['track'] end - other_tracks_mb |= cd['track_list']['track'] end - end - - other_tracks_mb.each do |mb_track| - unless mb_main_tracks_hashed.include? mb_track['recording']['title'].downcase.scan(/[a-z0-9]*/).join - mb_track['country'] = mb_release['country'] unless mb_release['country'].nil? - mb_track['bonus'] = 1 - tracks_mb << mb_track - end - end - end - - # Saving tracks - tracks_mb.each do |mb_track| - unless ['[silence]', '[untitled]'].include? mb_track['recording']['title'] - track = Track.new - track.name = mb_track['recording']['title'].gsub(/\s\\\s\[.*?\]/, '') - track.album_id = album.id - track.position = mb_track['position'] - track.length = mb_track['length'] unless mb_track['length'].nil? - track.length = mb_track['recording']['length'] unless mb_track['recording']['length'].nil? - track.country = mb_track['country'] - track.bonus = mb_track['bonus'] - track.live = mb_track['recording']['title'].downcase.include? 'live' - track.acoustic = mb_track['recording']['title'].downcase.include? 'acoustic' - track.mbid = mb_track['recording']['id'] - track.save - end - end - - end # unless mb_main_tracks.empty? - end # unless mb_releases.empty? + unless tracks_mb.empty? + + # Saving album + begin + album_lastfm = lastfm.album.get_info(artist_lastfm['name'], album_name) + album_image = album_lastfm['image'][3]['content'] + rescue + album_image = '' + end + + album = Album.new + album.name = album_name + album.year = main_release['date'].to_date.year + album.artist_id = artist.id + album.mbid = mb_album['id'] + album.album_type = mb_album['type'] + album.pic_url = album_image + album.save + + # Creating hashed array for matching + mb_main_tracks_hashed = [] + tracks_mb.each do |mb_track| + unless mb_main_tracks_hashed.include? mb_track['recording']['title'].downcase.scan(/[a-z0-9]*/).join + mb_main_tracks_hashed << mb_track['recording']['title'].downcase.scan(/[a-z0-9]*/).join + mb_track['country'] = main_release['country'] unless main_release['country'].nil? + mb_track['bonus'] = 0 if mb_track['bonus'].nil? + end + end + + # Searching for tracks in other releases + releases_mb.each do |mb_release| + + other_tracks_mb = [] + other_tracks_mb_r = brainz.release(mb_release['id'], {'inc' => 'recordings'})['release']['medium_list']['medium'] + if other_tracks_mb_r.is_a? Hash + if other_tracks_mb_r['track_list']['track'].is_a? Hash + other_tracks_mb_r['track_list']['track'] = [other_tracks_mb_r['track_list']['track']] + end + other_tracks_mb = other_tracks_mb_r['track_list']['track'] + elsif other_tracks_mb_r.is_a? Array + other_tracks_mb_r.each do |cd| + if cd['track_list']['track'].is_a? Hash + cd['track_list']['track'] = [cd['track_list']['track']] + end + other_tracks_mb |= cd['track_list']['track'] + end + end + + other_tracks_mb.each do |mb_track| + unless mb_main_tracks_hashed.include? mb_track['recording']['title'].downcase.scan(/[a-z0-9]*/).join + mb_track['country'] = mb_release['country'] unless mb_release['country'].nil? + mb_track['bonus'] = 1 + tracks_mb << mb_track + end + end + end + + # Saving tracks + tracks_mb.each do |mb_track| + unless ['[silence]', '[untitled]'].include? mb_track['recording']['title'] + track = Track.new + track.name = mb_track['recording']['title'].gsub(/\s\\\s\[.*?\]/, '') + track.album_id = album.id + track.position = mb_track['position'] + track.length = mb_track['length'] unless mb_track['length'].nil? + track.length = mb_track['recording']['length'] unless mb_track['recording']['length'].nil? + track.country = mb_track['country'] + track.bonus = mb_track['bonus'] + track.live = mb_track['recording']['title'].downcase.include? 'live' + track.acoustic = mb_track['recording']['title'].downcase.include? 'acoustic' + track.mbid = mb_track['recording']['id'] + track.save + end + end + + end # unless tracks_mb_r['release']['medium_list']['medium'].nil? + + end # releases_mb.empty? + + end # unless mb_main_tracks.empty? + + end # unless releases_mb.nil? - end # mb_albums.each do |mb_album| + end # release_groups_mb.each do |mb_album| + + end # unless release_groups_mb['release_group_list']['release_group'].nil? artist.status = 1 diff --git a/lib/tasks/db_import.rake b/lib/tasks/db_import.rake index c7bb27d..caa1958 100644 --- a/lib/tasks/db_import.rake +++ b/lib/tasks/db_import.rake @@ -4,7 +4,7 @@ namespace :db do desc 'Imports test data from MusicBrainz database' task :import => :environment do - ati = ['The Lonely Island'] + ati = ['Weezer'] ati.each do |name| ImportController.importArtist(name) #ap res = ArtistController.musicBrainzExactSearch(name) diff --git a/public/css/layout.css b/public/css/layout.css index 069caa1..bd183c8 100644 --- a/public/css/layout.css +++ b/public/css/layout.css @@ -4,8 +4,11 @@ border: none; } -body { +html, body { overflow: hidden; +} + +body { font-family: Helvetica, Arial, sans-serif; font-size: 14px; -moz-user-select: none; @@ -14,6 +17,7 @@ body { user-select: none; cursor: default; min-width: 1000px; + width: 100%; } #header-container { diff --git a/public/js/beathaven/player.js b/public/js/beathaven/player.js index ad532cf..ed8e9af 100644 --- a/public/js/beathaven/player.js +++ b/public/js/beathaven/player.js @@ -41,6 +41,7 @@ var Player = { }, addTrack: function(artist, track, length) { + var initial_count = $('.playlist-tracks li').length; $('.playlist-tracks').append('\
  • \
    \ @@ -54,6 +55,10 @@ var Player = { $('#playlist').html($('.playlist-tracks')).scrollbar(); $('.playlist-tracks').sortable(); + + if (initial_count === 0 && !Player.hasTrack()) { + Player.setTrack($('.playlist-tracks li').first().attr('id').split('i')[1]); + } }, setTrack: function(id) { @@ -71,6 +76,16 @@ var Player = { Player.updateNowListening(tmp[0], tmp[1]); }, + hasTrack: function() { + if ($('#jplayer audio').length > 0) { + return typeof $('#jplayer audio').attr('src') !== 'undefined'; + } else if ($('#jplayer audio').length > 0) { + $('#jplayer').jPlayer('play'); + return true; + } + return false; + }, + playSource: function(url) { $('#jplayer').jPlayer('setMedia', {mp3: url}).jPlayer('play'); Player.scrobbled = false; @@ -132,6 +147,12 @@ $('.add-track').live('click', function(){ Player.addTrack(artist, track_name, length); }); +$('#player .play').live('click', function(){ + if ($('.playlist-tracks li').length > 0 && !Player.hasTrack()) { + Player.setTrack($('.playlist-tracks li').first().attr('id').split('i')[1]); + } +}); + $('.tracklist li').live('mouseover mouseout', function(e){ if (e.type == 'mouseover') { $(this).find('.add-track').show();