Beispiel #1
0
def apertium_generate(phenny, input):
    """Use Apertium APY's generate functionality"""
    lang, text = input.groups()

    opener = urllib.request.build_opener()
    opener.addheaders = headers

    constructed_url = APIanalyseURL + '/generate?lang=' + web.quote(lang)
    constructed_url += '&q=' + web.quote(text.strip())

    try:
        response = opener.open(constructed_url).read()
    except urllib.error.HTTPError as error:
        response = error.read()
        jobj = json.loads(response.decode('utf-8'))
        if 'explanation' in jobj:
            phenny.say('The following error occurred: ' + jobj['explanation'])
        else:
            phenny.say('An error occurred: ' + str(error))
        return

    jobj = json.loads(response.decode('utf-8'))
    messages = []
    for generation, original in jobj:
        messages.append(original + " → " + generation)

    more.add_messages(input.nick, phenny,
                      "\n".join(messages),
                      break_up=lambda x, y: x.split('\n'))
Beispiel #2
0
def randquote_fetcher(phenny, topic, to_user):

    # create opener
    opener = urllib.request.build_opener()
    opener.addheaders = [
        ('User-agent', web.Grab().version),
        ('Referer', "http://quotes.firespeaker.org/"),
    ]

    try:
        if topic == "" or topic == None:
            req = opener.open("http://quotes.firespeaker.org/random.php")
        else:
            req = opener.open(
                "http://quotes.firespeaker.org/random.php?topic=%s" %
                (web.quote(topic)))
        data = req.read().decode('utf-8')
        data = json.loads(data)
    except (HTTPError, IOError, ValueError):
        raise GrumbleError("Firespeaker.org down?  Try again later.")

    if len(data) == 0:
        phenny.say("No results found")
        return

    #result = data['list'][0]
    #url = 'http://www.urbandictionary.com/define.php?term={0}'.format(web.quote(word))
    #
    #response = "{0} - {1}".format(result['definition'].strip()[:256], url)

    if data['quote'] != None:
        quote = data['quote'].replace('</p>', '').replace('<p>', '').replace(
            '<em>', '_').replace('</em>', '_').replace('&mdash;', '—')
        response = data['short_url'] + ' - ' + quote
    else:
        phenny.say("Sorry, no quotes returned!")
        return

    more.add_messages(to_user,
                      phenny,
                      response,
                      break_up=(lambda x, y: x.split('\n')))
Beispiel #3
0
def randquote_fetcher(phenny, topic, to_user):

    # create opener
    opener = urllib.request.build_opener()
    opener.addheaders = [
        ('User-agent', web.Grab().version),
        ('Referer', "http://quotes.firespeaker.org/"),
    ]

    try:
        if topic == "" or topic==None:
            req = opener.open("http://quotes.firespeaker.org/random.php")
        else:
            req = opener.open("http://quotes.firespeaker.org/random.php?topic=%s" % (web.quote(topic)))
        data = req.read().decode('utf-8')
        data = json.loads(data)
    except (HTTPError, IOError, ValueError):
        raise GrumbleError(
                "Firespeaker.org down?  Try again later.")

    if len(data) == 0:
        phenny.say("No results found")
        return

    #result = data['list'][0]
    #url = 'http://www.urbandictionary.com/define.php?term={0}'.format(web.quote(word))
    #
    #response = "{0} - {1}".format(result['definition'].strip()[:256], url)

    if data['quote'] != None:
        quote = data['quote'].replace('</p>', '').replace('<p>', '').replace('<em>', '_').replace('</em>', '_').replace('&mdash;', '—')
        response = data['short_url'] + ' - ' + quote
    else:
        phenny.say("Sorry, no quotes returned!")
        return

    more.add_messages(to_user, phenny, response, break_up=(lambda x, y: x.split('\n')))
Beispiel #4
0
def queue(phenny, raw):
    """.queue- queue management."""
    if raw.group(1):
        command = raw.group(1)
        if command.lower() == 'display':
            search = raw.group(2)
            if search:
                queue_names = disambiguate_name(phenny.queue_data, search)
                if type(queue_names) is str:
                    #there was only one possible queue
                    more.add_messages(raw.nick, phenny, print_queue(queue_names, phenny.queue_data[queue_names]))
                elif len(queue_names) > 0:
                    queue_exact = list(queue_names)
                    for q in queue_names:
                        if q.split(':')[0] == raw.nick and q[len(raw.nick)+1:] == search:
                            #current user owns queue with exact name
                            more.add_messages(raw.nick, phenny, print_queue(q, phenny.queue_data[q]))
                            return
                        elif q[q.find(':')+1:] != search:
                            #filter queues with exact name
                            queue_exact.remove(q)
                    if len(queue_exact) == 1:
                        #only one user owns queue with exact name
                        more.add_messages(raw.nick, phenny, print_queue(queue_exact[0], phenny.queue_data[queue_exact[0]]))
                    elif len(queue_exact) > 1:
                        #more users own queues with exact name
                        phenny.reply('Did you mean: ' + ', '.join(queue_exact) + '?')
                    else:
                        #the name was ambiguous, show a list of queues
                        phenny.reply('Did you mean: ' + ', '.join(queue_names) + '?')
                else:
                    phenny.reply('No queues found.')
            else:
                #there was no queue name given, display all of their names
                if phenny.queue_data:
                    phenny.reply('Avaliable queues: ' + ', '.join(sorted(phenny.queue_data.keys())))
                else:
                    phenny.reply('There are no queues to display.')

        elif command.lower() == 'new':
            if raw.group(2):
                queue_name = raw.nick + ':' + raw.group(2)
                owner = raw.nick
                if queue_name not in phenny.queue_data:
                    if raw.group(3):
                        queue = raw.group(3).split(',')
                        queue = list(map(lambda x: x.strip(), queue))
                        phenny.queue_data[queue_name] = {'owner': owner, 'queue': queue}
                        write_dict(filename(phenny), phenny.queue_data)
                        phenny.reply('Queue {} with items {} created.'.format(
                            queue_name, ', '.join(queue)))
                    else:
                        phenny.queue_data[queue_name] = {'owner': owner, 'queue': []}
                        write_dict(filename(phenny), phenny.queue_data)
                        phenny.reply('Empty queue {} created.'.format(queue_name))
                else:
                    phenny.reply('You already have a queue with that name! Pick a new name or delete the old one.')
            else:
                phenny.reply('Syntax: .queue new <name> <item1>, <item2> ...')

        elif command.lower() in ['delete', 'remove', 'del', 'rm']:
            if raw.group(2):
                queue_name, queue = get_queue(phenny.queue_data, raw.group(2), raw.nick)
                if type(queue_name) is str:
                    if raw.nick == queue['owner'] or raw.admin:
                        phenny.queue_data.pop(queue_name)
                        write_dict(filename(phenny), phenny.queue_data)
                        phenny.reply('Queue {} deleted.'.format(queue_name))
                    else:
                        phenny.reply('You aren\'t authorized to do that!')
                else:
                    phenny.reply('That queue wasn\'t found!')
            else:
                phenny.reply('Syntax: .queue delete <name>')

        elif get_queue(phenny.queue_data, raw.group(1), raw.nick)[0]:
            #queue-specific commands
            queue_name, queue = get_queue(phenny.queue_data, raw.group(1), raw.nick)
            if raw.group(2):
                command = raw.group(2).lower()
                if queue['owner'] == raw.nick or raw.admin:
                    if command == 'add':
                        if raw.group(3):
                            new_queue = raw.group(3).split(',')
                            new_queue = list(map(lambda x: x.strip(), new_queue))
                            queue['queue'] += new_queue
                            write_dict(filename(phenny), phenny.queue_data)
                            more.add_messages(raw.nick, phenny, print_queue(queue_name, queue))
                        else:
                            phenny.reply('Syntax: .queue <name> add <item1>, <item2> ...')
                    elif command == 'swap':
                        if raw.group(3):
                            indices = raw.group(3).split(',')
                            try:
                                id1, id2 = tuple(map(lambda x: int(x.strip()), indices))[:2]
                            except ValueError:
                                q1, q2 = tuple(map(lambda x: x.strip(), indices))[:2]
                                id1 = search_queue(queue['queue'], q1)
                                if id1 is None:
                                    phenny.reply('{} not found in {}'.format(indices[0].strip(), queue_name))
                                    return
                                id2 = search_queue(queue['queue'], q2)
                                if id2 is None:
                                    phenny.reply('{} not found in {}'.format(indices[1].strip(), queue_name))
                                    return
                            queue['queue'][id1], queue['queue'][id2] = queue['queue'][id2], queue['queue'][id1]
                            write_dict(filename(phenny), phenny.queue_data)
                            more.add_messages(raw.nick, phenny, print_queue(queue_name, queue))
                        else:
                            phenny.reply('Syntax: .queue <name> swap <index/item1>, <index/item2>')
                    elif command in ['move', 'mv']:
                        if raw.group(3) and ',' in raw.group(3):
                            indices = raw.group(3).split(',')
                            try:
                                id1, id2 = tuple(map(lambda x: int(x.strip()), indices))[:2]
                            except ValueError:
                                q1, q2 = tuple(map(lambda x: x.strip(), indices))[:2]
                                id1 = search_queue(queue['queue'], q1)
                                if id1 is None:
                                    phenny.reply('{} not found in {}'.format(indices[0].strip(), queue_name))
                                    return
                                id2 = search_queue(queue['queue'], q2)
                                if id2 is None:
                                    phenny,reply('{} not found in {}'.format(indices[1].strip(), queue_name))
                                    return
                            #queue['queue'][id2 + (-1 if id1 < id2 else 0)] = queue['queue'].pop(id1)
                            queue['queue'].insert(id2, queue['queue'].pop(id1))
                            write_dict(filename(phenny), phenny.queue_data)
                            more.add_messages(raw.nick, phenny, print_queue(queue_name, queue))
                        else:
                            phenny.reply('Syntax: .queue <name> move <source_index/item>, <target_index/item>')
                    elif command == 'replace':
                        if raw.group(3) and ',' in raw.group(3):
                            old, new = raw.group(3).split(',')
                            old = old.strip()
                            try:
                                old_id = int(old)
                            except ValueError:
                                old_id = search_queue(queue['queue'], old)
                                if old_id is None:
                                    phenny.reply('{} not found in {}'.format(old, queue_name))
                                    return
                            queue['queue'][old_id] = new.strip()
                            write_dict(filename(phenny), phenny.queue_data)
                            more.add_messages(raw.nick, phenny, print_queue(queue_name, queue))
                        else:
                            phenny.reply('Syntax: .queue <name> replace <index/item>, <new_item>')
                    elif command in ['remove', 'delete', 'del', 'rm']:
                        if raw.group(3):
                            item = raw.group(3)
                            if item in queue['queue']:
                                queue['queue'].remove(item)
                                write_dict(filename(phenny), phenny.queue_data)
                                more.add_messages(raw.nick, phenny, print_queue(queue_name, queue))
                            elif search_queue(queue['queue'], item):
                                queue['queue'].pop(search_queue(queue['queue'], item))
                                write_dict(filename(phenny), phenny.queue_data)
                                more.add_messages(raw.nick, phenny, print_queue(queue_name, queue))
                            else:
                                phenny.reply('{} not found in {}'.format(item, queue_name))
                        else:
                            phenny.reply('Syntax: .queue <name> remove <item>')
                    elif command == 'pop':
                        try:
                            queue['queue'].pop(0)
                            write_dict(filename(phenny), phenny.queue_data)
                            more.add_messages(raw.nick, phenny, print_queue(queue_name, queue))
                        except IndexError:
                            phenny.reply('That queue is already empty.')
                    elif command == 'random':
                        mphenny.reply('%s is the lucky one.' % repr(random.choice(queue['queue'])))
                    elif command == 'reassign':
                        if raw.group(3):
                            new_owner = raw.group(3)
                            new_queue_name = new_owner + queue_name[queue_name.index(':'):]
                            phenny.queue_data.pop(queue_name)
                            phenny.queue_data[new_queue_name] = {'owner': new_owner, 'queue': queue['queue']}
                            write_dict(filename(phenny), phenny.queue_data)
                            more.add_messages(raw.nick, phenny, print_queue(new_queue_name, queue))
                        else:
                            phenny.reply('Syntax: .queue <name> reassign <nick>')
                    elif command.lower() in ['rename', 'ren']:
                        if raw.group(3):
                            new_queue_name = queue['owner'] + ':' + raw.group(3)
                            phenny.queue_data[new_queue_name] = phenny.queue_data.pop(queue_name)
                            write_dict(filename(phenny), phenny.queue_data)
                            more.add_messages(raw.nick, phenny, print_queue(new_queue_name, queue))
                        else:
                            phenny.reply('Syntax: .queue <name> rename <new_name>')
                elif command == 'random':
                    phenny.reply('%s is the lucky one.' % repr(random.choice(queue['queue'])))
                else:
                    phenny.reply('You aren\'t the owner of this queue!')
            else:
                more.add_messages(raw.nick, phenny, print_queue(queue_name, queue))
        else:
            if raw.group(3):
                phenny.reply('That\'s not a command. Commands: ' + commands)
            else:
                phenny.reply('That queue wasn\'t found!')
    else:
        phenny.reply('Commands: ' + commands)