Artist wipe, local brainz
This commit is contained in:
		
							parent
							
								
									3cd6c111bd
								
							
						
					
					
						commit
						2536645cb8
					
				@ -54,7 +54,7 @@ GEM
 | 
			
		||||
      activesupport (~> 3.0)
 | 
			
		||||
      daemons
 | 
			
		||||
    erubis (2.7.0)
 | 
			
		||||
    execjs (1.2.6)
 | 
			
		||||
    execjs (1.2.7)
 | 
			
		||||
      multi_json (~> 1.0)
 | 
			
		||||
    haml (3.1.3)
 | 
			
		||||
    hike (1.2.1)
 | 
			
		||||
@ -67,7 +67,7 @@ GEM
 | 
			
		||||
      treetop (~> 1.4.8)
 | 
			
		||||
    mime-types (1.16)
 | 
			
		||||
    multi_json (1.0.3)
 | 
			
		||||
    musicbrainz (0.4.7)
 | 
			
		||||
    musicbrainz (0.4.9)
 | 
			
		||||
      nokogiri
 | 
			
		||||
    nokogiri (1.5.0)
 | 
			
		||||
    pg (0.11.0)
 | 
			
		||||
 | 
			
		||||
@ -37,7 +37,15 @@ class ImportController < ApplicationController
 | 
			
		||||
      artist.listeners = lastfm_artist['artist']['stats']['listeners']
 | 
			
		||||
      artist.artist_type = brainz_artist.type
 | 
			
		||||
      artist.mbid = brainz_artist.id
 | 
			
		||||
      dry_run ? ap(artist) : artist.save
 | 
			
		||||
      artist.save
 | 
			
		||||
      
 | 
			
		||||
      brainz_artist.urls.each do |service, url|
 | 
			
		||||
        ArtistLink.new(
 | 
			
		||||
          :artist_id => artist.id,
 | 
			
		||||
          :service => service.to_s,
 | 
			
		||||
          :url => url
 | 
			
		||||
        ).save
 | 
			
		||||
      end
 | 
			
		||||
    
 | 
			
		||||
      brainz_artist.release_groups.each do |brainz_release_group|
 | 
			
		||||
        # Saving album
 | 
			
		||||
@ -56,12 +64,30 @@ class ImportController < ApplicationController
 | 
			
		||||
        album.pic_url = album_image
 | 
			
		||||
        album.has_pic = (album_image != '' and not album_image.nil?)
 | 
			
		||||
        album.status = 0
 | 
			
		||||
        dry_run ? ap(album) : album.save
 | 
			
		||||
        album.save
 | 
			
		||||
        # Tracks from the first release
 | 
			
		||||
        tracks_hashed = []
 | 
			
		||||
        brainz_release_group.releases.each_with_index do |brainz_release, i|
 | 
			
		||||
          local_brainz_release = LocalBrainz::Release.new(
 | 
			
		||||
            :mbid => brainz_release.id,
 | 
			
		||||
            :title => brainz_release.title,
 | 
			
		||||
            :status => brainz_release.status,
 | 
			
		||||
            :date => brainz_release.date,
 | 
			
		||||
            :country => brainz_release.country,
 | 
			
		||||
            :format => brainz_release.format,
 | 
			
		||||
            :album_id => album.id
 | 
			
		||||
          )
 | 
			
		||||
          local_brainz_release.save
 | 
			
		||||
          # Processing tracks
 | 
			
		||||
          brainz_release.tracks.each do |brainz_track|
 | 
			
		||||
            local_brainz_track = LocalBrainz::Track.new(
 | 
			
		||||
              :position => brainz_track.position,
 | 
			
		||||
              :recording_id => brainz_track.recording_id,
 | 
			
		||||
              :title => brainz_track.title,
 | 
			
		||||
              :length => brainz_track.length,
 | 
			
		||||
              :release_id => local_brainz_release.id
 | 
			
		||||
            )
 | 
			
		||||
            local_brainz_track.save
 | 
			
		||||
            track_title = brainz_track.title.gsub(/\s\/\s\[.*?\]/, '')
 | 
			
		||||
            if tracks_hashed.include? track_title.downcase.scan(/[a-z0-9]*/).join
 | 
			
		||||
              next
 | 
			
		||||
@ -77,7 +103,7 @@ class ImportController < ApplicationController
 | 
			
		||||
            track.live = track_title.downcase.include? 'live'
 | 
			
		||||
            track.acoustic = track_title.downcase.include? 'acoustic'
 | 
			
		||||
            track.mbid = brainz_track.recording_id
 | 
			
		||||
            dry_run ? ap(track) : track.save
 | 
			
		||||
            track.save
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
        album.status = 1
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
class Album < ActiveRecord::Base
 | 
			
		||||
  belongs_to :artist
 | 
			
		||||
  has_many :tracks, :order => 'bonus ASC, position ASC', :dependent => :destroy
 | 
			
		||||
  has_many :local_brainz_releases, :class_name => 'LocalBrainz::Release', :dependent => :destroy
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
class Artist < ActiveRecord::Base
 | 
			
		||||
  has_many :albums, :order => 'has_pic DESC, year ASC, id ASC', :dependent => :destroy
 | 
			
		||||
  has_many :artist_links, :dependent => :destroy
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										3
									
								
								app/models/artist_link.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								app/models/artist_link.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
			
		||||
class ArtistLink < ActiveRecord::Base
 | 
			
		||||
  belongs_to :artist
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										5
									
								
								app/models/local_brainz.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								app/models/local_brainz.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,5 @@
 | 
			
		||||
module LocalBrainz
 | 
			
		||||
  def self.table_name_prefix
 | 
			
		||||
    'local_brainz_'
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										4
									
								
								app/models/local_brainz/release.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								app/models/local_brainz/release.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
			
		||||
class LocalBrainz::Release < ActiveRecord::Base
 | 
			
		||||
  belongs_to :album, :class_name => 'Album'
 | 
			
		||||
  has_many :local_brainz_tracks, :class_name => 'LocalBrainz::Track', :dependent => :destroy
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										3
									
								
								app/models/local_brainz/track.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								app/models/local_brainz/track.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
			
		||||
class LocalBrainz::Track < ActiveRecord::Base
 | 
			
		||||
  belongs_to :local_brainz_release, :class_name => 'LocalBrainz::Release'
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										13
									
								
								db/migrate/20110920152432_create_local_brainz_releases.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								db/migrate/20110920152432_create_local_brainz_releases.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
			
		||||
class CreateLocalBrainzReleases < ActiveRecord::Migration
 | 
			
		||||
  def change
 | 
			
		||||
    create_table :local_brainz_releases do |t|
 | 
			
		||||
      t.string :mbid
 | 
			
		||||
      t.string :title
 | 
			
		||||
      t.string :status
 | 
			
		||||
      t.date :date
 | 
			
		||||
      t.string :country
 | 
			
		||||
 | 
			
		||||
      t.timestamps
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										12
									
								
								db/migrate/20110920152641_create_local_brainz_tracks.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								db/migrate/20110920152641_create_local_brainz_tracks.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
			
		||||
class CreateLocalBrainzTracks < ActiveRecord::Migration
 | 
			
		||||
  def change
 | 
			
		||||
    create_table :local_brainz_tracks do |t|
 | 
			
		||||
      t.integer :position
 | 
			
		||||
      t.string :recording_id
 | 
			
		||||
      t.string :title
 | 
			
		||||
      t.integer :length
 | 
			
		||||
 | 
			
		||||
      t.timestamps
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										11
									
								
								db/migrate/20110920154338_create_artist_links.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								db/migrate/20110920154338_create_artist_links.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,11 @@
 | 
			
		||||
class CreateArtistLinks < ActiveRecord::Migration
 | 
			
		||||
  def change
 | 
			
		||||
    create_table :artist_links do |t|
 | 
			
		||||
      t.integer :artist_id
 | 
			
		||||
      t.string :service
 | 
			
		||||
      t.string :url
 | 
			
		||||
 | 
			
		||||
      t.timestamps
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@ -0,0 +1,5 @@
 | 
			
		||||
class AddArtistIdToLocalBrainzRelease < ActiveRecord::Migration
 | 
			
		||||
  def change
 | 
			
		||||
    add_column :local_brainz_releases, :artist_id, :integer
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@ -0,0 +1,5 @@
 | 
			
		||||
class AddFormatToLocalBrainzRelease < ActiveRecord::Migration
 | 
			
		||||
  def change
 | 
			
		||||
    add_column :local_brainz_releases, :format, :string
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@ -0,0 +1,5 @@
 | 
			
		||||
class AddReleaseIdToLocalBrainzTrack < ActiveRecord::Migration
 | 
			
		||||
  def change
 | 
			
		||||
    add_column :local_brainz_tracks, :release_id, :integer
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@ -0,0 +1,9 @@
 | 
			
		||||
class RenameFieldInLocalBrainzReleases < ActiveRecord::Migration
 | 
			
		||||
  def up
 | 
			
		||||
    rename_column :local_brainz_releases, :artist_id, :album_id
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def down
 | 
			
		||||
    rename_column :local_brainz_releases, :album_id, :artist_id
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										32
									
								
								db/schema.rb
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								db/schema.rb
									
									
									
									
									
								
							@ -11,7 +11,7 @@
 | 
			
		||||
#
 | 
			
		||||
# It's strongly recommended to check this file into your version control system.
 | 
			
		||||
 | 
			
		||||
ActiveRecord::Schema.define(:version => 20110919003651) do
 | 
			
		||||
ActiveRecord::Schema.define(:version => 20110920164044) do
 | 
			
		||||
 | 
			
		||||
  create_table "albums", :force => true do |t|
 | 
			
		||||
    t.string   "name"
 | 
			
		||||
@ -26,6 +26,14 @@ ActiveRecord::Schema.define(:version => 20110919003651) do
 | 
			
		||||
    t.integer  "status"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  create_table "artist_links", :force => true do |t|
 | 
			
		||||
    t.integer  "artist_id"
 | 
			
		||||
    t.string   "service"
 | 
			
		||||
    t.string   "url"
 | 
			
		||||
    t.datetime "created_at"
 | 
			
		||||
    t.datetime "updated_at"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  create_table "artists", :force => true do |t|
 | 
			
		||||
    t.string   "name"
 | 
			
		||||
    t.text     "desc"
 | 
			
		||||
@ -63,6 +71,28 @@ ActiveRecord::Schema.define(:version => 20110919003651) do
 | 
			
		||||
    t.datetime "updated_at"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  create_table "local_brainz_releases", :force => true do |t|
 | 
			
		||||
    t.string   "mbid"
 | 
			
		||||
    t.string   "title"
 | 
			
		||||
    t.string   "status"
 | 
			
		||||
    t.date     "date"
 | 
			
		||||
    t.string   "country"
 | 
			
		||||
    t.datetime "created_at"
 | 
			
		||||
    t.datetime "updated_at"
 | 
			
		||||
    t.integer  "album_id"
 | 
			
		||||
    t.string   "format"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  create_table "local_brainz_tracks", :force => true do |t|
 | 
			
		||||
    t.integer  "position"
 | 
			
		||||
    t.string   "recording_id"
 | 
			
		||||
    t.string   "title"
 | 
			
		||||
    t.integer  "length"
 | 
			
		||||
    t.datetime "created_at"
 | 
			
		||||
    t.datetime "updated_at"
 | 
			
		||||
    t.integer  "release_id"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  create_table "playlist_items", :force => true do |t|
 | 
			
		||||
    t.integer  "playlist_id"
 | 
			
		||||
    t.integer  "track_id"
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										7
									
								
								lib/tasks/artist_wipe.rake
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								lib/tasks/artist_wipe.rake
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
			
		||||
namespace :artist do
 | 
			
		||||
  desc 'Empty artist database'
 | 
			
		||||
  task :wipe => :environment do
 | 
			
		||||
    Album.destroy_all
 | 
			
		||||
    ArtistLink.destroy_all
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@ -5,7 +5,7 @@ namespace :lastfm do
 | 
			
		||||
      ImportController.parseLastfmXML('tmp/data/top1000.xml').each do |artist|
 | 
			
		||||
        puts artist[:name] + (artist[:mbid].empty? ? '' : ' (' + artist[:mbid] + ')')
 | 
			
		||||
        record = Artist.find_or_create_by_name(artist[:name])
 | 
			
		||||
        record.mbid = artist[:mbid]
 | 
			
		||||
        # record.mbid = artist[:mbid]
 | 
			
		||||
        record.save
 | 
			
		||||
        res = ImportController.importArtist(artist[:name])
 | 
			
		||||
        if res == 1
 | 
			
		||||
@ -22,7 +22,7 @@ namespace :lastfm do
 | 
			
		||||
      ImportController.parseLastfmXML('tmp/data/hyped500.xml').each do |artist|
 | 
			
		||||
        puts artist[:name] + (artist[:mbid].empty? ? '' : ' (' + artist[:mbid] + ')')
 | 
			
		||||
        record = Artist.find_or_create_by_name(artist[:name])
 | 
			
		||||
        record.mbid = artist[:mbid]
 | 
			
		||||
        # record.mbid = artist[:mbid]
 | 
			
		||||
        record.save
 | 
			
		||||
        res = ImportController.importArtist(artist[:name])
 | 
			
		||||
        if res == 1
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										11
									
								
								test/fixtures/artist_links.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								test/fixtures/artist_links.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,11 @@
 | 
			
		||||
# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
 | 
			
		||||
 | 
			
		||||
one:
 | 
			
		||||
  artist_id: 1
 | 
			
		||||
  service: MyString
 | 
			
		||||
  url: MyString
 | 
			
		||||
 | 
			
		||||
two:
 | 
			
		||||
  artist_id: 1
 | 
			
		||||
  service: MyString
 | 
			
		||||
  url: MyString
 | 
			
		||||
							
								
								
									
										15
									
								
								test/fixtures/local_brainz/releases.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								test/fixtures/local_brainz/releases.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,15 @@
 | 
			
		||||
# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
 | 
			
		||||
 | 
			
		||||
one:
 | 
			
		||||
  mbid: MyString
 | 
			
		||||
  title: MyString
 | 
			
		||||
  status: MyString
 | 
			
		||||
  date: 2011-09-20
 | 
			
		||||
  country: MyString
 | 
			
		||||
 | 
			
		||||
two:
 | 
			
		||||
  mbid: MyString
 | 
			
		||||
  title: MyString
 | 
			
		||||
  status: MyString
 | 
			
		||||
  date: 2011-09-20
 | 
			
		||||
  country: MyString
 | 
			
		||||
							
								
								
									
										13
									
								
								test/fixtures/local_brainz/tracks.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								test/fixtures/local_brainz/tracks.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
			
		||||
# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
 | 
			
		||||
 | 
			
		||||
one:
 | 
			
		||||
  position: 1
 | 
			
		||||
  recording_id: MyString
 | 
			
		||||
  title: MyString
 | 
			
		||||
  length: 1
 | 
			
		||||
 | 
			
		||||
two:
 | 
			
		||||
  position: 1
 | 
			
		||||
  recording_id: MyString
 | 
			
		||||
  title: MyString
 | 
			
		||||
  length: 1
 | 
			
		||||
							
								
								
									
										7
									
								
								test/unit/artist_link_test.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								test/unit/artist_link_test.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
			
		||||
require 'test_helper'
 | 
			
		||||
 | 
			
		||||
class ArtistLinkTest < ActiveSupport::TestCase
 | 
			
		||||
  # test "the truth" do
 | 
			
		||||
  #   assert true
 | 
			
		||||
  # end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										7
									
								
								test/unit/local_brainz/release_test.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								test/unit/local_brainz/release_test.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
			
		||||
require 'test_helper'
 | 
			
		||||
 | 
			
		||||
class LocalBrainz::ReleaseTest < ActiveSupport::TestCase
 | 
			
		||||
  # test "the truth" do
 | 
			
		||||
  #   assert true
 | 
			
		||||
  # end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										7
									
								
								test/unit/local_brainz/track_test.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								test/unit/local_brainz/track_test.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
			
		||||
require 'test_helper'
 | 
			
		||||
 | 
			
		||||
class LocalBrainz::TrackTest < ActiveSupport::TestCase
 | 
			
		||||
  # test "the truth" do
 | 
			
		||||
  #   assert true
 | 
			
		||||
  # end
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user