def retrievequote(bot, trigger): """gets a quote from quotes .quote | random quote .quote (number) | get this number quote .quote (word) | get random quote with this word """ text = trigger.group(2) try: fn = open('quotes.txt', 'r') except: return bot.reply('Please add a quote first.') lines = fn.readlines() if len(lines) < 1: return bot.reply('There are currently no quotes saved.') MAX = len(lines) fn.close() random.seed() randomized = True try: number = int(text) if number < 0: number = MAX - abs(number) + 1 randomized = False except: number = random.randint(1, MAX) if not (0 <= number <= MAX): return bot.reply("I'm not sure which quote you'd like to see") if randomized and text: which = list() for linenumber, line in enumerate(lines): if uc.decode(line).find(text) > -1: which.append(linenumber) if which: number = random.choice(which) number = number + 1 if lines: if number == 1: line = lines[0] elif number == 0: return bot.say('There is no "0th" quote!') else: line = lines[number - 1] bot.say('Quote %s of %s: ' % (number, MAX) + uc.decode(line)) else: bot.reply('There are currently no quotes saved.')
def e(m): entity = m.group() if entity.startswith("&#x"): cp = int(entity[3:-1], 16) meep = unichr(cp) elif entity.startswith("&#"): cp = int(entity[2:-1]) meep = unichr(cp) else: entity_stripped = entity[1:-1] try: char = name2codepoint[entity_stripped] meep = unichr(char) except: if entity_stripped in HTML_ENTITIES: meep = HTML_ENTITIES[entity_stripped] else: meep = str() try: return uc.decode(meep) except: return uc.decode(uc.encode(meep))
def quote(s, safe='/'): # quote('abc def') -> 'abc%20def' # print (s) # s = string.replace(s, 'ä', 'ä') # s = string.replace(s, 'ö', 'ö') s = uc.encode(s) s = uc.decode(s) safe += always_safe safe_map = dict() for i in range(256): c = chr(i) safe_map[c] = (c in safe) and c or ('%%%02X' % i) try: res = map(safe_map.__getitem__, s) except: return '' return ''.join(res)