Use magic encoding to ret rid of "invalid multibyte chat (US-ASCII)" errors. track_search is now renamed recording_search. recording_search now works (I mixed up the inputs).
This commit is contained in:
		
							parent
							
								
									813b6f0383
								
							
						
					
					
						commit
						2d4c1c42fd
					
				
							
								
								
									
										4
									
								
								Gemfile
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								Gemfile
									
									
									
									
									
								
							@ -1,2 +1,6 @@
 | 
			
		||||
source 'https://rubygems.org'
 | 
			
		||||
gemspec
 | 
			
		||||
 | 
			
		||||
group :test do
 | 
			
		||||
	gem "debugger"
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										20
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								README.md
									
									
									
									
									
								
							@ -121,11 +121,10 @@ MusicBrainz::Release.find(id)
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
MusicBrainz::Track
 | 
			
		||||
MusicBrainz::Track (depreciated, now called Recording)
 | 
			
		||||
```ruby
 | 
			
		||||
# Class Methods
 | 
			
		||||
MusicBrainz::Track.find(id)
 | 
			
		||||
MusicBrainz::ReleaseGroup.search(artist_name, track_name)
 | 
			
		||||
 | 
			
		||||
# Fields
 | 
			
		||||
{
 | 
			
		||||
@ -136,6 +135,23 @@ MusicBrainz::ReleaseGroup.search(artist_name, track_name)
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
MusicBrainz::Recording
 | 
			
		||||
```ruby
 | 
			
		||||
# Class Methods
 | 
			
		||||
MusicBrainz::Recording.find(id)
 | 
			
		||||
MusicBrainz::Recording.search(track_name, artist_name)
 | 
			
		||||
 | 
			
		||||
# Fields
 | 
			
		||||
{
 | 
			
		||||
  :id     	=> Integer,
 | 
			
		||||
  :mbid			=> Integer,
 | 
			
		||||
  :title		=> String,
 | 
			
		||||
  :artist		=> String,
 | 
			
		||||
	:releases	=> String,
 | 
			
		||||
	:score		=> Integer
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### Testing
 | 
			
		||||
```
 | 
			
		||||
bundle exec rspec
 | 
			
		||||
 | 
			
		||||
@ -1,2 +1,3 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
require "musicbrainz"
 | 
			
		||||
MB = MusicBrainz
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,7 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
#!/bin/env ruby
 | 
			
		||||
# encoding: utf-8
 | 
			
		||||
 | 
			
		||||
require "digest/sha1"
 | 
			
		||||
require "fileutils"
 | 
			
		||||
require "date"
 | 
			
		||||
@ -20,6 +24,7 @@ require "musicbrainz/models/artist"
 | 
			
		||||
require "musicbrainz/models/release_group"
 | 
			
		||||
require "musicbrainz/models/release"
 | 
			
		||||
require "musicbrainz/models/track"
 | 
			
		||||
require "musicbrainz/models/recording"
 | 
			
		||||
 | 
			
		||||
require "musicbrainz/bindings/artist"
 | 
			
		||||
require "musicbrainz/bindings/artist_search"
 | 
			
		||||
@ -32,7 +37,8 @@ require "musicbrainz/bindings/release_group_releases"
 | 
			
		||||
require "musicbrainz/bindings/release"
 | 
			
		||||
require "musicbrainz/bindings/release_tracks"
 | 
			
		||||
require "musicbrainz/bindings/track"
 | 
			
		||||
require "musicbrainz/bindings/track_search"
 | 
			
		||||
require "musicbrainz/bindings/recording"
 | 
			
		||||
require "musicbrainz/bindings/recording_search"
 | 
			
		||||
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  GH_PAGE_URL = "http://git.io/brainz"
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
# encoding: UTF-8
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  module Bindings
 | 
			
		||||
    module Artist
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  module Bindings
 | 
			
		||||
    module ArtistReleaseGroups
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
# encoding: UTF-8
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  module Bindings
 | 
			
		||||
    module ArtistSearch
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										20
									
								
								lib/musicbrainz/bindings/recording.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								lib/musicbrainz/bindings/recording.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  module Bindings
 | 
			
		||||
    module Recording
 | 
			
		||||
      def parse(xml)
 | 
			
		||||
				xml = xml.xpath('./recording') unless xml.xpath('./recording').empty?
 | 
			
		||||
        {
 | 
			
		||||
					id: (xml.attribute('id').value rescue nil),
 | 
			
		||||
					mbid: (xml.attribute('id').value rescue nil), # Old shit
 | 
			
		||||
					title: (xml.xpath('./title').text.gsub(/[`’]/, "'") rescue nil),
 | 
			
		||||
					artist: (xml.xpath('./artist-credit/name-credit/artist/name').text rescue nil),
 | 
			
		||||
					releases: (xml.xpath('./release-list/release/title').map{ |xml| xml.text } rescue []),
 | 
			
		||||
					score: (xml.attribute('score').value.to_i rescue nil)
 | 
			
		||||
				}
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      extend self
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@ -1,7 +1,7 @@
 | 
			
		||||
# encoding: UTF-8
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  module Bindings
 | 
			
		||||
    module TrackSearch
 | 
			
		||||
    module RecordingSearch 
 | 
			
		||||
      def parse(xml)
 | 
			
		||||
        xml.xpath('./recording-list/recording').map do |xml|
 | 
			
		||||
          {
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  module Bindings
 | 
			
		||||
    module Release
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  module Bindings
 | 
			
		||||
    module ReleaseGroup
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  module Bindings
 | 
			
		||||
    module ReleaseGroupReleases
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
# encoding: UTF-8
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  module Bindings
 | 
			
		||||
    module ReleaseGroupSearch
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  module Bindings
 | 
			
		||||
    module ReleaseTracks
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  module Bindings
 | 
			
		||||
    module Track
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  class Client
 | 
			
		||||
    include ClientModules::TransparentProxy
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  module ClientModules
 | 
			
		||||
    module CachingProxy
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  module ClientModules
 | 
			
		||||
    module FailsafeProxy
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  module ClientModules
 | 
			
		||||
    module TransparentProxy
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  class Configuration
 | 
			
		||||
    attr_accessor :app_name, :app_version, :contact,
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  module Deprecated
 | 
			
		||||
    module ProxyConfig
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  class Middleware < Faraday::Middleware
 | 
			
		||||
    def call(env)
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  class Artist < BaseModel
 | 
			
		||||
    field :id, String
 | 
			
		||||
 | 
			
		||||
@ -24,15 +24,16 @@ module MusicBrainz
 | 
			
		||||
        MusicBrainz.client
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
			def search(hash, resource=nil)
 | 
			
		||||
			def find(hash)
 | 
			
		||||
				underscored_name = underscore_name.to_sym
 | 
			
		||||
				client.load(underscored_name, hash, { binding: underscored_name, create_model: underscored_name })
 | 
			
		||||
			end
 | 
			
		||||
 | 
			
		||||
			def search(hash)
 | 
			
		||||
				hash = escape_strings(hash)
 | 
			
		||||
				query_val = build_query(hash)
 | 
			
		||||
				underscore_name = self.name[13..-1].underscore
 | 
			
		||||
				if resource # only needed since "track" is really a "recording", ugly
 | 
			
		||||
					client.load(resource, { query: query_val, limit: 10 }, { binding: underscore_name.insert(-1,"_search").to_sym })
 | 
			
		||||
				else
 | 
			
		||||
					client.load(underscore_name.to_sym, { query: query_val, limit: 10 }, { binding: underscore_name.insert(-1,"_search").to_sym })
 | 
			
		||||
				end
 | 
			
		||||
				underscored_name = underscore_name
 | 
			
		||||
				client.load(underscored_name.to_sym, { query: query_val, limit: 10 }, { binding: underscored_name.insert(-1,"_search").to_sym })
 | 
			
		||||
			end
 | 
			
		||||
 | 
			
		||||
			class ::String
 | 
			
		||||
@ -57,6 +58,10 @@ module MusicBrainz
 | 
			
		||||
				hash
 | 
			
		||||
			end
 | 
			
		||||
 | 
			
		||||
			def underscore_name
 | 
			
		||||
				self.name[13..-1].underscore
 | 
			
		||||
			end
 | 
			
		||||
 | 
			
		||||
			# these probably should be private... but I'm not sure how to get it to work in a module...
 | 
			
		||||
			# private_class_method :build_query, :escape_strings
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										20
									
								
								lib/musicbrainz/models/recording.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								lib/musicbrainz/models/recording.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  class Recording < BaseModel
 | 
			
		||||
    field :id, Integer
 | 
			
		||||
    field :mbid, Integer
 | 
			
		||||
    field :title, String
 | 
			
		||||
    field :artist, String
 | 
			
		||||
		field :releases, String
 | 
			
		||||
		field :score, Integer
 | 
			
		||||
 | 
			
		||||
    class << self
 | 
			
		||||
      def find(id)
 | 
			
		||||
				super({ id: id })
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
			def search(track_name, artist_name)
 | 
			
		||||
				super({recording: track_name, artist: artist_name})
 | 
			
		||||
			end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  class Release < BaseModel
 | 
			
		||||
    field :id, String
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  class ReleaseGroup < BaseModel
 | 
			
		||||
    field :id, String
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  class Track < BaseModel
 | 
			
		||||
    field :position, Integer
 | 
			
		||||
@ -12,11 +13,6 @@ module MusicBrainz
 | 
			
		||||
          create_model: :track
 | 
			
		||||
        })
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
			def search(artist_name, track_name)
 | 
			
		||||
				# this model really should be named "recording" I'd rename, but I don't want to break anything
 | 
			
		||||
				super({recording: track_name, artist: artist_name}, "recording")
 | 
			
		||||
			end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
module MusicBrainz
 | 
			
		||||
  VERSION = "0.7.7"
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -1,10 +1,10 @@
 | 
			
		||||
# -*- encoding: utf-8 -*-
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
 | 
			
		||||
require "spec_helper"
 | 
			
		||||
 | 
			
		||||
describe MusicBrainz::Bindings::TrackSearch do
 | 
			
		||||
describe MusicBrainz::Bindings::RecordingSearch do
 | 
			
		||||
  describe '.parse' do
 | 
			
		||||
    it "gets correct Track (really recording) data" do
 | 
			
		||||
    it "gets correct Recording data" do
 | 
			
		||||
			response = '<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ext="http://musicbrainz.org/ns/ext#-2.0"><recording-list offset="0" count="1"><recording id="0b382a13-32f0-4743-9248-ba5536a6115e" ext:score="100"><title>King Fred</title><artist-credit><name-credit><artist id="f52f7a92-d495-4d32-89e7-8b1e5b8541c8"><name>Too Much Joy</name></artist></name-credit></artist-credit><release-list><release id="8442e42b-c40a-4817-89a0-dbe663c94d2d"><title>Green Eggs and Crack</title></release></release-list></recording></recording-list></metadata>'
 | 
			
		||||
      expect(described_class.parse(Nokogiri::XML.parse(response).remove_namespaces!.xpath('/metadata'))).to eq [
 | 
			
		||||
        {
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
# -*- encoding: utf-8 -*-
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
 | 
			
		||||
require "spec_helper"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
# -*- encoding: utf-8 -*-
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
 | 
			
		||||
require "spec_helper"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
# encoding: utf-8
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
 | 
			
		||||
require "ostruct"
 | 
			
		||||
require "spec_helper"
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
# -*- encoding: utf-8 -*-
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
 | 
			
		||||
require "spec_helper"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
# -*- encoding: utf-8 -*-
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
 | 
			
		||||
require "spec_helper"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
# -*- encoding: utf-8 -*-
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
 | 
			
		||||
require "spec_helper"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
# -*- encoding: utf-8 -*-
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
 | 
			
		||||
require "spec_helper"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										32
									
								
								spec/models/recording_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								spec/models/recording_spec.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,32 @@
 | 
			
		||||
# -*- encoding: utf-8 -*-
 | 
			
		||||
 | 
			
		||||
require "spec_helper"
 | 
			
		||||
 | 
			
		||||
describe MusicBrainz::Recording do
 | 
			
		||||
	describe '.find' do
 | 
			
		||||
		it "gets no exception while loading release info" do
 | 
			
		||||
			lambda {
 | 
			
		||||
				MusicBrainz::Recording.find("b3015bab-1540-4d4e-9f30-14872a1525f7")
 | 
			
		||||
			}.should_not raise_error(Exception)
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		it "gets correct instance" do
 | 
			
		||||
			track = MusicBrainz::Recording.find("b3015bab-1540-4d4e-9f30-14872a1525f7")
 | 
			
		||||
			track.should be_an_instance_of(MusicBrainz::Recording)
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		it "gets correct track data" do
 | 
			
		||||
			track = MusicBrainz::Recording.find("b3015bab-1540-4d4e-9f30-14872a1525f7")
 | 
			
		||||
			track.title.should == "Empire"
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
  describe '.search' do
 | 
			
		||||
		it "searches tracks (aka recordings) by artist name and title" do
 | 
			
		||||
			matches = MusicBrainz::Recording.search('Bound for the floor', 'Local H')
 | 
			
		||||
			matches.length.should be > 0
 | 
			
		||||
			matches.first[:title].should == "Bound for the Floor"
 | 
			
		||||
			matches.first[:artist].should == "Local H"
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
# -*- encoding: utf-8 -*-
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
 | 
			
		||||
require "spec_helper"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
# -*- encoding: utf-8 -*-
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
 | 
			
		||||
require "spec_helper"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
# -*- encoding: utf-8 -*-
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
 | 
			
		||||
require "spec_helper"
 | 
			
		||||
 | 
			
		||||
@ -22,13 +22,4 @@ describe MusicBrainz::Track do
 | 
			
		||||
			track.length.should == 233013
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
  describe '.search' do
 | 
			
		||||
		it "searches tracks (aka recordings) by artist name and title" do
 | 
			
		||||
			matches = MusicBrainz::Track.search('Local H', 'Bound for the floor')
 | 
			
		||||
			matches.length.should be > 0
 | 
			
		||||
			matches.first[:title].should == 'Empire'
 | 
			
		||||
			matches.first[:type].should == 'Album'
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
# -*- encoding : utf-8 -*-
 | 
			
		||||
require "rubygems"
 | 
			
		||||
require "bundler/setup"
 | 
			
		||||
require "musicbrainz"
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user