1
0
Fork 0

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:
Thomas Wolfe 2013-11-05 20:15:09 -06:00 committed by Diego d'Ursel
parent 813b6f0383
commit 2d4c1c42fd
42 changed files with 151 additions and 42 deletions

View File

@ -1,2 +1,6 @@
source 'https://rubygems.org' source 'https://rubygems.org'
gemspec gemspec
group :test do
gem "debugger"
end

View File

@ -121,11 +121,10 @@ MusicBrainz::Release.find(id)
} }
``` ```
MusicBrainz::Track MusicBrainz::Track (depreciated, now called Recording)
```ruby ```ruby
# Class Methods # Class Methods
MusicBrainz::Track.find(id) MusicBrainz::Track.find(id)
MusicBrainz::ReleaseGroup.search(artist_name, track_name)
# Fields # 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 ### Testing
``` ```
bundle exec rspec bundle exec rspec

View File

@ -1,2 +1,3 @@
# -*- encoding : utf-8 -*-
require "musicbrainz" require "musicbrainz"
MB = MusicBrainz MB = MusicBrainz

View File

@ -1,3 +1,7 @@
# -*- encoding : utf-8 -*-
#!/bin/env ruby
# encoding: utf-8
require "digest/sha1" require "digest/sha1"
require "fileutils" require "fileutils"
require "date" require "date"
@ -20,6 +24,7 @@ require "musicbrainz/models/artist"
require "musicbrainz/models/release_group" require "musicbrainz/models/release_group"
require "musicbrainz/models/release" require "musicbrainz/models/release"
require "musicbrainz/models/track" require "musicbrainz/models/track"
require "musicbrainz/models/recording"
require "musicbrainz/bindings/artist" require "musicbrainz/bindings/artist"
require "musicbrainz/bindings/artist_search" require "musicbrainz/bindings/artist_search"
@ -32,7 +37,8 @@ require "musicbrainz/bindings/release_group_releases"
require "musicbrainz/bindings/release" require "musicbrainz/bindings/release"
require "musicbrainz/bindings/release_tracks" require "musicbrainz/bindings/release_tracks"
require "musicbrainz/bindings/track" require "musicbrainz/bindings/track"
require "musicbrainz/bindings/track_search" require "musicbrainz/bindings/recording"
require "musicbrainz/bindings/recording_search"
module MusicBrainz module MusicBrainz
GH_PAGE_URL = "http://git.io/brainz" GH_PAGE_URL = "http://git.io/brainz"

View File

@ -1,4 +1,4 @@
# encoding: UTF-8 # -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
module Bindings module Bindings
module Artist module Artist

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
module Bindings module Bindings
module ArtistReleaseGroups module ArtistReleaseGroups

View File

@ -1,4 +1,4 @@
# encoding: UTF-8 # -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
module Bindings module Bindings
module ArtistSearch module ArtistSearch

View 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

View File

@ -1,7 +1,7 @@
# encoding: UTF-8 # -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
module Bindings module Bindings
module TrackSearch module RecordingSearch
def parse(xml) def parse(xml)
xml.xpath('./recording-list/recording').map do |xml| xml.xpath('./recording-list/recording').map do |xml|
{ {

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
module Bindings module Bindings
module Release module Release

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
module Bindings module Bindings
module ReleaseGroup module ReleaseGroup

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
module Bindings module Bindings
module ReleaseGroupReleases module ReleaseGroupReleases

View File

@ -1,4 +1,4 @@
# encoding: UTF-8 # -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
module Bindings module Bindings
module ReleaseGroupSearch module ReleaseGroupSearch

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
module Bindings module Bindings
module ReleaseTracks module ReleaseTracks

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
module Bindings module Bindings
module Track module Track

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
class Client class Client
include ClientModules::TransparentProxy include ClientModules::TransparentProxy

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
module ClientModules module ClientModules
module CachingProxy module CachingProxy

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
module ClientModules module ClientModules
module FailsafeProxy module FailsafeProxy

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
module ClientModules module ClientModules
module TransparentProxy module TransparentProxy

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
class Configuration class Configuration
attr_accessor :app_name, :app_version, :contact, attr_accessor :app_name, :app_version, :contact,

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
module Deprecated module Deprecated
module ProxyConfig module ProxyConfig

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
class Middleware < Faraday::Middleware class Middleware < Faraday::Middleware
def call(env) def call(env)

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
class Artist < BaseModel class Artist < BaseModel
field :id, String field :id, String

View File

@ -24,15 +24,16 @@ module MusicBrainz
MusicBrainz.client MusicBrainz.client
end 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) hash = escape_strings(hash)
query_val = build_query(hash) query_val = build_query(hash)
underscore_name = self.name[13..-1].underscore underscored_name = underscore_name
if resource # only needed since "track" is really a "recording", ugly client.load(underscored_name.to_sym, { query: query_val, limit: 10 }, { binding: underscored_name.insert(-1,"_search").to_sym })
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
end end
class ::String class ::String
@ -57,6 +58,10 @@ module MusicBrainz
hash hash
end 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... # 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 # private_class_method :build_query, :escape_strings
end end

View 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

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
class Release < BaseModel class Release < BaseModel
field :id, String field :id, String

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
class ReleaseGroup < BaseModel class ReleaseGroup < BaseModel
field :id, String field :id, String

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
class Track < BaseModel class Track < BaseModel
field :position, Integer field :position, Integer
@ -12,11 +13,6 @@ module MusicBrainz
create_model: :track create_model: :track
}) })
end 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 end
end end

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz module MusicBrainz
VERSION = "0.7.7" VERSION = "0.7.7"
end end

View File

@ -1,10 +1,10 @@
# -*- encoding: utf-8 -*- # -*- encoding : utf-8 -*-
require "spec_helper" require "spec_helper"
describe MusicBrainz::Bindings::TrackSearch do describe MusicBrainz::Bindings::RecordingSearch do
describe '.parse' 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>' 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 [ expect(described_class.parse(Nokogiri::XML.parse(response).remove_namespaces!.xpath('/metadata'))).to eq [
{ {

View File

@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*- # -*- encoding : utf-8 -*-
require "spec_helper" require "spec_helper"

View File

@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*- # -*- encoding : utf-8 -*-
require "spec_helper" require "spec_helper"

View File

@ -1,4 +1,4 @@
# encoding: utf-8 # -*- encoding : utf-8 -*-
require "ostruct" require "ostruct"
require "spec_helper" require "spec_helper"

View File

@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*- # -*- encoding : utf-8 -*-
require "spec_helper" require "spec_helper"

View File

@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*- # -*- encoding : utf-8 -*-
require "spec_helper" require "spec_helper"

View File

@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*- # -*- encoding : utf-8 -*-
require "spec_helper" require "spec_helper"

View File

@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*- # -*- encoding : utf-8 -*-
require "spec_helper" require "spec_helper"

View 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

View File

@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*- # -*- encoding : utf-8 -*-
require "spec_helper" require "spec_helper"

View File

@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*- # -*- encoding : utf-8 -*-
require "spec_helper" require "spec_helper"

View File

@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*- # -*- encoding : utf-8 -*-
require "spec_helper" require "spec_helper"
@ -22,13 +22,4 @@ describe MusicBrainz::Track do
track.length.should == 233013 track.length.should == 233013
end end
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 end

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
require "rubygems" require "rubygems"
require "bundler/setup" require "bundler/setup"
require "musicbrainz" require "musicbrainz"