From 7a3444d10390953a8dc0908caf68ab26281d8a3b Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Thu, 22 Sep 2011 16:04:59 +0400 Subject: [PATCH] Disable barista, xml parse refactor --- Gemfile | 2 +- Gemfile.lock | 3 - config/initializers/barista_config.rb | 128 ++++++++++++------------- lib/discogs.rb | 57 ++++++----- lib/tasks/discogs_import_releases.rake | 9 ++ 5 files changed, 106 insertions(+), 93 deletions(-) create mode 100644 lib/tasks/discogs_import_releases.rake diff --git a/Gemfile b/Gemfile index f10aadc..46bc76b 100644 --- a/Gemfile +++ b/Gemfile @@ -10,7 +10,7 @@ gem 'sass' gem 'coffee-script' gem 'therubyracer', :require => false -gem 'barista' +# gem 'barista' gem 'awesome_print', :require => 'ap' gem 'delayed_job' diff --git a/Gemfile.lock b/Gemfile.lock index 5ba961e..725be8a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -39,8 +39,6 @@ GEM multi_json (~> 1.0) arel (2.2.1) awesome_print (0.4.0) - barista (1.2.1) - coffee-script (~> 2.2) bcrypt-ruby (3.0.1) bitmask_attributes (0.2.2) activerecord (~> 3.0) @@ -121,7 +119,6 @@ PLATFORMS DEPENDENCIES awesome_print - barista bitmask_attributes coffee-script delayed_job diff --git a/config/initializers/barista_config.rb b/config/initializers/barista_config.rb index 7811a7a..cf268f5 100644 --- a/config/initializers/barista_config.rb +++ b/config/initializers/barista_config.rb @@ -1,64 +1,64 @@ -# Configure barista. -Barista.configure do |c| - - # Change the root to use app/scripts - # c.root = Rails.root.join("app", "coffeescripts") - - # Change the output root, causing Barista to compile into public/coffeescripts - # c.output_root = Rails.root.join("public", "assets", "coffee") - # - # Disable auto compile, use generated file directly: - # c.auto_compile = false - - # Add a new framework: - - # c.register :tests, :root => Rails.root.join('test', 'coffeescript'), :output_prefix => 'test' - - # Disable wrapping in a closure: - c.bare = true - # ... or ... - # c.bare! - - # Change the output root for a framework: - - # c.change_output_prefix! 'framework-name', 'output-prefix' - - # or for all frameworks... - - # c.each_framework do |framework| - # c.change_output_prefix! framework, "vendor/#{framework.name}" - # end - - # or, prefix the path for the app files: - - # c.change_output_prefix! :default, 'my-app-name' - - # or, change the directory the framework goes into full stop: - - # c.change_output_prefix! :tests, Rails.root.join('spec', 'javascripts') - - # or, hook into the compilation: - - # c.before_compilation { |path| puts "Barista: Compiling #{path}" } - # c.on_compilation { |path| puts "Barista: Successfully compiled #{path}" } - # c.on_compilation_error { |path, output| puts "Barista: Compilation of #{path} failed with:\n#{output}" } - # c.on_compilation_with_warning { |path, output| puts "Barista: Compilation of #{path} had a warning:\n#{output}" } - - # Turn off preambles and exceptions on failure: - - # c.verbose = false - - # Or, make sure it is always on - # c.verbose! - - # If you want to use a custom JS file, you can as well - # e.g. vendoring CoffeeScript in your application: - # c.js_path = Rails.root.join('public', 'javascripts', 'coffee-script.js') - - # Make helpers and the HAML filter output coffee-script instead of the compiled JS. - # Used in combination with the coffeescript_interpreter_js helper in Rails. - # c.embedded_interpreter = true - - c.add_preamble = false - -end +# # Configure barista. +# Barista.configure do |c| +# +# # Change the root to use app/scripts +# # c.root = Rails.root.join("app", "coffeescripts") +# +# # Change the output root, causing Barista to compile into public/coffeescripts +# # c.output_root = Rails.root.join("public", "assets", "coffee") +# # +# # Disable auto compile, use generated file directly: +# # c.auto_compile = false +# +# # Add a new framework: +# +# # c.register :tests, :root => Rails.root.join('test', 'coffeescript'), :output_prefix => 'test' +# +# # Disable wrapping in a closure: +# c.bare = true +# # ... or ... +# # c.bare! +# +# # Change the output root for a framework: +# +# # c.change_output_prefix! 'framework-name', 'output-prefix' +# +# # or for all frameworks... +# +# # c.each_framework do |framework| +# # c.change_output_prefix! framework, "vendor/#{framework.name}" +# # end +# +# # or, prefix the path for the app files: +# +# # c.change_output_prefix! :default, 'my-app-name' +# +# # or, change the directory the framework goes into full stop: +# +# # c.change_output_prefix! :tests, Rails.root.join('spec', 'javascripts') +# +# # or, hook into the compilation: +# +# # c.before_compilation { |path| puts "Barista: Compiling #{path}" } +# # c.on_compilation { |path| puts "Barista: Successfully compiled #{path}" } +# # c.on_compilation_error { |path, output| puts "Barista: Compilation of #{path} failed with:\n#{output}" } +# # c.on_compilation_with_warning { |path, output| puts "Barista: Compilation of #{path} had a warning:\n#{output}" } +# +# # Turn off preambles and exceptions on failure: +# +# # c.verbose = false +# +# # Or, make sure it is always on +# # c.verbose! +# +# # If you want to use a custom JS file, you can as well +# # e.g. vendoring CoffeeScript in your application: +# # c.js_path = Rails.root.join('public', 'javascripts', 'coffee-script.js') +# +# # Make helpers and the HAML filter output coffee-script instead of the compiled JS. +# # Used in combination with the coffeescript_interpreter_js helper in Rails. +# # c.embedded_interpreter = true +# +# c.add_preamble = false +# +# end diff --git a/lib/discogs.rb b/lib/discogs.rb index 8d506ad..0e442fd 100644 --- a/lib/discogs.rb +++ b/lib/discogs.rb @@ -1,34 +1,41 @@ class Discogs + def self.artists - File.open('tmp/data/discogs_artists.xml') do |file| + self.get_nodes('tmp/data/discogs_artists.xml', 'artist') do + artist = Artist.new( + :name => (node.css('name').first.text), + :pic_url => (node.css('images > image[type="primary"]').first.attr('uri') unless node.css('images > image[type="primary"]').empty?), + :status => 1 + ) + artist.save + node.css('namevariations > name, aliases > name').each do |v| + ArtistAlias.new( + :artist_id => artist.id, + :name => v.text + ).save + end + end + end + + def self.releases + self.get_nodes('tmp/data/discogs_releases_test.xml', 'release') do |node| + r = { + :pic => (node.css('images > image[type="primary"]').first.attr('uri') unless node.css('images > image[type="primary"]').empty?) + } + ap r + end + end + + private + + def self.get_nodes filename, nodename, &block + File.open(filename) do |file| Nokogiri::XML::Reader.from_io(file).each do |node| - if node.name == 'artist' and node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT - self.process_artist Nokogiri::XML(node.outer_xml).root + if node.name == nodename and node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT + yield(Nokogiri::XML(node.outer_xml).root) end end end end - def self.process_artist node - artist = Artist.new( - :name => (node.css('name').first.text), - :pic_url => (node.css('images > image[type="primary"]').first.attr('uri') unless node.css('images > image[type="primary"]').empty?), - :status => 1 - ) - artist.save - node.css('namevariations > name, aliases > name').each do |v| - ArtistAlias.new( - :artist_id => artist.id, - :name => v.text - ).save - end - end - - def self.releases - - end - - def self.process_release - - end end \ No newline at end of file diff --git a/lib/tasks/discogs_import_releases.rake b/lib/tasks/discogs_import_releases.rake new file mode 100644 index 0000000..e8c7bee --- /dev/null +++ b/lib/tasks/discogs_import_releases.rake @@ -0,0 +1,9 @@ +require "discogs" +namespace :discogs do + namespace :import do + desc 'Import discogs releases xml' + task :releases => :environment do + Discogs.releases + end + end +end