Class: Descartes::LastFm

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin
Defined in:
lib/descartes/modules/lastfm.rb

Instance Method Summary collapse

Instance Method Details

#add_user(m, lastfmnick) ⇒ Object



54
55
56
57
58
59
60
61
62
# 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!Object



23
24
25
26
# 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_archiveObject



28
29
30
31
32
# 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) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# 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