anyplayer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ *.bundle
4
+
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Gem dependencies in anyplayer.gemspec
4
+ gemspec
5
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/Readme.markdown ADDED
@@ -0,0 +1,12 @@
1
+ Anyplayer
2
+ =========
3
+
4
+ Ruby library to interact with the running music player (iTunes, Rythmbox, MPD, XMMS).
5
+
6
+ require 'anyplayer'
7
+ player = Anyplayer::launched
8
+ player.name # => Rythmbox
9
+ player.track # => "Frontier Psychiatrist"
10
+ player.next
11
+ player.track # => "Ready For The Floor"
12
+
data/anyplayer.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "anyplayer/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "anyplayer"
7
+ s.version = Anyplayer::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Sunny Ripert"]
10
+ s.email = ["sunny@sunfox.org"]
11
+ s.homepage = "http://github.com/sunny/anyplayer"
12
+ s.summary = %q{Interact with the running music player}
13
+ s.description = %q{Play/pause/skip songs in iTunes, Rythmbox, MPD, XMMS}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ end
data/lib/anyplayer.rb ADDED
@@ -0,0 +1,24 @@
1
+ require "timeout"
2
+ require "anyplayer/player"
3
+
4
+ module Anyplayer
5
+ PLAYERS = :itunes, :rhythmbox, :ituneswindows, :mpd, :xmms2, :amarok
6
+ for player in PLAYERS
7
+ require "anyplayer/players/#{player}"
8
+ end
9
+
10
+ # Return the first music player that's launched
11
+ def self::launched(verbose = false)
12
+ PLAYERS.each { |player|
13
+ player = const_get(player.to_s.capitalize).new
14
+ puts "Trying #{player.name}..." if verbose
15
+
16
+ begin
17
+ Timeout::timeout(5) { return player if player.launched? }
18
+ rescue Timeout::Error
19
+ puts "Timed out" if verbose
20
+ end
21
+ }
22
+ nil
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ module Anyplayer
2
+ class Player
3
+
4
+ # Guess the player name
5
+ def name
6
+ self.class.to_s.gsub(/^.*::/, '')
7
+ end
8
+
9
+ # Methods to override
10
+ def launched?; false; end
11
+ def playpause; end
12
+ def next; end
13
+ def prev; end
14
+ def voldown; end
15
+ def volup; end
16
+ def volume; end
17
+ def track; end
18
+ def artist; end
19
+ def album; end
20
+
21
+ end
22
+ end
23
+
@@ -0,0 +1,53 @@
1
+ module Anyplayer
2
+ class Amarok < Player
3
+ def playpause
4
+ tell_to 'PlayPause'
5
+ end
6
+
7
+ def prev
8
+ tell_to 'Prev'
9
+ end
10
+
11
+ def next
12
+ tell_to 'Next'
13
+ end
14
+
15
+ def voldown
16
+ tell_to 'VolumeDown 5'
17
+ end
18
+
19
+ def volup
20
+ tell_to 'VolumeUp 5'
21
+ end
22
+
23
+ def volume
24
+ tell_to 'VolumeGet'
25
+ end
26
+
27
+ def track
28
+ get_metadata('title')
29
+ end
30
+
31
+ def artist
32
+ get_metadata('artist')
33
+ end
34
+
35
+ def album
36
+ get_metadata('album')
37
+ end
38
+
39
+ def launched?
40
+ not %x(qdbus org.kde.amarok 2>&1).match(/does not exist/)
41
+ end
42
+
43
+ private
44
+ def tell_to(command)
45
+ %x(qdbus org.kde.amarok /Player org.freedesktop.MediaPlayer.#{command}).rstrip
46
+ end
47
+
48
+ def get_metadata(name)
49
+ tell_to('GetMetadata').match(/#{name}: (\S.*)/)[1] rescue nil
50
+ end
51
+ end
52
+ end
53
+
@@ -0,0 +1,55 @@
1
+ module Anyplayer
2
+ class Itunes < Player
3
+ def playpause
4
+ tell_to 'playpause'
5
+ end
6
+
7
+ def prev
8
+ tell_to 'previous track'
9
+ end
10
+
11
+ def next
12
+ tell_to 'next track'
13
+ end
14
+
15
+ def voldown
16
+ tell_to 'set sound volume to sound volume - 10'
17
+ end
18
+
19
+ def volup
20
+ tell_to 'set sound volume to sound volume + 10'
21
+ end
22
+
23
+ def volume
24
+ tell_to 'return sound volume'
25
+ end
26
+
27
+ def track
28
+ tell_to 'return name of current track'
29
+ end
30
+
31
+ def artist
32
+ tell_to 'return artist of current track'
33
+ end
34
+
35
+ def album
36
+ tell_to 'return album of current track'
37
+ end
38
+
39
+ def launched?
40
+ return false if RUBY_PLATFORM !~ /darwin/
41
+ nb = %x(osascript -e 'tell app "System Events" to count (every process whose name is "iTunes")' 2>/dev/null).rstrip
42
+ nb.match(/^\d+/) and nb.to_i > 0 ? true : false
43
+ end
44
+
45
+ def name
46
+ "iTunes"
47
+ end
48
+
49
+ private
50
+ def tell_to(command)
51
+ %x(osascript -e 'tell app "iTunes" to #{command}').rstrip
52
+ end
53
+ end
54
+ end
55
+
@@ -0,0 +1,56 @@
1
+ module Anyplayer
2
+ class Ituneswindows < Player
3
+ def playpause
4
+ @itunes.PlayPause()
5
+ end
6
+
7
+ def prev
8
+ @itunes.PreviousTrack()
9
+ end
10
+
11
+ def next
12
+ @itunes.NextTrack()
13
+ end
14
+
15
+ def voldown
16
+ @itunes.SoundVolume=@itunes.SoundVolume - 10
17
+ end
18
+
19
+ def volup
20
+ @itunes.SoundVolume=@itunes.SoundVolume + 10
21
+ end
22
+
23
+ def volume
24
+ @itunes.SoundVolume
25
+ end
26
+
27
+ def track
28
+ @itunes.CurrentTrack.name
29
+ end
30
+
31
+ def artist
32
+ @itunes.CurrentTrack.Artist
33
+ end
34
+
35
+ def album
36
+ @itunes.CurrentTrack.Album
37
+ end
38
+
39
+ def launched?
40
+ return false if RUBY_PLATFORM !~ /cygwin|mswin|mingw|bccwin|wince|emx/
41
+
42
+ begin
43
+ require 'win32ole'
44
+ @itunes = WIN32OLE.new("iTunes.Application")
45
+ return true
46
+ rescue LoadError
47
+ return false
48
+ end
49
+ end
50
+
51
+ def name
52
+ "iTunes Windows"
53
+ end
54
+ end
55
+ end
56
+
@@ -0,0 +1,55 @@
1
+ module Anyplayer
2
+ class Mpd < Player
3
+ def playpause
4
+ mpc 'toggle'
5
+ end
6
+
7
+ def prev
8
+ mpc 'prev'
9
+ end
10
+
11
+ def next
12
+ mpc 'next'
13
+ end
14
+
15
+ def voldown
16
+ mpc 'volume -10'
17
+ end
18
+
19
+ def volup
20
+ mpc 'volume +10'
21
+ end
22
+
23
+ def volume
24
+ mpc('volume').grep(/([0-9]+)/)
25
+ $1
26
+ end
27
+
28
+ def track
29
+ mpc '-f "%title%" current'
30
+ end
31
+
32
+ def artist
33
+ mpc '-f "%artist%" current'
34
+ end
35
+
36
+ def album
37
+ mpc '-f "%album%" current'
38
+ end
39
+
40
+ def launched?
41
+ %x(mpc 2> /dev/null)
42
+ $? == 0
43
+ end
44
+
45
+ def host
46
+ ENV['MPD_HOST'] || super
47
+ end
48
+
49
+ private
50
+ def mpc(command)
51
+ %x(mpc #{command}).split("\n").first
52
+ end
53
+ end
54
+ end
55
+
@@ -0,0 +1,49 @@
1
+ module Anyplayer
2
+ class Rhythmbox < Player
3
+ def playpause
4
+ tell_to 'play-pause'
5
+ end
6
+
7
+ def prev
8
+ tell_to 'previous'
9
+ end
10
+
11
+ def next
12
+ tell_to 'next'
13
+ end
14
+
15
+ def voldown
16
+ tell_to 'volume-down'
17
+ end
18
+
19
+ def volup
20
+ tell_to 'volume-up'
21
+ end
22
+
23
+ def volume
24
+ tell_to 'print-volume'
25
+ end
26
+
27
+ def track
28
+ tell_to 'print-playing-format=%tt'
29
+ end
30
+
31
+ def artist
32
+ tell_to 'print-playing-format=%ta'
33
+ end
34
+
35
+ def album
36
+ tell_to 'print-playing-format=%at'
37
+ end
38
+
39
+ def launched?
40
+ %x(rhythmbox-client --no-start --print-playing 2>/dev/null).rstrip != ""
41
+ end
42
+
43
+ private
44
+ def tell_to(command)
45
+ %x(rhythmbox-client --no-start --#{command}).rstrip
46
+ end
47
+ end
48
+ end
49
+
@@ -0,0 +1,61 @@
1
+ module Anyplayer
2
+ class Xmms2 < Player
3
+ def playpause
4
+ xmms2 'toggle'
5
+ end
6
+
7
+ def prev
8
+ xmms2 'prev'
9
+ end
10
+
11
+ def next
12
+ xmms2 'next'
13
+ end
14
+
15
+ def voldown
16
+ current = volume.to_i
17
+ new_volume = (current < 11 ? 0 : current - 10)
18
+ xmms2 "server volume #{new_volume}"
19
+ end
20
+
21
+ def volup
22
+ current = volume.to_i
23
+ new_volume = (current > 89 ? 100 : current + 10)
24
+ xmms2 "server volume #{new_volume}"
25
+ end
26
+
27
+ def volume
28
+ #currently just the first (left?) channel
29
+ xmms2('server volume').split("\n").first.sub /([^0-9]*)/, ''
30
+ $1
31
+ end
32
+
33
+ def track
34
+ xmms2 "status -f '${title}'"
35
+ end
36
+
37
+ def artist
38
+ xmms2 "status -f '${artist}'"
39
+ end
40
+
41
+ def album
42
+ xmms2 "status -f '${album}'"
43
+ end
44
+
45
+ def launched?
46
+ # xmms2 autolaunches the daemon, so this should always be true
47
+ %x(xmms2 status 2> /dev/null)
48
+ $? == 0
49
+ end
50
+
51
+ def host
52
+ ENV['XMMS_PATH'] || super
53
+ end
54
+
55
+ private
56
+ def xmms2(command)
57
+ %x(xmms2 #{command}).split("\n").first
58
+ end
59
+ end
60
+
61
+ end
@@ -0,0 +1,3 @@
1
+ module Anyplayer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'test/unit'
5
+ require 'anyplayer'
6
+
7
+ class AnyplayerTest < Test::Unit::TestCase
8
+ def test_music_player_running
9
+ player = Anyplayer::launched
10
+ assert_not_nil player
11
+ assert player.is_a?(Anyplayer::Player)
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: anyplayer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Sunny Ripert
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-18 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Play/pause/skip songs in iTunes, Rythmbox, MPD, XMMS
23
+ email:
24
+ - sunny@sunfox.org
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - Readme.markdown
36
+ - anyplayer.gemspec
37
+ - lib/anyplayer.rb
38
+ - lib/anyplayer/player.rb
39
+ - lib/anyplayer/players/amarok.rb
40
+ - lib/anyplayer/players/itunes.rb
41
+ - lib/anyplayer/players/ituneswindows.rb
42
+ - lib/anyplayer/players/mpd.rb
43
+ - lib/anyplayer/players/rhythmbox.rb
44
+ - lib/anyplayer/players/xmms2.rb
45
+ - lib/anyplayer/version.rb
46
+ - test/anyplayer_test.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/sunny/anyplayer
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.3.7
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Interact with the running music player
81
+ test_files: []
82
+