xbmc 0.0.1

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 (4) hide show
  1. data/README.textile +17 -0
  2. data/lib/xbmc.rb +87 -0
  3. data/spec/xbmc_spec.rb +75 -0
  4. metadata +57 -0
data/README.textile ADDED
@@ -0,0 +1,17 @@
1
+ h2. XBMC
2
+
3
+ A simple wrapper for XBMC's "HTTP API":http://www.xbmc.org/wiki/?title=WebServerHTTP-API
4
+
5
+ h3. Installation
6
+
7
+ The xbmc gem is hosted by "gemcutter":http://gemcutter.org
8
+
9
+ gem sources -a http://gemcutter.org
10
+ gem install xbmc
11
+
12
+ h3. Usage
13
+
14
+ rodeo:~ rob$ ruby -rubygems -e "require 'xbmc';x=XBMC.new('xbox');puts x.currently_playing[:artist]"
15
+ Bibio
16
+
17
+ For examples of usage, check out the spec file or an example of "controlling XBMC from iTunes":http://gist.github.com/212375
data/lib/xbmc.rb ADDED
@@ -0,0 +1,87 @@
1
+ require 'rubygems'
2
+ require 'open-uri'
3
+ require 'nokogiri'
4
+ require 'cgi'
5
+
6
+ def returning(value)
7
+ yield(value)
8
+ value
9
+ end
10
+
11
+ class XBMC
12
+ # Creates a new instance using the hostname or IP address, port, and
13
+ def initialize(host,port = 80, playlist = 0)
14
+ @host = host
15
+ @port = port
16
+ @paused = false
17
+ @playlist = playlist
18
+
19
+ set_playlist
20
+ end
21
+
22
+ # returns a hash with details on the currently_playing track
23
+ # xbox.currently_playing[:Title] #=> "La Vida Loca"
24
+ def currently_playing
25
+ returning(Hash.new) do |result|
26
+ send_command('getCurrentlyPlaying').search("//li").each do |item|
27
+ key,*value = item.inner_html.split(':')
28
+ result[key.downcase.to_sym] = value.join(':').chomp
29
+ end
30
+ end
31
+ end
32
+
33
+ # Whether or not a track is in progress
34
+ def playing?
35
+ currently_playing[:filename] == "[Nothing Playing]" ? false : (true && !paused?)
36
+ end
37
+
38
+ # Whether or not xbmc is paused
39
+ def paused?
40
+ @paused
41
+ end
42
+
43
+ # An array of the currently loaded tracks
44
+ def playlist
45
+ send_command('getPlaylistContents',0).search("//li").map{|i| i.inner_html.chomp}.reject{|i| i == "[Empty]"}
46
+ end
47
+
48
+ # Add a file to the queue. Specify the path to the file as xbmc would access it.
49
+ def queue(song)
50
+ send_command('addToPlaylist',song,0)
51
+ end
52
+
53
+ # Play the next track
54
+ def play_next
55
+ send_command('PlayNext')
56
+ end
57
+
58
+ # Pause or unpause the player.
59
+ # This is a simple toggle, it doesn't check current state.
60
+ def pause
61
+ send_command('Pause')
62
+ @paused = !@paused
63
+ end
64
+ alias :unpause :pause
65
+
66
+ def clear_playlist
67
+ send_command('ClearPlaylist',0)
68
+ end
69
+
70
+ # 0 = Music
71
+ # 1 = Video
72
+ def set_playlist(type=@playlist)
73
+ send_command('SetCurrentPlaylist',type)
74
+ end
75
+
76
+ private
77
+
78
+ def send_command(command,*args)
79
+ args = '(' + args.join(';') + ')'
80
+ Nokogiri::HTML(open(url_for(command + args)))
81
+ end
82
+
83
+ def url_for(command)
84
+ "http://#{@host}:#{@port}/xbmcCmds/xbmcHttp?command=" + CGI.escape(command)
85
+ end
86
+
87
+ end
data/spec/xbmc_spec.rb ADDED
@@ -0,0 +1,75 @@
1
+ require 'lib/xbmc'
2
+
3
+ describe XBMC do
4
+
5
+ before do
6
+ @xbox = XBMC.new('myxbox')
7
+ end
8
+
9
+ describe "when a song is playing" do
10
+
11
+ describe "and the user needs detials about what's currently playing" do
12
+ before do
13
+ @xbox.should_receive(:open).at_least(:once).and_return(fixture('currently_playing'))
14
+ end
15
+
16
+ it "should detect that a song is playing" do
17
+ @xbox.playing?.should be_true
18
+ end
19
+
20
+ it "should get currently playing song" do
21
+ @xbox.currently_playing[:title].should == 'Ambivalence Avenue'
22
+ end
23
+
24
+ it "should know the path to the current song" do
25
+ @xbox.currently_playing[:filename].should == "smb://luna/music/Bibio/Ambivalence Avenue/01 Ambivalence Avenue.mp3"
26
+ end
27
+
28
+ end
29
+
30
+ describe "and the users asks about the playlist" do
31
+
32
+ before do
33
+ @xbox.should_receive(:open).at_least(:once).and_return(fixture('current_playlist'))
34
+ end
35
+
36
+ it "should return the playlist" do
37
+ @xbox.playlist.size.should == 2
38
+ @xbox.playlist.first.should == "smb://luna/music/Bibio/Ambivalence Avenue/01 Ambivalence Avenue.mp3"
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+
45
+ describe "when nothing is playing" do
46
+
47
+ it "should detect that nothing is playing" do
48
+ @xbox.should_receive(:open).at_least(:once).and_return(fixture('nothing_playing'))
49
+ @xbox.playing?.should be_false
50
+ end
51
+
52
+ describe "and the users asks about the playlist" do
53
+
54
+ it "should return an empty list" do
55
+ @xbox.should_receive(:open).at_least(:once).and_return(fixture('empty_playlist'))
56
+ @xbox.playlist.size.should == 0
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+ describe "when queueing a song" do
64
+ it "should send a request with the file path" do
65
+ filepath = "smb://path/to/file.mp3"
66
+ @xbox.should_receive(:open).with("http://myxbox:80/xbmcCmds/xbmcHttp?command=addToPlaylist" + CGI.escape("(#{filepath};0)"))
67
+ @xbox.queue(filepath)
68
+ end
69
+ end
70
+
71
+ end
72
+
73
+ def fixture(name)
74
+ IO.readlines("spec/fixtures/#{name}.xml").to_s
75
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xbmc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rob Lingle
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-17 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: xbmc provides a super simple wrapper for controlling your Xbox Media Center.
17
+ email: rob.lingle@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.textile
26
+ - lib/xbmc.rb
27
+ - spec/xbmc_spec.rb
28
+ has_rdoc: true
29
+ homepage: http://github.com/roblingle/xbmc
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.3.5
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: A simple wrapper for XBMC's HTTP API
56
+ test_files: []
57
+