Script: weespotify.rb

Display which song spotify is currently playing.
Author: PawelP — Version: 1.1 — License: GPL-3.0-or-later
For WeeChat ≥ 0.3.0, requires: dbus.
Tags: music
Added: 2013-09-21 — Updated: 2016-03-25

Download GitHub Repository

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# encoding: UTF-8
# Copyright (C) 2013 Paweł Pogorzelski <pawelpogorzelski@gmail.com>
# Released under GPL3.
#

SCRIPT_NAME = "weespotify".freeze
SCRIPT_AUTHOR = "Paweł Pogorzelski <pawelpogorzelski@gmail.com>".freeze
SCRIPT_VERSION = "1.1".freeze
SCRIPT_LICENSE = "GLP3".freeze
DESCRIPTION = "Now playing script for spotify (*nix only)".freeze

COMMAND_NAME = "weespotify"
COMMAND_DESCRIPTION = "display currently playing track from spotify"

class SpotifyTrack
  attr_accessor :title, :album, :artist

  SPLITTER = "♫"

  def print_output
    return "/me is listening to #{SPLITTER} #{self.title} #{SPLITTER} by #{self.artist} from the album #{self.album} on Spotify."
  end
end


def weechat_init
  Weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, DESCRIPTION, "", "")

  command_hook = Weechat.hook_command(COMMAND_NAME, COMMAND_DESCRIPTION, "", "", "", "weespotify_command", "")

  Weechat::WEECHAT_RC_OK
end


def weespotify_command(data, buffer,args)
  begin
    spotify_data = `dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata'`
  rescue
    spotify_data = "SPOTIFY_NOT_RUNNING"
  end
  spotify_object = SpotifyTrack.new

  if spotify_data!= "SPOTIFY_NOT_RUNNING"
    spotify_array = spotify_data.to_s.split('dict entry')
    spotify_array.each {|spotify_info|
      ["title","albumArtist","album"].any? { |tested_info|
        if spotify_info.include? tested_info
          data = spotify_info.split('variant')[1]
          start_position = data.index('"')
          end_position = data.index('"',start_position+1)
          content = data[start_position+1..end_position-1]
          tested_info == 'title' ? spotify_object.title=content : nil
          tested_info == 'album' ? spotify_object.album=content : nil
          tested_info == 'albumArtist' ? spotify_object.artist=content : nil
        end
      }
    }
    Weechat.command(Weechat.current_buffer,spotify_object.print_output)
  end
end