subsonic-rb 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,10 @@
1
+ # Subsonic
2
+
3
+ client = Subsonic::Client.new(url, username, password)
4
+
5
+
6
+ ## Now Playing
7
+
8
+ client.now_playing
9
+
10
+ returns a string in "artist - track" or an array if more people are streaming
@@ -0,0 +1,13 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/subsonic')
2
+
3
+ %w[
4
+ httparty
5
+ json
6
+ client
7
+ ].each do |file|
8
+ require file
9
+ end
10
+
11
+ module Subsonic
12
+ autoload :Client, "./subsonic-rb/client"
13
+ end
@@ -0,0 +1,80 @@
1
+ module Subsonic
2
+ class Client
3
+ include ::HTTParty
4
+
5
+ API_VERSION = {
6
+ '3.8' => '1.0.0',
7
+ '3.9' => '1.1.1',
8
+ '4.0' => '1.2.0',
9
+ '4.1' => '1.3.0',
10
+ '4.2' => '1.4.0',
11
+ '4.3.1' => '1.5.0'
12
+ }
13
+
14
+ attr_accessor :url, :version, :api_version, :user
15
+
16
+ def initialize(url, username, password, options={})
17
+ pass_prefix = options[:enc] ? "enc:" : ""
18
+ version = @version = options[:version] || API_VERSION.values.last
19
+ @api_version = API_VERSION[@version] || version
20
+ format = options[:format] || "json"
21
+
22
+ Struct.new("User", :username, :password)
23
+ @user = Struct::User.new(username, "#{pass_prefix}#{password}")
24
+ username, password = @user.username, @user.password
25
+
26
+ self.class.class_eval do
27
+ base_uri url
28
+ default_params :u => username, :p => password,
29
+ :v => version, :f => format, :c => "subsonic-rb.gem"
30
+ end
31
+ end
32
+
33
+ def now_playing
34
+ response = self.class.get('/getNowPlaying.view')
35
+ if response.code == 200
36
+ now_playing = response.parsed_response['subsonic-response']['nowPlaying']['entry']
37
+ if now_playing.is_a? Array
38
+ now_playing.map {|entry| "#{entry['artist']} - #{entry['title']}"}
39
+ else
40
+ "#{now_playing['artist']} - #{now_playing['title']}"
41
+ end
42
+ end
43
+ end
44
+
45
+ def say(message)
46
+ response = self.class.post('/addChatMessage.view', :query => {:message => message})
47
+ response.code == 200 ? message : false
48
+ end
49
+
50
+ def messages
51
+ response = self.class.get('/getChatMessages.view')
52
+ if response.code == 200
53
+ chat_messages = response.parsed_response['subsonic-response']['chatMessages']['chatMessage']
54
+ chat_messages.map do |msg|
55
+ time = Time.at(msg['time'] / 1000)
56
+ "[#{time.strftime("%b-%e")}] #{msg['username']}: #{msg['message']}"
57
+ end
58
+ end
59
+ end
60
+
61
+ def random_songs
62
+ response = self.class.get('/getRandomSongs')
63
+ if response.code == 200
64
+ songs = response['subsonic-response']['randomSongs']['song']
65
+ songs.map do |song|
66
+ {:song => "#{song['artist']} - #{song['title']}",
67
+ :id => song['id']}
68
+ end
69
+ end
70
+ end
71
+
72
+ def add_song(*ids)
73
+ count = ids.length
74
+ ids = ids.join(',').gsub(/\s/, '')
75
+ response = self.class.post('/jukeboxControl.view', :query => {:action => 'add', :id => ids})
76
+ response.code == 200 ? "#{count} songs added" : false
77
+ end
78
+
79
+ end
80
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: subsonic-rb
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: "0.1"
6
+ platform: ruby
7
+ authors:
8
+ - Joseph Hsu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-24 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Interact with a subsonic music streaming server
18
+ email:
19
+ - jhsu@josephhsu.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - README.markdown
28
+ - lib/subsonic-rb/client.rb
29
+ - lib/subsonic-rb.rb
30
+ has_rdoc: true
31
+ homepage: http://github.com/jhsu/subsonic-rb
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
+ version: 1.8.7
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 1.3.6
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.6.1
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Subsonic music streaming api client
58
+ test_files: []
59
+