<commit_before>from willie.module import commands
from willie.config import ConfigurationError

import urllib, json

def configure(config):
    if config.option('Configure last.fm', False):
        config.add_section('lastfm')
        config.interactive_add(
            'lastfm', 'apikey',
            'Last.fm API key'
        )

def setup(bot):
    if not bot.config.has_section('lastfm'):
        raise ConfigurationError('Last.fm is not configured')
    if not bot.config.has_option('lastfm', 'apikey'):
        raise ConfigurationError('Last.fm API key is not defined')

@commands('stream')
def lastfm(bot, trigger):
    username = trigger.group(2)

    if not username:
        bot.say('You need to specify a username.')
    else:
        resp = urllib.urlopen("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user={0}&api_key={1}&format=json".format(username, bot.config.lastfm.apikey))

        if not resp:
            bot.say('Failed to request last.fm data.')
        else:
            data = json.loads(resp.read())

            if not data:
                bot.say('Failed to parse last.fm data.')
            else:
                if not 'recenttracks' in data or not 'track' in data.recenttracks:
                    bot.say('Invalid username or no recently played tracks.')
                else:
                    currenttrack = ''

                    for track in data.recenttracks.track:
                        if track['@attr'] and track['@attr']['nowplaying']:
                            currenttrack = '{0} - {1}'.format(track.artist['#text'], track.name) 
                            break

                    if currenttrack != '' then:
                        bot.say('{0} is currently listening to: {1} (via last.fm)'.format(username, currenttrack))
                    else:
                        bot.say('%s is not currently listening to anything.' % username)<commit_msg>I went all Lua-mode<commit_after>from willie.module import commands
from willie.config import ConfigurationError

import urllib, json

def configure(config):
    if config.option('Configure last.fm', False):
        config.add_section('lastfm')
        config.interactive_add(
            'lastfm', 'apikey',
            'Last.fm API key'
        )

def setup(bot):
    if not bot.config.has_section('lastfm'):
        raise ConfigurationError('Last.fm is not configured')
    if not bot.config.has_option('lastfm', 'apikey'):
        raise ConfigurationError('Last.fm API key is not defined')

@commands('stream')
def lastfm(bot, trigger):
    username = trigger.group(2)

    if not username:
        bot.say('You need to specify a username.')
    else:
        resp = urllib.urlopen("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user={0}&api_key={1}&format=json".format(username, bot.config.lastfm.apikey))

        if not resp:
            bot.say('Failed to request last.fm data.')
        else:
            data = json.loads(resp.read())

            if not data:
                bot.say('Failed to parse last.fm data.')
            else:
                if not 'recenttracks' in data or not 'track' in data.recenttracks:
                    bot.say('Invalid username or no recently played tracks.')
                else:
                    currenttrack = ''

                    for track in data.recenttracks.track:
                        if track['@attr'] and track['@attr']['nowplaying']:
                            currenttrack = '{0} - {1}'.format(track.artist['#text'], track.name) 
                            break

                    if currenttrack != '':
                        bot.say('{0} is currently listening to: {1} (via last.fm)'.format(username, currenttrack))
                    else:
                        bot.say('%s is not currently listening to anything.' % username)
{'fraction_non_alphanumeric': 0.11885565669700911, 'fraction_numerical': 0.0046814044213263975, 'mean_word_length': 1.9403669724770642, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2907702', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1026, 'n_words': 275}