Last active
September 25, 2018 09:38
-
-
Save pawndev/213e0e79ae7331e27de35fd34871767a to your computer and use it in GitHub Desktop.
clemtine cli dbus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# gem install ruby-dbus | |
require 'optparse' | |
require 'optparse/time' | |
require 'ostruct' | |
require 'pp' | |
require 'dbus' | |
Version = [0, 1, 0] | |
class ClementineDbus | |
def initialize() | |
@bus = DBus::SessionBus.instance | |
@clementine = @bus.service("org.mpris.clementine").object('/org/mpris/MediaPlayer2') | |
@clementine.introspect | |
@clementine.default_iface = 'org.mpris.MediaPlayer2.Player' | |
end | |
def Toggle() | |
@clementine.PlayPause | |
end | |
def Play() | |
@clementine.Play | |
end | |
def Pause() | |
@clementine.Pause | |
end | |
def Next() | |
@clementine.Next | |
end | |
def Prev() | |
@clementine.Previous | |
end | |
def Stop() | |
@clementine.Stop | |
end | |
def Exit() | |
@clementine.Quit | |
end | |
def Exec() | |
=begin | |
Here, you can use these following command: | |
:PlayPause, :Play, :Pause, :Next, :Previous, :Stop, :Quit, :GetAll, :Get, :Set, :Raise, | |
:Seek, :SetPosition, :OpenUri, :ActivatePlaylist, :GetPlaylists, :GetTracksMetadata, | |
:AddTrack, :RemoveTrack, :GoTo, :Introspect, :Ping, :GetMachineId | |
=end | |
return @clementine | |
end | |
end | |
class OptparseClementine | |
@bus = ClementineDbus.new | |
def self.parse(args) | |
options = OpenStruct.new | |
options.encoding = "utf8" | |
opt_parser = OptionParser.new do |opts| | |
opts.banner = "Usage: clem-cli [options]\nVersion: %{version}" % {version: ::Version.join('.')} | |
opts.separator "" | |
opts.separator "Specific options:" | |
opts.on("-t", "--toggle", "[T]oggle between play/pause") do | |
@bus.Toggle | |
end | |
opts.on("-l", "--play", "P[l]ay clementine") do | |
@bus.Play | |
end | |
opts.on("-a", "--pause", "P[a]use clementine") do | |
@bus.Pause | |
end | |
opts.on("-n", "--next", "[N]ext song in clementine") do | |
@bus.Next | |
end | |
opts.on("-p", "--prev", "[P]revious song in clementine") do | |
@bus.Prev | |
end | |
opts.on("-s", "--stop", "[S]top song in clementine") do | |
@bus.Stop | |
end | |
opts.on("-x", "--exit", "E[x]it the clementine player") do | |
print @bus.Exit | |
end | |
# opts.on("-e", "--execute", "[S]top song in clementine") do | |
# print @bus.Exec | |
# end | |
opts.separator "" | |
opts.separator "Common options:" | |
opts.on_tail("-h", "--help", "Show this message") do | |
puts opts | |
exit | |
end | |
opts.on_tail("-v", "--version", "Show version") do | |
puts ::Version.join('.') | |
exit | |
end | |
end | |
opt_parser.parse!(args) | |
options | |
end | |
end | |
if ARGV.empty? | |
ARGV.push "-h" | |
end | |
begin | |
options = OptparseClementine.parse(ARGV) | |
pp options | |
pp ARGV | |
rescue OptionParser::InvalidOption => e | |
e = e.to_s | |
e[0] = e.chars.first.upcase | |
puts e | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment