class MprisScrobbler::Spy

Constants

CONFIG_DIR
CONFIG_FILE

Attributes

config[R]
interface[R]
queue[R]

Public Class Methods

new() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 10
def initialize
  @config =
    if File.exists?(config_file_path)
      load_config
    else
      gen_config
      puts "Config example saved to #{File.join(CONFIG_DIR, CONFIG_FILE)}"
      puts "Refer to the README file for next steps."
      exit(0)
    end

  init_rockstar
  ask_auth unless session_key

  begin
    @interface = get_dbus_interface
  rescue DBus::Error
    puts "error querying DBus, make sure your music player is running!"
    exit 1
  end

  @queue = []
end

Public Instance Methods

run() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 34
def run
  user = Rockstar::User.new(lastfm_config["username"])
  last_track = user.recent_tracks.first.name rescue nil

  loop do
    current_track, artist, album, length = get_dbus_metadata

    if current_track != last_track
      @queue << {
        track: current_track,
        artist: artist.first,
        album: album,
        length: length,
        scrobbled: false
      }

      if queue.size == 1
        update_now_playing
      else
        scrobble_previous_track
        update_now_playing
      end

      last_track = current_track
    end

    trim_queue if queue.size > 5

    sleep sleep_period
  end
end

Private Instance Methods

ask_auth() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 115
def ask_auth
  auth = Rockstar::Auth.new
  token = auth.token

  puts
  puts "Please open http://www.last.fm/api/auth/?api_key=#{Rockstar.lastfm_api_key}&token=#{token}"
  puts
  puts "Press enter when done."

  gets

  session = auth.session(token)

  save_session(session)
end
config_file_path() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 111
def config_file_path
  File.expand_path(File.join(CONFIG_DIR, CONFIG_FILE))
end
gen_config() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 160
def gen_config
  require 'fileutils'
  FileUtils.mkdir_p File.expand_path(CONFIG_DIR)

  template = {
    lastfm: {
      username: "XXX",
      api_key: "XXX",
      api_secret: "XXX",
      session_key: nil
    },
    mpris: {
      player: "audacious",
      poll_every_seconds: 10
    }
  }

  File.open(config_file_path,"w") { |f| f.write template.to_yaml }
end
get_dbus_interface() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 184
def get_dbus_interface
  bus = DBus::SessionBus.instance
  service = bus.service("org.mpris.MediaPlayer2.#{mpris_config["player"]}")
  player = service.object("/org/mpris/MediaPlayer2")

  player.introspect
  player["org.mpris.MediaPlayer2.Player"]
end
get_dbus_metadata() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 102
def get_dbus_metadata
  interface["Metadata"].values_at(
    "xesam:title",
    "xesam:artist",
    "xesam:album",
    "mpris:length"
  )
end
init_rockstar() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 153
def init_rockstar
  Rockstar.lastfm = {
    api_key: lastfm_config["api_key"],
    api_secret: lastfm_config["api_secret"]
  }
end
lastfm_config() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 145
def lastfm_config
  config["lastfm"]
end
load_config() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 180
def load_config
  YAML.load_file(config_file_path)
end
mpris_config() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 149
def mpris_config
  config["mpris"]
end
save_session(session) click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 131
def save_session(session)
  @config["lastfm"]["session_key"] = session.key

  File.open(config_file_path,"w") { |f| f.write @config.to_yaml }
end
scrobble_previous_track() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 73
def scrobble_previous_track
  track_info = queue[-2]
  return if track_info[:scrobbled]

  Rockstar::Track.scrobble(
    session_key: session_key,
    track: track_info[:track],
    artist: track_info[:artist],
    album: track_info[:album],
    time: Time.now - (track_info[:length] / 1_000_000),
    length: track_info[:length]
  )

  @queue[-2][:scrobbled] = true
end
session_key() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 137
def session_key
  lastfm_config["session_key"]
end
sleep_period() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 141
def sleep_period
  mpris_config["poll_every_seconds"]
end
trim_queue() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 69
def trim_queue
  @queue = @queue[-3..-1]
end
update_now_playing() click to toggle source
# File lib/mpris_scrobbler/spy.rb, line 89
def update_now_playing
  track_info = queue.last

  Rockstar::Track.updateNowPlaying(
    session_key: session_key,
    track: track_info[:track],
    artist: track_info[:artist],
    album: track_info[:album],
    time: Time.now,
    length: track_info[:length]
  )
end