music-query 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/music-query.rb +136 -0
  2. metadata +65 -0
@@ -0,0 +1,136 @@
1
+ ##
2
+ # Module that contains classes that perform queries to pull song information up
3
+ # from various platforms.
4
+
5
+ module MusicQuery
6
+ require 'open-uri'
7
+ require 'json'
8
+
9
+ ##
10
+ # This is a base class to handle query requests
11
+
12
+ class MusicQuery
13
+
14
+ ##
15
+ # Creates a new base query class object.
16
+ #
17
+ # Optional arguments can be passed in to parse JSON and URI
18
+ # information, if not the json and open-uri gems are used in its place
19
+
20
+ def initialize(json_parser = JSON, uri_parser = URI)
21
+ @json_parser = json_parser
22
+ @uri_parser = uri_parser
23
+ end
24
+
25
+
26
+ ##
27
+ # Returns name of current calling class.
28
+
29
+ def class_name
30
+ return self.class.name.split('::').last
31
+ end
32
+
33
+ end
34
+
35
+
36
+ ##
37
+ # Class that handles track query requests from Grooveshark
38
+
39
+ class Grooveshark < MusicQuery
40
+
41
+ ##
42
+ # Creates a new base query object to handle query request to
43
+ # Grooveshark.
44
+
45
+ def initialize(api_key, json_parser = JSON, uri_parser = URI)
46
+ @api_key = api_key
47
+ super(json_parser, uri_parser)
48
+ end
49
+
50
+ ##
51
+ # Queries the Grooveshark API
52
+
53
+ def query(query_content)
54
+ query_base_uri = "http://tinysong.com/b/"
55
+ query_content.gsub!(" ", "+")
56
+
57
+ query_full = "#{query_base_uri}#{query_content}?format=json&key=#{@api_key}"
58
+ track_response_json = @uri_parser.parse("#{query_full}").read
59
+ track_hash = @json_parser.parse(track_response_json)
60
+ return Song.new(self, track_hash)
61
+ end
62
+
63
+ end
64
+
65
+ ##
66
+ # Class that handles track query requests from Spotify
67
+
68
+ class Spotify < MusicQuery
69
+
70
+ ##
71
+ # Creates a new base query object to handle query request to
72
+ # Spotify.
73
+
74
+ def query(query_content)
75
+ return_uri = ""
76
+ query_content.gsub!(" ", "+")
77
+
78
+ track_response_json = @uri_parser.parse("http://ws.spotify.com/search/1/track.json?q=#{query_content}").read
79
+ track_hash = @json_parser.parse(track_response_json)
80
+
81
+ # Walk the hash and find the uri for the matching song
82
+ track_hash["tracks"].each_with_index do |track, i|
83
+ return Song.new(self, track)
84
+ end
85
+ end
86
+
87
+ end
88
+
89
+
90
+ ##
91
+ # Class that represents Song objects for querying
92
+ # music services.
93
+
94
+ class Song
95
+
96
+ attr_accessor :artist_name, :song_title, :id
97
+
98
+ ##
99
+ # Creates a new song object containing the artist name,
100
+ # song title and unique identifer for the track
101
+
102
+ def initialize(music_service, json_text)
103
+ parse_song(music_service, json_text)
104
+ end
105
+
106
+ private
107
+
108
+ ##
109
+ # Parses a json object into a song object
110
+
111
+ def parse_song(music_service, json_text)
112
+ if music_service == ""
113
+ return ""
114
+ end
115
+
116
+ if music_service.class_name == "Grooveshark"
117
+ parse_grooveshark(json_text)
118
+ elsif music_service.class_name == "Spotify"
119
+ parse_spotify(json_text)
120
+ end
121
+ end
122
+
123
+ def parse_grooveshark(return_text)
124
+ @artist_name = return_text["ArtistName"].to_s
125
+ @song_title = return_text["SongName"].to_s
126
+ @id = return_text["Url"].to_s
127
+ end
128
+
129
+ def parse_spotify(return_text)
130
+ @artist_name = return_text["artists"][0]["name"].to_s
131
+ @song_title = return_text["name"].to_s
132
+ @id = return_text["href"].to_s
133
+ end
134
+ end
135
+ end
136
+
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: music-query
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Oshin Karamian
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-07-21 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: A gem that wraps around the Grooveshark and Spotify APIs to help make simple database queries
22
+ email: okaramia@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/music-query.rb
31
+ homepage: http://rubygems.org/gems/music-query
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.5
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Grooveshark/Spotify music querying gem
64
+ test_files: []
65
+