diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 84c8101..c8138cc 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -8,11 +8,11 @@ class ApplicationController < ActionController::Base def index; end def greetings - render :partial => 'greetings' + render_compact_partial :greetings end def about - render :partial => 'about' + render_compact_partial :about end protected @@ -33,7 +33,7 @@ protected def session_key if cookies[:beathaven_sid].nil? cookies[:beathaven_sid] = { - :value => Digest::SHA1.hexdigest(Beathaven::Application.config.secret_token+Time.now.to_f.to_s+Random.rand().to_s), + :value => Session.generate_key, :expire => 42.years.from_now.utc } end diff --git a/app/controllers/artist_controller.rb b/app/controllers/artist_controller.rb index 21bc94c..9c69ef9 100644 --- a/app/controllers/artist_controller.rb +++ b/app/controllers/artist_controller.rb @@ -1,5 +1,3 @@ -require 'musicbrainz' - class ArtistController < ApplicationController def data # Bad params @@ -9,7 +7,7 @@ class ArtistController < ApplicationController # Searching for artist artist_name = get_artist_name_from_query - @artist = Artist.find_by_name(artist_name, include: {artist_links: {}, albums: {tracks: {}}}) + @artist = Artist.find_by_name(artist_name, include: { artist_links: {}, albums: { tracks: {}}}) # Artist not found in DB unless @artist @@ -38,7 +36,7 @@ class ArtistController < ApplicationController status: @artist.status_str, artist: @artist, html: render_compact_partial(:page) - }, include: {albums: {include: {tracks: {}}}} + }, include: { albums: { include: { tracks: {}}}} end private diff --git a/app/controllers/import_controller.rb b/app/controllers/import_controller.rb index c2de0ec..a397a19 100644 --- a/app/controllers/import_controller.rb +++ b/app/controllers/import_controller.rb @@ -49,6 +49,10 @@ class ImportController < ApplicationController ).save end + if brainz_artist.urls[:discogs].nil? + brainz_artist.urls[:discogs] = Discogs.search_for_artist(artist.name) + end + unless brainz_artist.urls[:discogs].nil? Discogs.get_master_albums(brainz_artist.urls[:discogs]).each do |info| begin diff --git a/app/models/beathaven/session.rb b/app/models/beathaven/session.rb index c66afec..764f42d 100644 --- a/app/models/beathaven/session.rb +++ b/app/models/beathaven/session.rb @@ -1,3 +1,7 @@ class Session < ActiveRecord::Base belongs_to :user + + def self.generate_key + Digest::SHA1.hexdigest(Beathaven::Application.config.secret_token + Time.now.to_f.to_s + Random.rand().to_s) + end end diff --git a/lib/discogs.rb b/lib/discogs.rb index 92a54c7..f2de145 100644 --- a/lib/discogs.rb +++ b/lib/discogs.rb @@ -1,3 +1,5 @@ +require 'zlib' + class Discogs def self.artists @@ -148,7 +150,22 @@ class Discogs albums end - private + def self.search_for_artist name + url = 'http://api.discogs.com/search?q=' << CGI::escape(name) << '&f=xml' + stream = open(url, + 'User-Agent' => 'Haven Import Bot', + 'Accept-Encoding' => 'gzip, deflate' + ) + if (stream.content_encoding.empty?) + body = stream.read + else + body = Zlib::GzipReader.new(stream).read + end + x = Nokogiri::XML(body) + x.css('exactresults > result[type=artist] > uri, searchresults > result[type=artist] > uri').map{|n| n.text}.first + end + +private def self.get_nodes filename, nodename, &block File.open(filename) do |file|