class Descartes::LastFm

Public Instance Methods

add_user(m, lastfmnick) click to toggle source
# File lib/descartes/modules/lastfm.rb, line 54
def add_user(m, lastfmnick)
  nicks              = get_lastfm_nicks_archive
  nicks[m.user.nick] = lastfmnick

  file = File.join $options[:dotfiles], 'lastfm_nicks.yml'
  File.open(file, ?w) { |f| f.write YAML.dump(nicks) }

  m.reply "Ok, added user #{lastfmnick}."
end
authenticate!() click to toggle source
# File lib/descartes/modules/lastfm.rb, line 23
def authenticate!
  file = File.join $options[:dotfiles], 'lastfm_api.yml'
  Rockstar.lastfm = YAML.load_file file
end
get_lastfm_nicks_archive() click to toggle source
# File lib/descartes/modules/lastfm.rb, line 28
def get_lastfm_nicks_archive
  file = File.join $options[:dotfiles], 'lastfm_nicks.yml'
  FileUtils.touch file unless File.exists? file
  YAML.load_file(file) || {}
end
last_played_song(m) click to toggle source
# File lib/descartes/modules/lastfm.rb, line 35
def last_played_song(m)
  authenticate!

  usernick   = m.user.nick
  lastfmnick = get_lastfm_nicks_archive[usernick]
  m.reply "Hey #{usernick.colorize}, I don't know your Last.fm nick. add it using '!lastfmuser add <lastfmnick>'." unless lastfmnick

  user  = Rockstar::User.new lastfmnick
  track = user.recent_tracks.first

  album = track.album.empty? ? '' : " (in #{track.album.colorize})"
  if track.now_playing?
    m.reply "#{lastfmnick.colorize} is listening to #{track.name.decode.colorize} by #{track.artist.decode.colorize}#{album} right now!"
  else
    m.reply "The last song #{lastfmnick.colorize} listened to is #{track.name.decode.colorize} by #{track.artist.decode.colorize}#{album}."
  end
end
remove_user(m, lastfmnick) click to toggle source
# File lib/descartes/modules/lastfm.rb, line 65
def remove_user(m, lastfmnick)
  nicks = get_lastfm_nicks_archive
  nicks.delete lastfmnick

  file  = File.join $options[:dotfiles], 'lastfm_nicks.yml'
  File.open(file, ?w) { |f| f.write YAML.dump(nicks) }

  m.reply "Ok, removed user #{lastfmnick}."
end
show_relations(m, usernicks) click to toggle source
# File lib/descartes/modules/lastfm.rb, line 76
def show_relations(m, usernicks)
  usernick_list = usernicks.split
  found         = false

  get_lastfm_nicks_archive.each { |usernick, lastfmnick|
    if usernick_list.include? usernick
      found = true
      m.reply "#{usernick} is known as #{lastfmnick}."
    end
  }

  m.reply 'I don\'t know anthing, I know only what I know.' unless found
end