Beispiel #1
0
def find_title(url):
    """Return the title for the given URL."""
    try:
        content, headers = web.get(url, return_headers=True, limit_bytes=max_bytes)
    except UnicodeDecodeError:
        return # Fail silently when data can't be decoded

    # Some cleanup that I don't really grok, but was in the original, so
    # we'll keep it (with the compiled regexes made global) for now.
    content = title_tag_data.sub(r'<\1title>', content)
    content = quoted_title.sub('', content)

    start = content.find('<title>')
    end = content.find('</title>')
    if start == -1 or end == -1:
        return
    title = web.decode(content[start + 7:end])
    title = title.strip()[:200]

    title = ' '.join(title.split())  # cleanly remove multiple spaces

    # More cryptic regex substitutions. This one looks to be myano's invention.
    title = re_dcc.sub('', title)

    return title or None
Beispiel #2
0
def tr2(bot, trigger):
    """Traduce un texto instantaneamente usando el traductor de Google"""
    if not trigger.group(2):
        return bot.reply('Para usar el traductor ingresa "%tr [idioma origen] [idioma destino] [texto a traducir]"')
    command = trigger.group(2)

    args = command.split(' ')
    phrase = ' '.join(args[2:])

    if (len(phrase) > 350) and (not trigger.admin):
        return bot.reply('La frase debe ser menor a 350 caracteres')

    src = args[0]
    dest = args[1]
    if src != dest:
        msg, src = translate(phrase, src, dest)
        if sys.version_info.major < 3 and isinstance(msg, str):
            msg = msg.decode('utf-8')
        if msg:
            msg = web.decode(msg)  # msg.replace('&#39;', "'")
            msg = '%s' % msg
        else:
            msg = 'La traducción de %s a %s ha fallado, lo siento!' % (src, dest)

        bot.reply(msg)
    else:
        bot.reply('La detección de idioma ha fallado, intenta sugerir uno!')
Beispiel #3
0
def tr(bot, trigger):
    """Translates a phrase, with an optional language hint."""
    in_lang, out_lang, phrase = trigger.groups()

    if (len(phrase) > 350) and (not trigger.admin):
        return bot.reply('Phrase must be under 350 characters.')

    in_lang = in_lang or 'auto'
    out_lang = out_lang or 'en'

    if in_lang != out_lang:
        msg, in_lang = translate(phrase, in_lang, out_lang)
        if sys.version_info.major < 3 and isinstance(msg, str):
            msg = msg.decode('utf-8')
        if msg:
            msg = web.decode(msg)  # msg.replace('&#39;', "'")
            msg = '"%s" (%s to %s, translate.google.com)' % (msg, in_lang,
                                                             out_lang)
        else:
            msg = 'The %s to %s translation failed, sorry!' % (in_lang,
                                                               out_lang)

        bot.reply(msg)
    else:
        bot.reply('Language guessing failed, so try suggesting one!')
Beispiel #4
0
def duck_search(query):
    query = query.replace('!', '')
    uri = 'http://duckduckgo.com/html/?q=%s&kl=uk-en' % query
    bytes = web.get(uri)
    m = r_duck.search(bytes)
    if m:
        return web.decode(m.group(1))
Beispiel #5
0
def tr2(bot, trigger):
    """Translates a phrase, with an optional language hint."""
    command = trigger.group(2).encode('utf-8')

    def langcode(p):
        return p.startswith(':') and (2 < len(p) < 10) and p[1:].isalpha()

    args = ['auto', 'en']

    for i in xrange(2):
        if not ' ' in command:
            break
        prefix, cmd = command.split(' ', 1)
        if langcode(prefix):
            args[i] = prefix[1:]
            command = cmd
    phrase = command

    if (len(phrase) > 350) and (not trigger.admin):
        return bot.reply('Phrase must be under 350 characters.')

    src, dest = args
    if src != dest:
        msg, src = translate(phrase, src, dest)
        if isinstance(msg, str):
            msg = msg.decode('utf-8')
        if msg:
            msg = web.decode(msg)  # msg.replace('&#39;', "'")
            msg = '"%s" (%s to %s, translate.google.com)' % (msg, src, dest)
        else:
            msg = 'The %s to %s translation failed, sorry!' % (src, dest)

        bot.reply(msg)
    else:
        bot.reply('Language guessing failed, so try suggesting one!')
Beispiel #6
0
def tr(bot, trigger):
    """Translates a phrase, with an optional language hint."""
    input, output, phrase = trigger.groups()

    phrase = phrase.encode('utf-8')

    if (len(phrase) > 350) and (not trigger.admin):
        return bot.reply('Phrase must be under 350 characters.')

    input = input or 'auto'
    input = input.encode('utf-8')
    output = (output or 'en').encode('utf-8')

    if input != output:
        msg, input = translate(phrase, input, output)
        if isinstance(msg, str):
            msg = msg.decode('utf-8')
        if msg:
            msg = web.decode(msg)  # msg.replace('&#39;', "'")
            msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output)
        else:
            msg = 'The %s to %s translation failed, sorry!' % (input, output)

        bot.reply(msg)
    else:
        bot.reply('Language guessing failed, so try suggesting one!')
Beispiel #7
0
def duck_search(query):
    query = query.replace('!', '')
    uri = 'http://duckduckgo.com/html/?q=%s&kl=uk-en' % query
    bytes = web.get(uri)
    m = r_duck.search(bytes)
    if m:
        return web.decode(m.group(1))
Beispiel #8
0
def text(html):
    '''html to text dumb converter

    cargo-culted from etymology.py'''
    html = r_tag.sub('', html)
    html = r_whitespace.sub(' ', html)
    return web.decode(html.strip())
Beispiel #9
0
def tr(bot, trigger):
    """Translates a phrase, with an optional language hint."""
    input, output, phrase = trigger.groups()

    phrase = phrase.encode('utf-8')

    if (len(phrase) > 350) and (not trigger.admin):
        return bot.reply('Phrase must be under 350 characters.')

    input = input or 'auto'
    input = input.encode('utf-8')
    output = (output or 'en').encode('utf-8')

    if input != output:
        msg, input = translate(phrase, input, output)
        if isinstance(msg, str):
            msg = msg.decode('utf-8')
        if msg:
            msg = web.decode(msg)  # msg.replace('&#39;', "'")
            msg = '"%s" (%s to %s, translate.google.com)' % (msg, input,
                                                             output)
        else:
            msg = 'The %s to %s translation failed, sorry!' % (input, output)

        bot.reply(msg)
    else:
        bot.reply('Language guessing failed, so try suggesting one!')
Beispiel #10
0
def find_title(url=None, content=None):
    """Return the title for the given URL.

    Copy of find_title that allows for avoiding duplicate requests."""
    if (not content and not url) or (content and url):
        raise ValueError('url *or* content needs to be provided to find_title')
    if url:
        try:
            content, headers = web.get(url,
                                       return_headers=True,
                                       limit_bytes=max_bytes)
        except UnicodeDecodeError:
            return  # Fail silently when data can't be decoded
    assert content

    # Some cleanup that I don't really grok, but was in the original, so
    # we'll keep it (with the compiled regexes made global) for now.
    content = title_tag_data.sub(r'<\1title>', content)
    content = quoted_title.sub('', content)

    start = content.find('<title>')
    end = content.find('</title>')
    if start == -1 or end == -1:
        return
    title = web.decode(content[start + 7:end])
    title = title.strip()[:200]

    title = ' '.join(title.split())  # cleanly remove multiple spaces

    # More cryptic regex substitutions. This one looks to be myano's invention.
    title = re_dcc.sub('', title)

    return title or None
Beispiel #11
0
def text(html):
    '''html to text dumb converter

    cargo-culted from etymology.py'''
    html = r_tag.sub('', html)
    html = r_whitespace.sub(' ', html)
    return web.decode(html.strip())
Beispiel #12
0
def find_title(url=None, content=None):
    """Return the title for the given URL.

    Copy of find_title that allows for avoiding duplicate requests."""
    if (not content and not url) or (content and url):
        raise ValueError('url *or* content needs to be provided to find_title')
    if url:
        try:
            content, headers = web.get(url, return_headers=True, limit_bytes=max_bytes)
        except UnicodeDecodeError:
            return # Fail silently when data can't be decoded
    assert content

    # Some cleanup that I don't really grok, but was in the original, so
    # we'll keep it (with the compiled regexes made global) for now.
    content = title_tag_data.sub(r'<\1title>', content)
    content = quoted_title.sub('', content)

    start = content.find('<title>')
    end = content.find('</title>')
    if start == -1 or end == -1:
        return
    title = web.decode(content[start + 7:end])
    title = title.strip()[:200]

    title = ' '.join(title.split())  # cleanly remove multiple spaces

    # More cryptic regex substitutions. This one looks to be myano's invention.
    title = re_dcc.sub('', title)

    return title or None
Beispiel #13
0
def tr2(bot, trigger):
    """Translates a phrase, with an optional language hint."""
    command = trigger.group(2)

    def langcode(p):
        return p.startswith(':') and (2 < len(p) < 10) and p[1:].isalpha()

    args = ['auto', 'en']

    for i in range(2):
        if ' ' not in command:
            break
        prefix, cmd = command.split(' ', 1)
        if langcode(prefix):
            args[i] = prefix[1:]
            command = cmd
    phrase = command

    if (len(phrase) > 350) and (not trigger.admin):
        return bot.reply('Phrase must be under 350 characters.')

    src, dest = args
    if src != dest:
        msg, src = translate(phrase, src, dest)
        if sys.version_info.major < 3 and isinstance(msg, str):
            msg = msg.decode('utf-8')
        if msg:
            msg = web.decode(msg)  # msg.replace('&#39;', "'")
            msg = '"%s" (%s to %s, translate.google.com)' % (msg, src, dest)
        else:
            msg = 'The %s to %s translation failed, sorry!' % (src, dest)

        bot.reply(msg)
    else:
        bot.reply('Language guessing failed, so try suggesting one!')
Beispiel #14
0
def duck_search(query):
    query = query.replace('!', '')
    uri = 'http://duckduckgo.com/html/?q=%s&kl=uk-en' % query
    bytes = web.get(uri)
    if 'web-result"' in bytes:  #filter out the adds on top of the page
        bytes = bytes.split('web-result"')[1]
    m = r_duck.search(bytes)
    if m:
        return web.decode(m.group(1))
Beispiel #15
0
def duck_search(query):
    query = query.replace('!', '')
    uri = 'http://duckduckgo.com/html/?q=%s&kl=uk-en' % query
    bytes = web.get(uri)
    if 'web-result"' in bytes: #filter out the adds on top of the page
        bytes = bytes.split('web-result"')[1]
    m = r_duck.search(bytes)
    if m:
        return web.decode(m.group(1))
Beispiel #16
0
def fucking_dinner(bot, trigger):
    txt = trigger.group(2)
    url = 'http://www.whatthefuckshouldimakefordinner.com'
    if txt == '-v':
        url = 'http://whatthefuckshouldimakefordinner.com/veg.php'
    page = web.decode(web.get(url))
    re_mark = re.compile('<dt><a href="(.*?)" target="_blank">(.*?)</a></dt>')
    results = re_mark.findall(page)
    if results:
        bot.say("WHY DON'T YOU EAT SOME F*****G: " + results[0][1] +
                  " HERE IS THE RECIPE: " + results[0][0])
    else:
        bot.say("I DON'T F*****G KNOW, EAT PIZZA.")
Beispiel #17
0
def fucking_weather(bot, trigger):
    text = trigger.group(2)
    if not text:
        bot.reply("INVALID F*****G PLACE. PLEASE ENTER A F*****G ZIP CODE, OR A F*****G CITY-STATE PAIR.")
        return
    text = web.quote(text)
    try:
        page = web.decode(web.get("http://thefuckingweather.com/?where=%s" % (text)))
    except:
        bot.reply("I COULDN'T ACCESS THE F*****G SITE.")
        return

    re_mark = re.compile('<p class="remark">(.*?)</p>')
    re_temp = re.compile('<span class="temperature" tempf="\S+">(\S+)</span>')
    re_condition = re.compile('<p class="large specialCondition">(.*?)</p>')
    re_flavor = re.compile('<p class="flavor">(.*?)</p>')
    re_location = re.compile('<span id="locationDisplaySpan" class="small">(.*?)</span>')

    temps = re_temp.findall(page)
    remarks = re_mark.findall(page)
    conditions = re_condition.findall(page)
    flavor = re_flavor.findall(page)
    new_location = re_location.findall(page)

    response = str()

    if new_location and new_location[0]:
        response += new_location[0] + ': '

    if temps:
        tempf = float(temps[0])
        tempc = (tempf - 32.0) * (5 / 9.0)
        response += u'%.1f°F?! %.1f°C?! ' % (tempf, tempc)

    if remarks:
        response += remarks[0]
    else:
        response += "THE F*****G SITE DOESN'T CONTAIN ANY F*****G INFORMATION ABOUT THE F*****G WEATHER FOR THE PROVIDED F*****G LOCATION."

    if conditions:
        response += ' ' + conditions[0]

    if flavor:
        response += ' -- ' + flavor[0].replace('  ', ' ')

    bot.reply(response)
Beispiel #18
0
def reddit(bot, trigger):
    """Display posts from reddit"""

    args = trigger.group(2)

    if not args:
        bot.reply('no, vieja: .reddit subreddit [cantidad]')
        return

    args = args.split(' ')

    subreddit = args[0]

    if len(args) == 1:
        limit = 1
    else:
        try:
            limit = int(args[1])
        except ValueError:
            limit = 1

    if limit > 4:
        limit = 4

    r = praw.Reddit(user_agent='Trolo Bot (praw)')

    try:
        subr = r.get_subreddit(subreddit, fetch=True)
    except InvalidSubreddit:
        bot.reply('rofl, ese subreddit no existe, locura!')
        return

    results = subr.get_hot(limit=limit)

    out = 'r/%(subreddit)s: %(title)s [%(ups)s/%(downs)s] %(url)s [%(short_link)s]'
    for r in results:
        output = out % {
            'subreddit': r.subreddit,
            'title': decode(r.title),
            'ups': r.ups,
            'downs': r.downs,
            'url': r.url,
            'short_link': r.short_link
        }
        bot.say(output)
Beispiel #19
0
def calculate(trigger):
    q = trigger.encode('utf-8')
    q = q.replace('\xcf\x95', 'phi') # utf-8 U+03D5
    q = q.replace('\xcf\x80', 'pi') # utf-8 U+03C0
    uri = 'http://www.google.com/ig/calculator?q='
    bytes = web.get(uri + web.quote(q))
    parts = bytes.split('",')
    answer = [p for p in parts if p.startswith('rhs: "')][0][6:]
    if answer:
        answer = answer.decode('unicode-escape')
        answer = ''.join(chr(ord(c)) for c in answer)
        answer = answer.decode('utf-8')
        answer = answer.replace(u'\xc2\xa0', ',')
        answer = answer.replace('<sup>', '^(')
        answer = answer.replace('</sup>', ')')
        answer = web.decode(answer)
        return answer
    else: return 'Sorry, no result.'
Beispiel #20
0
def calculate(q):
    q = q.encode('utf8')
    q = q.replace('\xcf\x95', 'phi')  # utf-8 U+03D5
    q = q.replace('\xcf\x80', 'pi')  # utf-8 U+03C0
    uri = 'http://www.google.com/ig/calculator?q='
    bytes = web.get(uri + web.quote(q))
    parts = bytes.split('",')
    answer = [p for p in parts if p.startswith('rhs: "')][0][6:]
    if answer:
        answer = answer.decode('unicode-escape')
        answer = ''.join(chr(ord(c)) for c in answer)
        answer = answer.decode('utf-8')
        answer = answer.replace(u'\xc2\xa0', ',')
        answer = answer.replace('<sup>', '^(')
        answer = answer.replace('</sup>', ')')
        answer = web.decode(answer)
        return answer
    else:
        return 'Sorry, no result.'
Beispiel #21
0
def find_title(url):
    """Return the title for the given URL."""
    content, headers = web.get(url,
                               return_headers=True,
                               limit_bytes=max_bytes,
                               dont_decode=True,
                               only_200=True)
    if not content:
        return

    content_type = headers.get('Content-Type') or ''
    encoding_match = re.match('.*?charset *= *(\S+)', content_type)
    # If they gave us something else instead, try that
    if encoding_match:
        try:
            content = content.decode(encoding_match.group(1))
        except:
            encoding_match = None
    # They didn't tell us what they gave us, so go with UTF-8 or fail silently.
    if not encoding_match:
        try:
            content = content.decode('utf-8')
        except:
            return

    # Some cleanup that I don't really grok, but was in the original, so
    # we'll keep it (with the compiled regexes made global) for now.
    content = title_tag_data.sub(r'<\1title>', content)
    content = quoted_title.sub('', content)

    start = content.find('<title>')
    end = content.find('</title>')
    if start == -1 or end == -1:
        return
    title = web.decode(content[start + 7:end])
    title = title.strip()[:200]

    title = ' '.join(title.split())  # cleanly remove multiple spaces

    # More cryptic regex substitutions. This one looks to be myano's invention.
    title = re_dcc.sub('', title)

    return title or None
Beispiel #22
0
def find_title(url):
    """Return the title for the given URL."""

    content, headers = web.get(url, return_headers=True, limit_bytes=max_bytes)

    if content == None:
        #If it cant find any encoding, it could be an image or other file. Show filetype and size.

        type = str(headers.get('content-type'))
        size = str(headers.get('content-length'))
        if size == "None":
            size = "Unknown"

        elif len(size) > 9:
            size = "~"+size[0:-9] +" GB"
        elif len(size) > 6:
            size = "~"+size[0:-6] +" MB"
        elif len(size) > 3:
            size = "~"+size[0:-3] +" KB"

        title = "Filetype: "+type+". Filesize: "+size+"."

        return title


    # Some cleanup that I don't really grok, but was in the original, so
    # we'll keep it (with the compiled regexes made global) for now.
    content = title_tag_data.sub(r'<\1title>', content)
    content = quoted_title.sub('', content)
    start = content.find('<title>')
    end = content.find('</title>')
    if start == -1 or end == -1:
        return
    title = web.decode(content[start + 7:end])
    title = title.strip()[:200]

    title = ' '.join(title.split())  # cleanly remove multiple spaces

    # More cryptic regex substitutions. This one looks to be myano's invention.
    title = re_dcc.sub('', title)

    return title or None
Beispiel #23
0
def tr2(bot, trigger):
    """Translates a phrase, with an optional language hint."""
    command = trigger.group(2)

    if not command:
        return bot.reply("You did not give me anything to translate")

    def langcode(p):
        return p.startswith(":") and (2 < len(p) < 10) and p[1:].isalpha()

    args = ["auto", "en"]

    for i in range(2):
        if " " not in command:
            break
        prefix, cmd = command.split(" ", 1)
        if langcode(prefix):
            args[i] = prefix[1:]
            command = cmd
    phrase = command

    if (len(phrase) > 350) and (not trigger.admin):
        return bot.reply("Phrase must be under 350 characters.")

    src, dest = args
    if src != dest:
        msg, src = translate(phrase, src, dest)
        if sys.version_info.major < 3 and isinstance(msg, str):
            msg = msg.decode("utf-8")
        if msg:
            msg = web.decode(msg)  # msg.replace('&#39;', "'")
            msg = '"%s" (%s to %s, translate.google.com)' % (msg, src, dest)
        else:
            msg = "The %s to %s translation failed, sorry!" % (src, dest)

        bot.reply(msg)
    else:
        bot.reply("Language guessing failed, so try suggesting one!")
Beispiel #24
0
def tr(bot, trigger):
    """Translates a phrase, with an optional language hint."""
    in_lang, out_lang, phrase = trigger.groups()

    if (len(phrase) > 350) and (not trigger.admin):
        return bot.reply('Phrase must be under 350 characters.')

    in_lang = in_lang or 'auto'
    out_lang = out_lang or 'en'

    if in_lang != out_lang:
        msg, in_lang = translate(phrase, in_lang, out_lang)
        if sys.version_info.major < 3 and isinstance(msg, str):
            msg = msg.decode('utf-8')
        if msg:
            msg = web.decode(msg)  # msg.replace('&#39;', "'")
            msg = '"%s" (%s to %s, translate.google.com)' % (msg, in_lang, out_lang)
        else:
            msg = 'The %s to %s translation failed, sorry!' % (in_lang, out_lang)

        bot.reply(msg)
    else:
        bot.reply('Language guessing failed, so try suggesting one!')
Beispiel #25
0
def find_title(url):
    """Return the title for the given URL."""
    content, headers = web.get(url, return_headers=True, limit_bytes=max_bytes,
                               dont_decode=True)
    content_type = headers.get('Content-Type') or ''
    encoding_match = re.match('.*?charset *= *(\S+)', content_type)
    # If they gave us something else instead, try that
    if encoding_match:
        try:
            content = content.decode(encoding_match.group(1))
        except:
            encoding_match = None
    # They didn't tell us what they gave us, so go with UTF-8 or fail silently.
    if not encoding_match:
        try:
            content = content.decode('utf-8')
        except:
            return

    # Some cleanup that I don't really grok, but was in the original, so
    # we'll keep it (with the compiled regexes made global) for now.
    content = title_tag_data.sub(r'<\1title>', content)
    content = quoted_title.sub('', content)

    start = content.find('<title>')
    end = content.find('</title>')
    if start == -1 or end == -1:
        return
    title = web.decode(content[start + 7:end])
    title = title.strip()[:200]

    title = ' '.join(title.split())  # cleanly remove multiple spaces

    # More cryptic regex substitutions. This one looks to be myano's invention.
    title = re_dcc.sub('', title)

    return title or None