Guest User

Untitled

a guest
Apr 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. class MPD < Widget
  2. description "MPD Information"
  3. dependency "socket", "Ruby standard library"
  4. dependency "yaml", "Ruby standard library"
  5. option :hostname, "MPD server hostname", "localhost"
  6. option :port, "MPD server port number", 6600
  7. option :password, "Authentication password"
  8. field :state, "Play state, :playing, :paused or :stopped"
  9. field :artist, "Song artist"
  10. field :title, "Song title"
  11. field :length, "Song length"
  12. field :position, "Song position"
  13. field :date, "Song date from ID3 tag"
  14. field :id, "Song ID in playlist"
  15. field :genre, "Song genre"
  16. field :album, "Song album"
  17. field :file, "File name of current song"
  18. field :shortfile, "Filename without directory path"
  19. field :percent, "Finished percent of current song", 0.0
  20. field :random, "True if playlist is on random", false
  21. field :repeat, "True if playlist is on repeat", false
  22. field :volume, "Volume of MPD mixer", 0
  23.  
  24. default do
  25. track = "#{@artist && "#@artist - "}#@title"
  26. track = @shortfile if track.empty?
  27. case @state
  28. when :playing
  29. track
  30. when :paused
  31. "#{track} [paused]"
  32. when :stopped
  33. "[mpd not playing]"
  34. end
  35. end
  36.  
  37. init do
  38. @@connections ||= {}
  39. mpd = nil
  40. unless @@connections["#@hostname:#@port"]
  41. @@connections["#@hostname:#@port"] = TCPSocket.new(@hostname, @port)
  42. mpd = @@connections["#@hostname:#@port"]
  43. mpd.gets
  44. else
  45. mpd = @@connections["#@hostname:#@port"]
  46. mpd.puts("ping")
  47. unless mpd.gets
  48. @@connections["#@hostname:#@port"] = TCPSocket.new(@hostname, @port)
  49. mpd = @@connections["#@hostname:#@port"]
  50. mpd.gets
  51. end
  52. end
  53. if @password
  54. mpd.puts("password #@password")
  55. if mpd.gets.split[0] == "ACK"
  56. raise WidgetError, "incorrect password"
  57. end
  58. end
  59. mpd.puts("status")
  60. status = YAML.load(mpd.gets("OK\n").chomp("OK\n"))
  61. @state = {:play => :playing, :pause => :paused, :stop => :stopped}[status["state"].to_sym]
  62. @random = status["random"] == 1 ? true : false
  63. @repeat = status["repeat"] == 1 ? true : false
  64. @volume = status["volume"]
  65. mpd.puts("currentsong")
  66. if song = YAML.load(mpd.gets("OK\n").chomp("OK\n"))
  67. @artist = song["Artist"]
  68. @title = song["Title"]
  69. len = song["Time"]
  70. minutes = (len / 60) % 60
  71. seconds = len % 60
  72. @length = "%d:%02d" % [minutes, seconds]
  73. pos = status["time"].to_i
  74. minutes = (pos / 60) % 60
  75. seconds = pos % 60
  76. @position = "%d:%02d" % [minutes, seconds]
  77. @date = song["Date"]
  78. @id = song["Id"]
  79. @genre = song["Genre"]
  80. @album = song["Album"]
  81. @file = song["file"]
  82. @shortfile = ::File.basename(@file)
  83. @percent = pos / len.to_f
  84. end
  85. end
  86. end
Add Comment
Please, Sign In to add comment