Skip to content

Commit

Permalink
Finalized Last.FM plugin. Compares users and fetches \"Now Playing\" …
Browse files Browse the repository at this point in the history
…information from the web
  • Loading branch information
Namasteh committed Aug 16, 2014
1 parent 64a1cab commit 177865d
Showing 1 changed file with 89 additions and 1 deletion.
90 changes: 89 additions & 1 deletion lib/plugins/last_fm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@ module Plugins
class LastFm
include Cinch::Plugin

set :required_options, [:key]
set :plugin_name, 'lastfm'
set :help, <<-USAGE.gsub(/^ {6}/, '')
Check the weather right from the IRC channel!
Usage:
* !np: This will return what you're currently listening to via Last.FM
* !compare <nick|username>: Using an IRC nick of someone who has their information stored in my databases or just a Last.FM username you can compare your tastes!
USAGE

BaseURL = "http://ws.audioscrobbler.com/2.0/"

CmpBars = ["[4==== ]",
"[4====7==== ]",
"[4====7====8==== ]",
"[4====7====8====9====]",
"[ ]"]

def initialize(*args)
super
if File.exist?('docs/userinfo.yaml')
Expand All @@ -20,6 +35,7 @@ def initialize(*args)
match /np/, method: :nowPlaying

def nowPlaying(m)
key = config[:key]
reload
return m.reply "You have no information saved in my database. To save your lastfm username type !set-lastfm <username>" if !@storage.key?(m.user.nick)
return m.reply "Your database table has no LastFM username saved. To save a LastFM username type !set-lastfm <username>" if !@storage[m.user.nick].key? 'lastfm'
Expand Down Expand Up @@ -47,7 +63,79 @@ def nowPlaying(m)

uPlays = trackInfo['track']['userplaycount']

m.reply "#{m.user.nick} - Track: \"4#{track}\" | Artist: 7#{artist} | Album: \"10#{album}\" | Loved #{loved} | Plays: #{uPlays} "
if uPlays.nil?
uPlays = "1"
else
uPlays = uPlays.to_i + 1
end

topTags = trackInfo['track']['toptags']
tags = []
if !topTags.is_a?(String)# when there are no tags on a track it returns a string (no keys)
for i in topTags['tag']
tags << i['name']
end
# sometimes tracks have no tags so lets fetch the artist's tags
else
artistInfo = JSON.parse(open(URI.encode("#{BaseURL}?method=artist.getInfo&artist=#{artist}&api_key=#{key}&format=json")).read)
topTags = artistInfo['artist']['tags']
for i in topTags['tag']
tags << i['name']
end
end

m.reply "(0,5Last.FM)#{m.user.nick} - Track: \"4#{track}\" | Artist: 7#{artist} | Album: \"10#{album}\" | Loved #{loved} | Plays: #{uPlays} | #{tags.join(", ")}"
end

match /compare (.+)/, method: :compare

def compare(m, user)
key = config[:key]
reload

return m.reply "You have no information saved in my database. To save your lastfm username type !set-lastfm <username>" if !@storage.key?(m.user.nick)
return m.reply "Your database table has no LastFM username saved. To save a LastFM username type !set-lastfm <username>" if !@storage[m.user.nick].key? 'lastfm'

user1 = @storage[m.user.nick]['lastfm']

if !@storage.key?(user)
m.reply "#{user} has no information in my database. Trying LastFM username..."

user2 = user
else
if !@storage[user].key? 'lastfm'
m.reply "#{user} has no LastFM username set in my database. Trying LastFM username..."

user2 = user
else
user2 = @storage[user]['lastfm']
end
end

compareInfo = JSON.parse(open(URI.encode("#{BaseURL}?method=tasteometer.compare&type1=user&type2=user&value1=#{user1}&value2=#{user2}&api_key=#{key}&format=json")).read)

return m.reply "Invalid username #{user2}." if compareInfo['error']

index = (compareInfo['comparison']['result']['score'].to_f * 100).to_i

bar = CmpBars[4]
if index >= 1
bar = CmpBars[(index / 25.01).to_i]
end

artists = []

raw_artists = compareInfo['comparison']['result']['artists']

if !raw_artists.key? '@attr'
artists = ["N/A"]
else
for i in raw_artists['artist']
artists << i['name']
end
end

m.reply "(0,5Last.FM) Comparison: #{m.user.nick} #{bar} #{user} | Similarity #{index}% | Common artists: #{artists.join(", ")}"
end

def reload
Expand Down

0 comments on commit 177865d

Please sign in to comment.