<commit_before>#coding:utf8
from queryBot.responsemodule import IModule
import lastfmapi

api_key_g = 'api_key'
service_g = 'service'

class MusicStatus(IModule):
    def __init__(self):
        self.settings = {api_key_g: None,
                         service_g: None}

        self.api = None
        self.shorten_url = None
        self.last_response = u''

    def uses_settings(self):
        return True

    def get_command_patterns(self):
        return [('^\.np (\S.*)', self.now_playing)]

    def get_settings_name(self):
        return 'music'

    def get_configuration(self):
        return [(key, value) for (key, value) in self.settings.items() if not value is None]

    def set_configuration(self, settings):
        #fetch the settings
        for key, value in settings.items():
            if key in settings:
                self.settings[key] = value

        # now we can configure the module
        if(self.settings[service_g] == 'lastfm' and \
           not self.settings[api_key_g] is None):
            self.api = lastfmapi.LastFmApi(self.settings[api_key_g])
        else:
            raise RuntimeError('Couldn\'t configure the module')

    def set_url_shortener(self, shortener):
        self.shorten_url = shortener.shorten_url

    # Functions that are needed for queries
    def now_playing(self, nick, message, channel):
        result = self.fetch_status(nick)
        #raise NotImplementedError()
        if result and result != self.last_response:
            self.last_response = result
            return result.encode('utf-8', errors='ignore')

    def __fetch_status(self, luser):
        #cleanup the message to get the query
        #query = re.sub(r'^\.np', '', message.decode('utf-8'), re.UNICODE).encode('utf-8')
        #luser = query.strip(u' \t\n\r')
        try:
            response = self.api.user_getRecentTracks(user=luser.encode('utf-8'))
        except lastfmapi.LastFmApiException:
            return u' '.join([luser, u'doesn\'t exist'])
        message = u' '.join([luser, u'isn\'t playing any song'])

        if u'track' in response[u'recenttracks'].keys() and len(response[u'recenttracks'][u'track']) > 0:
            rtrack = response[u'recenttracks'][u'track'][0]
            if u'@attr' in rtrack.keys() and u'nowplaying' in rtrack['@attr'].keys():
                ltitle = rtrack[u'name']
                lartist = rtrack[u'artist'][u'#text']

                message = u' '.join([luser,  u'is now playing', ltitle, u'—',  lartist])

            try:
                response = self.api.track_getInfo(track=ltitle.encode('utf-8'),artist=lartist.encode('utf-8'),username=luser.encode('utf-8'))
            except lastfmapi.LastFmApiException:
                return message

            if u'userloved' in response[u'track'].keys() and response[u'track'][u'userloved'] == '1':
                message += ' ♥'
            try:
                toptags = response[u'track'][u'toptags'][u'tag']
                message += ' ('
                for tag in toptags:
                    message += tag[u'name'] + ', '
                message = message[:-2] + ')'
            except Exception:
                pass

        return message<commit_msg>Corrected the status-fetching code in the music module<commit_after>#coding:utf8
from queryBot.responsemodule import IModule
import lastfmapi
import re

api_key_g = 'api_key'
service_g = 'service'

class MusicStatus(IModule):
    def __init__(self):
        self.settings = {api_key_g: None,
                         service_g: None}

        self.api = None
        self.shorten_url = None
        self.last_response = u''

    def uses_settings(self):
        return True

    def get_command_patterns(self):
        return [('^\.np( (\S.*))?', self.now_playing)]

    def get_settings_name(self):
        return 'music'

    def get_configuration(self):
        return [(key, value) for (key, value) in self.settings.items() if not value is None]

    def set_configuration(self, settings):
        #fetch the settings
        for key, value in settings.items():
            if key in settings:
                self.settings[key] = value

        # now we can configure the module
        if(self.settings[service_g] == 'lastfm' and \
           not self.settings[api_key_g] is None):
            self.api = lastfmapi.LastFmApi(self.settings[api_key_g])
        else:
            raise RuntimeError('Couldn\'t configure the module')

    def set_url_shortener(self, shortener):
        self.shorten_url = shortener.shorten_url

    # Functions that are needed for queries
    def now_playing(self, nick, message, channel):
        # check the message to see if we're being asked for a user different
        # from the user that sent the request
        query = re.sub(r'^\.np', '', message.decode('utf-8'), re.UNICODE).encode('utf-8')
        if query == u'':
            user = nick
        else:
            user = query.strip(u' \t\n\r')

        result = self.__fetch_status(user)

        if result and result != self.last_response:
            self.last_response = result
            return result.encode('utf-8', errors='ignore')

    def __fetch_status(self, luser):
        try:
            response = self.api.user_getRecentTracks(user=luser.encode('utf-8'))
        except lastfmapi.LastFmApiException:
            return u' '.join([luser, u'doesn\'t exist'])

        # if we later cannot find a song, we already know why:
        message = u' '.join([luser, u'isn\'t playing any song'])

        if u'track' in response[u'recenttracks'].keys() and len(response[u'recenttracks'][u'track']) > 0:
            rtrack = response[u'recenttracks'][u'track'][0]

            print (rtrack)
            play_text = u'last played'
            time      = u''
            if u'@attr' in rtrack.keys() and u'nowplaying' in rtrack[u'@attr'].keys():
                play_text = u'is now playing'
            elif u'date' in rtrack.keys() and u'#text' in rtrack[u'date'].keys():
                time = u' on ' + rtrack[u'date'][u'#text']

            if u'name' in rtrack.keys() and \
                 u'artist' in rtrack.keys() and u'#text' in rtrack[u'artist'].keys():
                ltitle = rtrack[u'name']
                lartist = rtrack[u'artist'][u'#text']
            else:
                # we don't have title nor artist!!!
                return message

            loved    = u''
            tag_list = u''

            try:
                #fetch more info avout the track to display more info about it
                response = self.api.track_getInfo(track=ltitle.encode('utf-8'),
                                                  artist=lartist.encode('utf-8'),
                                                  username=luser.encode('utf-8'))

                # see if it's a 'loved' track
                if u'userloved' in response[u'track'].keys() and response[u'track'][u'userloved'] == '1':
                    loved = ' ♥'

                # add the tags of the track
                try:
                    toptags = response[u'track'][u'toptags'][u'tag']
                    tag_list = ' ('
                    for tag in toptags:
                        tag_list += tag[u'name'] + ', '
                    tag_list = tag_list[:-2] + ')'
                except Exception:
                    pass

            except lastfmapi.LastFmApiException:
                pass

            message = u'{user} {done}: {song} — {artist}{heart}{tags}{when}'.format(
                user   = luser,
                done   = play_text,
                song   = ltitle,
                artist = lartist,
                heart  = loved,
                tags   = tag_list,
                when   = time)

        return message
{'fraction_non_alphanumeric': 0.12190402476780186, 'fraction_numerical': 0.0030959752321981426, 'mean_word_length': 1.8832279657865378, 'pattern_counts': {'":': 0, '<': 3, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1638145', 'n_tokens_mistral': 2356, 'n_tokens_neox': 2253, 'n_words': 561}