1
0
Fork 0
This commit is contained in:
Gregory Eremin 2011-09-22 14:37:53 +04:00
parent dbdff13cd0
commit 3ea944fbb0
23 changed files with 206 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,3 @@
class ArtistLink < ActiveRecord::Base
belongs_to :artist
end

View File

@ -0,0 +1,5 @@
module LocalBrainz
def self.table_name_prefix
'local_brainz_'
end
end

View 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

View File

@ -0,0 +1,3 @@
class LocalBrainz::Track < ActiveRecord::Base
belongs_to :local_brainz_release, :class_name => 'LocalBrainz::Release'
end

View 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

View 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

View 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

View File

@ -0,0 +1,5 @@
class AddArtistIdToLocalBrainzRelease < ActiveRecord::Migration
def change
add_column :local_brainz_releases, :artist_id, :integer
end
end

View File

@ -0,0 +1,5 @@
class AddFormatToLocalBrainzRelease < ActiveRecord::Migration
def change
add_column :local_brainz_releases, :format, :string
end
end

View File

@ -0,0 +1,5 @@
class AddReleaseIdToLocalBrainzTrack < ActiveRecord::Migration
def change
add_column :local_brainz_tracks, :release_id, :integer
end
end

View File

@ -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

View File

@ -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"

View File

@ -0,0 +1,7 @@
namespace :artist do
desc 'Empty artist database'
task :wipe => :environment do
Album.destroy_all
ArtistLink.destroy_all
end
end

View File

@ -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
View 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
View 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
View 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

View File

@ -0,0 +1,7 @@
require 'test_helper'
class ArtistLinkTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,7 @@
require 'test_helper'
class LocalBrainz::ReleaseTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,7 @@
require 'test_helper'
class LocalBrainz::TrackTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end