Musix 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/musix.rb +92 -0
  2. metadata +108 -0
data/lib/musix.rb ADDED
@@ -0,0 +1,92 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+
4
+ require 'httparty'
5
+ require 'nokogiri'
6
+ require 'Plist'
7
+ require 'metaid'
8
+ require 'cgi'
9
+
10
+ # = What is Musix?
11
+ #
12
+ # Musix was written to provide a simple interface for searching popular
13
+ # music services such as Spotify, Grooveshark, ITunes and possibly more
14
+ # in the future.
15
+ module Musix
16
+ # = A Spotify metadata API wrapper
17
+ #
18
+ #
19
+ # == Examples
20
+ # Service::Spotify::Search.track('Fix You')
21
+ # Service::Spotify::lookup(Service::Spotify::Search.track('Fix You')["track"].first["href"])
22
+ module Spotify
23
+ include HTTParty
24
+ format :xml
25
+ base_uri 'http://ws.spotify.com/'
26
+
27
+ # = Spotify metadata search API
28
+ # Spotify::Search provides three methods for searching the {Spotify metadata API}[http://developer.spotify.com/en/metadata-api/search/].
29
+ class Search
30
+
31
+ # Searches Spotify for a track.
32
+ def track(query, page = 1)
33
+ search("track", query, page)
34
+ end
35
+
36
+ # Searches Spotify for an album.
37
+ def album(query, page = 1)
38
+ search("album", query, page)
39
+ end
40
+
41
+ # Searches Spotify for an artist.
42
+ def artist(query, page = 1)
43
+ search("artist", query, page)
44
+ end
45
+
46
+ private
47
+ def search(type, query, page)
48
+ Spotify::get("/search/1/#{method}", :query => { :q => query, :page => [args.first.to_i, 1].max }).fetch("#{method}s")
49
+ end
50
+ end
51
+
52
+ # Looks up information about a Spotify URI with the +detail+ level of detail (max 2).
53
+ #
54
+ # Spotify URIs looks like the following:
55
+ # - spotify:track:id
56
+ # - spotify:artist:id
57
+ # - spotify:album:id
58
+ def self.lookup(uri, detail = 0)
59
+ type = uri.match('spotify:([^:]+)')[1]
60
+ get '/lookup/1/', :query => { :uri => uri, :extras => [type, "detail"].slice(0, detail).join }
61
+ end
62
+ end
63
+
64
+ # = Grooveshark API
65
+ # Searches Grooveshark using the {Tinysong API}[http://tinysong.com/api]
66
+ class Grooveshark
67
+ include HTTParty
68
+ format :json
69
+ default_params :format => 'json'
70
+
71
+ # Searches Grooveshark (tinysong), returns a maximum of +limit+ results
72
+ def self.search(uri, limit = 3)
73
+ get "http://tinysong.com/s/#{CGI::escape uri}", :query => { :limit => limit }
74
+ end
75
+ end
76
+
77
+ # = iTunes API
78
+ # This code would not have been possible if I hadn’t found {itms-lib}[http://itms-lib.rubyforge.org/].
79
+ # It made my quest for an iTunes search API much shorter.
80
+ class ITunes
81
+ include HTTParty
82
+ headers 'User-Agent' => 'iTunes/9.1'
83
+ format :plain
84
+
85
+ # Searches iTunes for +term+.
86
+ def self.search(term)
87
+ result = get 'http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZSearch.woa/wa/search?submit=edit&term=%s' % CGI::escape(term)
88
+ itms = Nokogiri::XML result
89
+ Plist::parse_xml(itms.css('TrackList plist').to_s)["items"]
90
+ end
91
+ end
92
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Musix
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Kim Burgestrand
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-04-25 00:00:00 +02:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: httparty
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ segments:
27
+ - 0
28
+ version: "0"
29
+ type: :runtime
30
+ version_requirements: *id001
31
+ - !ruby/object:Gem::Dependency
32
+ name: nokogiri
33
+ prerelease: false
34
+ requirement: &id002 !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ segments:
39
+ - 0
40
+ version: "0"
41
+ type: :runtime
42
+ version_requirements: *id002
43
+ - !ruby/object:Gem::Dependency
44
+ name: plist
45
+ prerelease: false
46
+ requirement: &id003 !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ type: :runtime
54
+ version_requirements: *id003
55
+ - !ruby/object:Gem::Dependency
56
+ name: metaid
57
+ prerelease: false
58
+ requirement: &id004 !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ type: :runtime
66
+ version_requirements: *id004
67
+ description:
68
+ email: kim@burgestrand.se
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ extra_rdoc_files: []
74
+
75
+ files:
76
+ - lib/musix.rb
77
+ has_rdoc: true
78
+ homepage: http://github.com/Burgestrand/Musix
79
+ licenses: []
80
+
81
+ post_install_message:
82
+ rdoc_options: []
83
+
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project:
103
+ rubygems_version: 1.3.6
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: A simplified API for searching iTunes, Grooveshark and Spotify
107
+ test_files: []
108
+