def compare(self, cardinal, user, channel, msg):
        # Before we do anything, let's make sure we'll be able to query Last.fm
        if self.api_key is None:
            cardinal.sendMsg(
                channel,
                "Last.fm plugin is not configured correctly. "
                "Please set API key."
            )
            self.logger.error(
                "Attempt to compare users failed, API key not set"
            )
            return

        if not self.conn:
            cardinal.sendMsg(
                channel,
                "Unable to access local Last.fm database."
            )
            self.logger.error(
                "Attempt to compare users failed, no database connection"
            )
            return

        # Open the cursor for the query to find a saved Last.fm username
        c = self.conn.cursor()

        # If they supplied user parameter, use that for the query instead
        message = msg.split()

        if message[0] != '.compare':
            message.pop(0)

        if len(message) < 2:
            cardinal.sendMsg(channel, "Syntax: .compare <username> [username]")

        nick = message[1]
        c.execute("SELECT username FROM users WHERE nick=?", (nick,))
        result = c.fetchone()

        if not result:
            username1 = nick
        else:
            username1 = result[0]

        if len(message) >= 3:
            nick = message[2]
            c.execute("SELECT username FROM users WHERE nick=?", (nick,))
        else:
            nick = user.group(1)
            vhost = user.group(3)
            c.execute(
                "SELECT username FROM users WHERE nick=? OR vhost=?",
                (nick, vhost)
            )
        result = c.fetchone()

        # Use the returned username, or the entered/user's nick otherwise
        if not result:
            username2 = nick
        else:
            username2 = result[0]

        try:
            uh = urllib2.urlopen(
                "http://ws.audioscrobbler.com/2.0/?method=tasteometer.compare"
                "&type1=user&type2=user&value1=%s&value2=%s&api_key=%s"
                "&format=json" % (username1, username2, self.api_key))
            content = json.load(uh)
        except Exception:
            cardinal.sendMsg(channel, "Unable to connect to Last.fm.")
            self.logger.exception("Failed to connect to Last.fm")
            return

        if 'error' in content and content['error'] == 10:
            cardinal.sendMsg(
                channel,
                "Last.fm plugin is not configured. Please set API key."
            )
            self.logger.error(
                "Attempt to compare users failed, API key incorrect"
            )
            return
        elif 'error' in content and content['error'] == 7:
            cardinal.sendMsg(
                channel,
                "One of the Last.fm usernames was invalid. Please try again."
            )
            return

        try:
            result = content['comparison']['result']

            score = int(float(result['score']) * 100)
            artists = []
            if 'artist' not in result['artists']:
                # Return early to avoid error on looping through artists
                cardinal.sendMsg(
                    channel,
                    "According to Last.fm's Tasteometer, %s and %s share none "
                    "of the same music." %
                    (str(username1), str(username2))
                )
                return

            # Account for Last.fm giving a string instead of a list if only
            # one artist is shared
            if not isinstance(result['artists']['artist'], list):
                artists.append(str(result['artists']['artist']['name']))
            else:
                # Loop through all artists to grab artist names
                for i in range(len(result['artists']['artist'])):
                    artists.append(str(result['artists']['artist'][i]['name']))

            cardinal.sendMsg(
                channel,
                "According to Last.fm's Tasteometer, %s and %s's music "
                "preferences are %d%% compatible! Some artists they have in "
                "common include: %s" %
                (str(username1), str(username2), score, ', '.join(artists))
            )
        except __HOLE__:
            cardinal.sendMsg(channel, "An unknown error has occurred.")
            self.logger.exception("An unknown error occurred comparing users")
dataset/ETHPy150Open JohnMaguire/Cardinal/plugins/lastfm/plugin.py/LastfmPlugin.compare