<commit_before>require 'json'

class LastfmPlugin
  include NinjaPlugin

  match /lastfm (.+)/

  def usage
    "!lastfm <user> -- display top weekly artists"
  end

  def execute(m, query)
    begin
      result = JSON.parse(open("http://ws.audioscrobbler.com/2.0/?method=user.getweeklyartistchart&user=#{URI.escape(query)}&api_key=b25b959554ed76058ac220b7b2e0a026&format=json").read)
      top_artists = result["weeklyartistchart"]["artist"][0..4]
      reply = "Top 5 Weekly Last.fm artists: "
      top_artists.each do |artist|
        reply = reply + "#{artist['name']} (#{artist['playcount']}), "
      end
      reply = reply[0..reply.length-3]
    rescue
      reply = "The user #{query} doesn't have a Last.fm account"
    end
    m.reply "#{m.user.nick}: #{reply}"
  end
end

class NowPlayingPlugin
  include NinjaPlugin

  match /np (.+)/, method: :np_user
  match /np$/, method: :np
  match /np_alias (\S+)/, method: :np_alias

  def usage
    "!np [user] -- display last played song"
  end

  def np_user(m, query)
    begin
      result = JSON.parse(open("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=#{URI.escape(query)}&api_key=b25b959554ed76058ac220b7b2e0a026&format=json&limit=1").read)
      last_song = result["recenttracks"]["track"][0]
      reply = "#{last_song['name']} by #{last_song['artist']['#text']}"
    rescue
      reply = "The user #{query} doesn't have a Last.fm account"
    end
    m.reply "#{m.user.nick}: #{reply}"
  end

  def np(m)
    user = Channel.get_user(m.channel.name, m.user.nick)

    begin
      result = JSON.parse(open("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=#{URI.escape(user.lastfm_user||user.nick)}&api_key=b25b959554ed76058ac220b7b2e0a026&format=json&limit=1").read)
      last_song = result["recenttracks"]["track"][0]
      reply = "#{last_song['name']} by #{last_song['artist']['#text']}"<commit_msg>Fixing LastFM plugin.<commit_after>require 'json'

class LastfmPlugin
  include NinjaPlugin

  match /lastfm (.+)/

  def usage
    "!lastfm <user> -- display top weekly artists"
  end

  def execute(m, query)
    begin
      result = JSON.parse(open("http://ws.audioscrobbler.com/2.0/?method=user.getweeklyartistchart&user=#{URI.escape(query)}&api_key=b25b959554ed76058ac220b7b2e0a026&format=json").read)
      top_artists = result["weeklyartistchart"]["artist"][0..4]
      reply = "Top 5 Weekly Last.fm artists: "
      top_artists.each do |artist|
        reply = reply + "#{artist['name']} (#{artist['playcount']}), "
      end
      reply = reply[0..reply.length-3]
    rescue
      reply = "The user #{query} doesn't have a Last.fm account"
    end
    m.reply "#{m.user.nick}: #{reply}"
  end
end

class NowPlayingPlugin
  include NinjaPlugin

  match /np (.+)/, method: :np_user
  match /np$/, method: :np
  match /np_alias (\S+)/, method: :np_alias

  def usage
    "!np [user] -- display last played song"
  end

  def np_user(m, query)
    begin
      result    = JSON.parse(open("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=#{URI.escape(query)}&api_key=b25b959554ed76058ac220b7b2e0a026&format=json&limit=1").read)
      last_song = result['recenttracks']['track']
      last_song = result['recenttracks']['track'].first if last_song.is_a? Array
      reply     = "♫ #{last_song['name']} - #{last_song['artist']['#text']} ♫"
    rescue
      reply = "The user #{query} doesn't have a Last.fm account"
    end
    m.reply "#{m.user.nick}: #{reply}"
  end

  def np(m)
    user = Channel.get_user(m.channel.name, m.user.nick)

    begin
      result    = JSON.parse(open("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=#{URI.escape(user.lastfm_user||user.nick)}&api_key=b25b959554ed76058ac220b7b2e0a026&format=json&limit=1").read)
      last_song = result['recenttracks']['track']
      last_song = result['recenttracks']['track'].first if last_song.is_a? Array
      reply     = "♫ #{last_song['name']} - #{last_song['artist']['#text']} ♫"
{'fraction_non_alphanumeric': 0.16916541729135431, 'fraction_numerical': 0.0399800099950025, 'mean_word_length': 3.7541567695961997, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6135415', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1466, 'n_words': 261}