Merge pull request #33 from rightsup/fix-recording
Adds Recording::find & moves Track::search into Recording::search
This commit is contained in:
commit
c2979690e0
4
Gemfile
4
Gemfile
|
@ -1,2 +1,6 @@
|
||||||
source 'https://rubygems.org'
|
source 'https://rubygems.org'
|
||||||
gemspec
|
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
|
```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
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
require "musicbrainz"
|
require "musicbrainz"
|
||||||
MB = MusicBrainz
|
MB = MusicBrainz
|
||||||
|
|
|
@ -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"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# encoding: UTF-8
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
module Bindings
|
module Bindings
|
||||||
module Artist
|
module Artist
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
module Bindings
|
module Bindings
|
||||||
module ArtistReleaseGroups
|
module ArtistReleaseGroups
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# encoding: UTF-8
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
module Bindings
|
module Bindings
|
||||||
module ArtistSearch
|
module ArtistSearch
|
||||||
|
|
|
@ -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 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|
|
||||||
{
|
{
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
module Bindings
|
module Bindings
|
||||||
module Release
|
module Release
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
module Bindings
|
module Bindings
|
||||||
module ReleaseGroup
|
module ReleaseGroup
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
module Bindings
|
module Bindings
|
||||||
module ReleaseGroupReleases
|
module ReleaseGroupReleases
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# encoding: UTF-8
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
module Bindings
|
module Bindings
|
||||||
module ReleaseGroupSearch
|
module ReleaseGroupSearch
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
module Bindings
|
module Bindings
|
||||||
module ReleaseTracks
|
module ReleaseTracks
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
module Bindings
|
module Bindings
|
||||||
module Track
|
module Track
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
class Client
|
class Client
|
||||||
include ClientModules::TransparentProxy
|
include ClientModules::TransparentProxy
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
module ClientModules
|
module ClientModules
|
||||||
module CachingProxy
|
module CachingProxy
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
module ClientModules
|
module ClientModules
|
||||||
module FailsafeProxy
|
module FailsafeProxy
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
module ClientModules
|
module ClientModules
|
||||||
module TransparentProxy
|
module TransparentProxy
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
module Deprecated
|
module Deprecated
|
||||||
module ProxyConfig
|
module ProxyConfig
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
class Artist < BaseModel
|
class Artist < BaseModel
|
||||||
field :id, String
|
field :id, String
|
||||||
|
|
|
@ -24,11 +24,16 @@ module MusicBrainz
|
||||||
MusicBrainz.client
|
MusicBrainz.client
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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)
|
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
|
||||||
client.load(underscore_name.to_sym, { query: query_val, limit: 10 }, { binding: underscore_name.insert(-1,"_search").to_sym })
|
client.load(underscored_name.to_sym, { query: query_val, limit: 10 }, { binding: underscored_name.insert(-1,"_search").to_sym })
|
||||||
end
|
end
|
||||||
|
|
||||||
class ::String
|
class ::String
|
||||||
|
@ -53,6 +58,11 @@ module MusicBrainz
|
||||||
hash
|
hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def underscore_name
|
||||||
|
# self.name[13..-1] => removes MusicBrainz::
|
||||||
|
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
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
module MusicBrainz
|
||||||
|
class Recording < BaseModel
|
||||||
|
field :id, String
|
||||||
|
field :mbid, String
|
||||||
|
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
|
module MusicBrainz
|
||||||
class Release < BaseModel
|
class Release < BaseModel
|
||||||
field :id, String
|
field :id, String
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
class ReleaseGroup < BaseModel
|
class ReleaseGroup < BaseModel
|
||||||
field :id, String
|
field :id, String
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
class Track < BaseModel
|
class Track < BaseModel
|
||||||
field :position, Integer
|
field :position, Integer
|
||||||
|
@ -12,10 +13,6 @@ module MusicBrainz
|
||||||
create_model: :track
|
create_model: :track
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
def search(artist_name, track_name)
|
|
||||||
super({artist: artist_name, recording: track_name})
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
VERSION = "0.7.7"
|
VERSION = "0.7.7"
|
||||||
end
|
end
|
||||||
|
|
|
@ -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 [
|
||||||
{
|
{
|
|
@ -1,4 +1,4 @@
|
||||||
# -*- encoding: utf-8 -*-
|
# -*- encoding : utf-8 -*-
|
||||||
|
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# -*- encoding: utf-8 -*-
|
# -*- encoding : utf-8 -*-
|
||||||
|
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# encoding: utf-8
|
# -*- encoding : utf-8 -*-
|
||||||
|
|
||||||
require "ostruct"
|
require "ostruct"
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# -*- encoding: utf-8 -*-
|
# -*- encoding : utf-8 -*-
|
||||||
|
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# -*- encoding: utf-8 -*-
|
# -*- encoding : utf-8 -*-
|
||||||
|
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# -*- encoding: utf-8 -*-
|
# -*- encoding : utf-8 -*-
|
||||||
|
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# -*- encoding: utf-8 -*-
|
# -*- encoding : utf-8 -*-
|
||||||
|
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
expect {
|
||||||
|
MusicBrainz::Recording.find("b3015bab-1540-4d4e-9f30-14872a1525f7")
|
||||||
|
}.to_not raise_error(Exception)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "gets correct instance" do
|
||||||
|
track = MusicBrainz::Recording.find("b3015bab-1540-4d4e-9f30-14872a1525f7")
|
||||||
|
expect(track).to be_an_instance_of(MusicBrainz::Recording)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "gets correct track data" do
|
||||||
|
track = MusicBrainz::Recording.find("b3015bab-1540-4d4e-9f30-14872a1525f7")
|
||||||
|
expect(track.title).to eq "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')
|
||||||
|
expect(matches.length).to be > 0
|
||||||
|
expect(matches.first[:title]).to eq "Bound for the Floor"
|
||||||
|
expect(matches.first[:artist]).to eq "Local H"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,4 +1,4 @@
|
||||||
# -*- encoding: utf-8 -*-
|
# -*- encoding : utf-8 -*-
|
||||||
|
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
|
||||||
|
@ -61,7 +61,6 @@ describe MusicBrainz::ReleaseGroup do
|
||||||
allow_any_instance_of(MusicBrainz::Client).to receive(:get_contents)
|
allow_any_instance_of(MusicBrainz::Client).to receive(:get_contents)
|
||||||
.with('http://musicbrainz.org/ws/2/release-group/6f33e0f0-cde2-38f9-9aee-2c60af8d1a61?inc=url-rels')
|
.with('http://musicbrainz.org/ws/2/release-group/6f33e0f0-cde2-38f9-9aee-2c60af8d1a61?inc=url-rels')
|
||||||
.and_return({ status: 200, body: response})
|
.and_return({ status: 200, body: response})
|
||||||
|
|
||||||
release_group = MusicBrainz::ReleaseGroup.find_by_artist_and_title('Kasabian', 'Empire')
|
release_group = MusicBrainz::ReleaseGroup.find_by_artist_and_title('Kasabian', 'Empire')
|
||||||
expect(release_group.id).to eq '6f33e0f0-cde2-38f9-9aee-2c60af8d1a61'
|
expect(release_group.id).to eq '6f33e0f0-cde2-38f9-9aee-2c60af8d1a61'
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# -*- encoding: utf-8 -*-
|
# -*- encoding : utf-8 -*-
|
||||||
|
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
# -*- encoding: utf-8 -*-
|
# -*- encoding : utf-8 -*-
|
||||||
|
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
|
||||||
describe MusicBrainz::Track do
|
describe MusicBrainz::Track do
|
||||||
|
describe '.find' do
|
||||||
it "gets no exception while loading release info" do
|
it "gets no exception while loading release info" do
|
||||||
expect {
|
expect {
|
||||||
MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7")
|
MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7")
|
||||||
|
@ -20,4 +21,5 @@ describe MusicBrainz::Track do
|
||||||
expect(track.title).to eq "Empire"
|
expect(track.title).to eq "Empire"
|
||||||
expect(track.length).to eq 233013
|
expect(track.length).to eq 233013
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- encoding : utf-8 -*-
|
||||||
require "rubygems"
|
require "rubygems"
|
||||||
require "bundler/setup"
|
require "bundler/setup"
|
||||||
require "musicbrainz"
|
require "musicbrainz"
|
||||||
|
|
Loading…
Reference in New Issue