Beispiel #1
0
def load_db():
    """ load lines from find.txt to search_dict """
    if not os.path.isfile("find.txt"):
        f = open("find.txt", "w")
        f.write("#test,yano,foobar\n")
        f.close()
    search_file = open("find.txt", "r")
    lines = search_file.readlines()
    search_file.close()
    search_dict = dict()
    for line in lines:
        line = uc.decode(line)
        line = uc.encode(line)
        a = line.replace(r'\n', '')
        new = a.split(r',')
        if len(new) < 3: continue
        channel = uc.encode(new[0])
        nick = new[1]
        if len(new) < 2: continue
        if channel not in search_dict:
            search_dict[channel] = dict()
        if nick not in search_dict[channel]:
            search_dict[channel][nick] = list()
        if len(new) > 3:
            result = ",".join(new[2:])
            result = result.replace('\n', '')
        elif len(new) == 3:
            result = new[-1]
            if len(result) > 0:
                result = result[:-1]
        if result:
            search_dict[channel][nick].append(uc.decode(result))
    return search_dict
Beispiel #2
0
def load_db():
    """ load lines from find.txt to search_dict """
    if not os.path.isfile("find.txt"):
        f = open("find.txt", "w")
        f.write("#test,yano,foobar\n")
        f.close()
    search_file = open("find.txt", "r")
    lines = search_file.readlines()
    search_file.close()
    search_dict = dict()
    for line in lines:
        line = uc.decode(line)
        line = uc.encode(line)
        a = line.replace(r'\n', '')
        new = a.split(r',')
        if len(new) < 3: continue
        channel = uc.encode(new[0])
        nick = new[1]
        if len(new) < 2: continue
        if channel not in search_dict:
            search_dict[channel] = dict()
        if nick not in search_dict[channel]:
            search_dict[channel][nick] = list()
        if len(new) > 3:
            result = ",".join(new[2:])
            result = result.replace('\n','')
        elif len(new) == 3:
            result = new[-1]
            if len(result) > 0:
                result = result[:-1]
        if result:
            search_dict[channel][nick].append(uc.decode(result))
    return search_dict
Beispiel #3
0
    def rmuser(self, jenni, input, line):
        if not input.admin:
            return
        if len(line) < 9:
            jenni.reply("No input provided.")
            return
        line = line[8:].split()
        channel = uc.encode((input.sender).lower())
        nick = uc.encode(line[0]).lower()

        def check(nick, channel):
            nick = nick.lower()
            channel = channel.lower()
            if channel in self.scores_dict:
                if nick in self.scores_dict[channel]:
                    del self.scores_dict[channel][nick]
                    return self.STRINGS["rmuser"].format(nick, channel)
                else:
                    return self.STRINGS["nouser"].format(nick, channel)
            else:
                return self.STRINGS["nochan"].format(channel)

        if len(line) == 1:
            ## .rmuser <nick>
            result = check(nick, (input.sender).lower())
            self.save()
        elif len(line) == 2:
            ## .rumser <channel> <nick>
            result = check(line[1], nick)
            self.save()

        jenni.say(result)
Beispiel #4
0
    def setpoint(self, jenni, input, line):
        if not input.admin:
            return
        line = line[10:].split()
        if len(line) != 4:
            return
        channel = uc.encode(line[0]).lower()
        nick = uc.encode(line[1]).lower()
        try:
            add = int(line[2])
            sub = int(line[3])
        except:
            jenni.say(self.STRINGS["invalid"])
            return

        if add < 0 or sub < 0:
            jenni.reply("You are doing it wrong.")
            return

        if channel not in self.scores_dict:
            self.scores_dict[channel] = dict()

        self.scores_dict[channel][nick] = [int(add), int(sub)]
        self.save()
        jenni.say(self.str_score(nick, channel))
Beispiel #5
0
    def rmuser(self, jenni, input, line):
        if not input.admin:
            return
        if len(line) < 9:
            jenni.reply("No input provided.")
            return
        line = line[8:].split()
        channel = uc.encode((input.sender).lower())
        nick = uc.encode(line[0]).lower()

        def check(nick, channel):
            nick = nick.lower()
            channel = channel.lower()
            if channel in self.scores_dict:
                if nick in self.scores_dict[channel]:
                    del self.scores_dict[channel][nick]
                    return self.STRINGS["rmuser"].format(nick, channel)
                else:
                    return self.STRINGS["nouser"].format(nick, channel)
            else:
                return self.STRINGS["nochan"].format(channel)

        if len(line) == 1:
            ## .rmuser <nick>
            result = check(nick, (input.sender).lower())
            self.save()
        elif len(line) == 2:
            ## .rumser <channel> <nick>
            result = check(line[1], nick)
            self.save()

        jenni.say(result)
Beispiel #6
0
    def setpoint(self, jenni, input, line):
        if not input.admin:
            return
        line = line[10:].split()
        if len(line) != 4:
            return
        channel = uc.encode(line[0]).lower()
        nick = uc.encode(line[1]).lower()
        try:
            add = int(line[2])
            sub = int(line[3])
        except:
            jenni.say(self.STRINGS["invalid"])
            return

        if add < 0 or sub < 0:
            jenni.reply("You are doing it wrong.")
            return

        if channel not in self.scores_dict:
            self.scores_dict[channel] = dict()

        self.scores_dict[channel][nick] = [int(add), int(sub)]
        self.save()
        jenni.say(self.str_score(nick, channel))
Beispiel #7
0
def delquote(jenni, input):
    '''.rmquote <number> -- removes a given quote from the database. Can only be done by the owner of the bot.'''
    if not input.owner: return
    text = input.group(2)
    number = int()
    try:
        fn = open('quotes.txt', 'r')
    except:
        return jenni.reply('No quotes to delete.')
    lines = fn.readlines()
    MAX = len(lines)
    fn.close()
    try:
        number = int(text)
    except:
        jenni.reply('Please enter the quote number you would like to delete.')
        return
    newlines = lines[:number - 1] + lines[number:]
    fn = open('quotes.txt', 'w')
    for line in newlines:
        txt = uc.encode(line)
        if txt:
            fn.write(txt)
            if txt[-1] != '\n':
                fn.write('\n')
    fn.close()
    jenni.reply('Successfully deleted quote %s.' % (number))
Beispiel #8
0
def get_results(text):
    if not text:
        return list()
    a = re.findall(url_finder, text)
    k = len(a)
    i = 0
    display = list()
    passs = False
    while i < k:
        url = uc.encode(a[i][0])
        url = uc.decode(url)
        url = uc.iriToUri(url)
        url = remove_nonprint(url)
        domain = getTLD(url)
        if '//' in domain:
            domain = domain.split('//')[1]
        if not url.startswith(EXCLUSION_CHAR):
            passs, page_title = find_title(url)
            if bitly_loaded:
                bitly = short(url)
                bitly = bitly[0][1]
            else:
                bitly = url
            display.append([page_title, url, bitly])
        i += 1
    return passs, display
Beispiel #9
0
def collectlines(jenni, input):
    """Creates a temporary storage of most recent lines for s///"""
    # don't log things in PM
    #channel = (input.sender).encode("utf-8")
    channel = uc.decode(input.sender)
    channel = uc.encode(channel)
    nick = (input.nick).encode("utf-8")
    if not channel.startswith('#'): return
    search_dict = load_db()
    if channel not in search_dict:
        search_dict[channel] = dict()
    if nick not in search_dict[channel]:
        search_dict[channel][nick] = list()
    templist = search_dict[channel][nick]
    line = input.group()
    if line.startswith("s/"):
        return
    elif line.startswith("\x01ACTION"):
        line = line[:-1]
        templist.append(line)
    else:
        templist.append(line)
    del templist[:-10]
    search_dict[channel][nick] = templist
    save_db(search_dict)
Beispiel #10
0
def fucking_weather(jenni, input):
    """.fw (ZIP|City, State) -- provide a ZIP code or a city state pair to hear about the f*****g weather"""
    ## thefuckingweather.com website is not very reliable, in fat this often breaks due to their site
    ## not returning correct data

    text = input.group(2)

    if not text:
        jenni.reply('INVALID F*****G INPUT. PLEASE ENTER A F*****G ZIP CODE, OR A F*****G CITY-STATE PAIR.')
        return

    new_text = str()

    new_text = uc.encode(text)
    search = urllib.quote((new_text).strip())

    url = 'http://thefuckingweather.com/?where=%s' % (search)

    try:
        page = web.get(url)
    except:
        return jenni.say("I COULDN'T ACCESS THE F*****G SITE.")

    ## hacky, yes, I know, but the position of this information has yet to change
    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>')

    ## find relevant information
    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)

    ## build return string
    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. F**K!"

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

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

    jenni.say(response)
Beispiel #11
0
def collectlines(jenni, input):
    """Creates a temporary storage of most recent lines for s///"""
    #channel = (input.sender).encode("utf-8")
    channel = uc.decode(input.sender)
    channel = uc.encode(channel)
    nick = (input.nick).encode("utf-8")
    search_dict = load_db()
    if channel not in search_dict:
        search_dict[channel] = dict()
    if 'last_said' not in search_dict[channel]:
        search_dict[channel]['last_said'] = list()
    if nick not in search_dict[channel]:
        search_dict[channel][nick] = list()
    templist = search_dict[channel][nick]
    last_said_templist = search_dict[channel]['last_said']
    line = input.group()
    try:
        line = (line).encode("utf-8")
    except Exception:
        return
    if line.startswith("s/") or line.startswith('!'):
        return
    elif line.startswith("\x01ACTION"):
        line = line[:-1]
    templist.append(line)
    last_said_templist.append("{}: {}".format(input.nick, line))
    del templist[:-50]
    del last_said_templist[:-50]
    search_dict[channel][nick] = templist
    search_dict[channel]['last_said'] = last_said_templist
    save_db(search_dict)
Beispiel #12
0
def delquote(code, input):
    '''delquote <number> -- removes a given quote from the database. Can only be done by the owner of the bot.'''
    text = input.group(2)
    number = int()
    try:
        fn = open('quotes.txt', 'r')
    except:
        return code.reply('{red}No quotes to delete.')
    lines = fn.readlines()
    fn.close()
    try:
        number = int(text)
    except:
        code.reply('Please enter the quote number you would like to delete.')
        return
    newlines = lines[:number - 1] + lines[number:]
    fn = open('quotes.txt', 'w')
    for line in newlines:
        txt = uc.encode(line)
        if txt:
            fn.write(txt)
            if txt[-1] != '\n':
                fn.write('\n')
    fn.close()
    code.reply('{green}Successfully deleted quote {b}%s{b}.' % (number))
Beispiel #13
0
def get_results(text):
    if not text:
        return list()
    a = re.findall(url_finder, text)
    k = len(a)
    i = 0
    display = list()
    passs = False
    while i < k:
        url = uc.encode(a[i][0])
        url = uc.decode(url)
        url = uc.iriToUri(url)
        url = remove_nonprint(url)
        domain = getTLD(url)
        if '//' in domain:
            domain = domain.split('//')[1]
        if not url.startswith(EXCLUSION_CHAR):
            passs, page_title = find_title(url)
            if bitly_loaded:
                bitly = short(url)
                bitly = bitly[0][1]
            else:
                bitly = url
            display.append([page_title, url, bitly])
        i += 1
    return passs, display
Beispiel #14
0
def collectlines(jenni, input):
    """Creates a temporary storage of most recent lines for s///"""
    #channel = (input.sender).encode("utf-8")
    channel = uc.decode(input.sender)
    channel = uc.encode(channel)
    nick = (input.nick).encode("utf-8")
    search_dict = load_db()
    if channel not in search_dict:
        search_dict[channel] = dict()
    if 'last_said' not in search_dict[channel]:
        search_dict[channel]['last_said'] = list()
    if nick not in search_dict[channel]:
        search_dict[channel][nick] = list()
    templist = search_dict[channel][nick]
    last_said_templist = search_dict[channel]['last_said']
    line = input.group()
    try:
        line = (line).encode("utf-8")
    except Exception:
        return
    if line.startswith("s/") or line.startswith('!'):
        return
    elif line.startswith("\x01ACTION"):
        line = line[:-1]
    templist.append(line)
    last_said_templist.append("{}: {}".format(input.nick, line))
    del templist[:-50]
    del last_said_templist[:-50]
    search_dict[channel][nick] = templist
    search_dict[channel]['last_said'] = last_said_templist
    save_db(search_dict)
Beispiel #15
0
def collectlines(jenni, input):
    """Creates a temporary storage of most recent lines for s///"""
    # don't log things in PM
    #channel = (input.sender).encode("utf-8")
    channel = uc.decode(input.sender)
    channel = uc.encode(channel)
    nick = (input.nick).encode("utf-8")
    if not channel.startswith('#'): return
    search_dict = load_db()
    if channel not in search_dict:
        search_dict[channel] = dict()
    if nick not in search_dict[channel]:
        search_dict[channel][nick] = list()
    templist = search_dict[channel][nick]
    line = input.group()
    if line.startswith("s/"):
        return
    elif line.startswith("\x01ACTION"):
        line = line[:-1]
        templist.append(line)
    else:
        templist.append(line)
    del templist[:-10]
    search_dict[channel][nick] = templist
    save_db(search_dict)
Beispiel #16
0
def delquote(jenni, input):
    '''.rmquote <number> -- removes a given quote from the database. Can only be done by the owner of the bot.'''
    if not input.owner: return
    text = input.group(2)
    number = int()
    try:
        fn = open('quotes.txt', 'r')
    except:
        return jenni.reply('No quotes to delete.')
    lines = fn.readlines()
    MAX = len(lines)
    fn.close()
    try:
        number = int(text)
    except:
        jenni.reply('Please enter the quote number you would like to delete.')
        return
    newlines = lines[:number-1] + lines[number:]
    fn = open('quotes.txt', 'w')
    for line in newlines:
        txt = uc.encode(line)
        if txt:
            fn.write(txt)
            if txt[-1] != '\n':
                fn.write('\n')
    fn.close()
    jenni.reply('Successfully deleted quote %s.' % (number))
Beispiel #17
0
def addquote(code, input):
    '''addquote <nick> something they said here -- adds the quote to the quote database.'''
    fn = open('quotes.txt', 'a')
    output = uc.encode(input.group(2))
    fn.write(output)
    fn.write('\n')
    fn.close()
    code.reply('Quote added.')
Beispiel #18
0
def addquote(jenni, input):
    '''.addquote <nick> something they said here -- adds the quote to the quote database.'''
    text = input.group(2)
    fn = open('quotes.txt', 'a')
    output = uc.encode(text)
    fn.write(output)
    fn.write('\n')
    fn.close()
    jenni.reply('Quote added.')
Beispiel #19
0
def addquote(jenni, input):
    '''.addquote <nick> something they said here -- adds the quote to the quote database.'''
    text = input.group(2)
    fn = open('quotes.txt', 'a')
    output = uc.encode(text)
    fn.write(output)
    fn.write('\n')
    fn.close()
    jenni.reply('Quote added.')
Beispiel #20
0
 def given_user(nick, channel):
     nick = uc.encode(nick.lower())
     channel = channel.lower()
     if channel in self.scores_dict:
         if nick in self.scores_dict[channel]:
             return self.str_score(nick, channel)
         else:
             return self.STRINGS["nouser"].format(nick, channel)
     else:
         return self.STRINGS["nochan"].format(channel)
Beispiel #21
0
def save_db(search_dict):
    """ save search_dict to find.txt """
    search_file = open("find.txt", "w")
    for channel in search_dict:
        if channel is not "":
            for nick in search_dict[channel]:
                for line in search_dict[channel][nick]:
                    channel_utf = uc.encode(channel)
                    search_file.write(channel_utf)
                    search_file.write(",")
                    nick = uc.encode(nick)
                    search_file.write(nick)
                    search_file.write(",")
                    line_utf = line
                    if not type(str()) == type(line):
                        line_utf = uc.encode(line)
                    search_file.write(line_utf)
                    search_file.write("\n")
    search_file.close()
Beispiel #22
0
 def given_user(nick, channel):
     nick = uc.encode(nick.lower())
     channel = channel.lower()
     if channel in self.scores_dict:
         if nick in self.scores_dict[channel]:
             return self.str_score(nick, channel)
         else:
             return self.STRINGS["nouser"].format(nick, channel)
     else:
         return self.STRINGS["nochan"].format(channel)
Beispiel #23
0
def save_db(search_dict):
    """ save search_dict to find.txt """
    search_file = open("find.txt", "w")
    for channel in search_dict:
        if channel is not "":
            for nick in search_dict[channel]:
                for line in search_dict[channel][nick]:
                    channel_utf = uc.encode(channel)
                    search_file.write(channel_utf)
                    search_file.write(",")
                    nick = uc.encode(nick)
                    search_file.write(nick)
                    search_file.write(",")
                    line_utf = line
                    if not type(str()) == type(line):
                        line_utf = uc.encode(line)
                    search_file.write(line_utf)
                    search_file.write("\n")
    search_file.close()
Beispiel #24
0
def addquote(phenny, input):
    '''.addquote <nick> something they said here -- adds the quote to the quote database.'''
    text = input.group(2)
    if not text:
        return phenny.say('No quote provided')
    fn = open('quotes.txt', 'a')
    output = uc.encode(text)
    fn.write(output)
    fn.write('\n')
    fn.close()
    phenny.reply('Quote added.')
Beispiel #25
0
Datei: ip.py Projekt: myano/jenni
def ip_lookup(jenni, input):
    txt = input.group(2)
    if not txt:
        return jenni.reply("No search term!")
    txt = uc.encode(txt)
    query = uc.decode(txt)
    response = "[IP/Host Lookup] "
    try:
        page = web.get(base + txt)
    except IOError, err:
        return jenni.say('Could not access given address. (Detailed error: %s)' % (err))
Beispiel #26
0
def f_lisp(jenni, input):
    '''.lisp <lisp code> -- execute arbitrary lisp code'''
    txt = input.group(2)
    txt = uc.encode(txt)
    try:
        output = parse(txt)
        output = eval(output)
        output = to_string(output)
    except Exception, e:
        jenni.reply('Scheme Lisp ERROR: %s' % (str(e)))
        return
Beispiel #27
0
def f_lisp(jenni, input):
    '''.lisp <lisp code> -- execute arbitrary lisp code'''
    txt = input.group(2)
    txt = uc.encode(txt)
    try:
        output = parse(txt)
        output = eval(output)
        output = to_string(output)
    except Exception, e:
        jenni.reply('Scheme Lisp ERROR: %s' % (str(e)))
        return
Beispiel #28
0
def ip_lookup(jenni, input):
    txt = input.group(2)
    if not txt:
        return jenni.reply("No search term!")
    txt = uc.encode(txt)
    query = uc.decode(txt)
    response = "[IP/Host Lookup] "
    try:
        page = web.get(base + txt)
    except IOError, err:
        return jenni.say(
            'Could not access given address. (Detailed error: %s)' % (err))
Beispiel #29
0
def quote(s, safe='/'):  # quote('abc def') -> 'abc%20def'
    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)
Beispiel #30
0
def get_results(text, manual=False):
    if not text:
        return False, list()
    a = re.findall(url_finder, text)
    k = len(a)
    i = 0
    display = list()
    passs = False
    channel = str()
    if hasattr(text, 'sender'):
        channel = text.sender
    while i < k:
        url = uc.encode(a[i][0])
        url = uc.decode(url)
        url = uc.iriToUri(url)
        url = remove_nonprint(url)
        domain = getTLD(url)
        if '//' in domain:
            domain = domain.split('//')[1]
        if 'i.imgur.com' in url and url.startswith('http://'):
            url = url.replace('http:', 'https:')

        bitly = url

        if not url.startswith(EXCLUSION_CHAR):
            passs, page_title = find_title(url)
            if not manual:
                if bitly_loaded:
                    if channel and channel not in simple_channels:
                        bitly = short(url)
                        if bitly:
                            bitly = bitly[0][1]
            display.append([page_title, url, bitly, passs])
        else:
            ## has exclusion character
            if manual:
                ## only process excluded URLs if .title is used
                url = url[1:]
                passs, page_title = find_title(url)
                display.append([page_title, url, bitly, passs])
        i += 1

    ## check to make sure at least 1 URL worked correctly
    overall_pass = False
    for x in display:
        if x[-1] == True:
            overall_pass = True

    return overall_pass, display
Beispiel #31
0
Datei: url.py Projekt: J3RN/jenni
def get_results(text, manual=False):
    if not text:
        return False, list()
    a = re.findall(url_finder, text)
    k = len(a)
    i = 0
    display = list()
    passs = False
    channel = str()
    if hasattr(text, 'sender'):
        channel = text.sender
    while i < k:
        url = uc.encode(a[i][0])
        url = uc.decode(url)
        url = uc.iriToUri(url)
        url = remove_nonprint(url)
        domain = getTLD(url)
        if '//' in domain:
            domain = domain.split('//')[1]
        if 'i.imgur.com' in url and url.startswith('http://'):
            url = url.replace('http:', 'https:')

        bitly = url

        if not url.startswith(EXCLUSION_CHAR):
            passs, page_title = find_title(url)
            if not manual:
                if bitly_loaded:
                    if channel and channel not in simple_channels:
                        bitly = short(url)
                        if bitly:
                            bitly = bitly[0][1]
            display.append([page_title, url, bitly, passs])
        else:
            ## has exclusion character
            if manual:
                ## only process excluded URLs if .title is used
                url = url[1:]
                passs, page_title = find_title(url)
                display.append([page_title, url, bitly, passs])
        i += 1

    ## check to make sure at least 1 URL worked correctly
    overall_pass = False
    for x in display:
        if x[-1] == True:
            overall_pass = True

    return overall_pass, display
Beispiel #32
0
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:
        char = name2codepoint[entity[1:-1]]
        meep = unichr(char)
    try:
        return uc.decode(meep)
    except:
        return uc.decode(uc.encode(meep))
Beispiel #33
0
def update_backlog(jenni, input):
    """Write every received line to a backlog file"""
    channel = uc.encode(uc.decode(input.sender))
    nick = (input.nick).encode("utf-8")
    line = input.group()
    if type(line) != type(str()):
        line = uc.encode(line)

    # Rules for lines that should be excluded
    if not channel.startswith("#"):
        return
    elif line.startswith("."):
        return
    elif line.startswith("s/"):
        return

    jenni.backlog_lock.acquire()
    try:
        backlog_file = open(jenni.backlog_filename, "a")
        backlog_file.write("{},{},{}\n".format(channel, nick, line))
        backlog_file.close()
        #TODO: Check backlog size and clear oldest entries if needed
    finally:
        jenni.backlog_lock.release()
Beispiel #34
0
def addinsult(jenni, input):
    """.iadd <insult> -- adds a harsh adjetive to the insult database"""
    try:
        insultFilename = os.path.expanduser('~/.jenni/insult.'+ jenni.config.insult_lang +'.txt')
    except:
        jenni.say("You need to configure the default language!")
        return

    text = input.group(2)
    text = uc.encode(text)
    fn = open(insultFilename, "a")
    fn.write(text)
    fn.write("\n")
    fn.close()
    jenni.reply("Insult added.")
Beispiel #35
0
def addinsult(kenni, input):
    """.iadd <insult> -- adds a harsh adjetive to the insult database"""
    try:
        insultFilename = os.path.expanduser('~/Kenni/config/insult.'+ kenni.config.insult_lang +'.txt')
    except:
        kenni.say("You need to configure the default language!")
        return

    text = input.group(2)
    text = uc.encode(text)
    fn = open(insultFilename, "a")
    fn.write(text)
    fn.write("\n")
    fn.close()
    kenni.say("Insult added.")
Beispiel #36
0
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:
        char = name2codepoint[entity[1:-1]]
        meep = unichr(char)
    try:
        return uc.decode(meep)
    except:
        return uc.decode(uc.encode(meep))
Beispiel #37
0
def ip_lookup(code, input):
    txt = input.group(2)
    if not txt:
        return code.reply(code.color('red', 'No search term!'))
    elif txt.find('192.168.0.') > -1 or txt.find('127.0.0.') > -1:
        return code.reply(code.color('red', 'That IP is blacklisted!'))
    elif not txt.find('.') > -1 or not txt.find(':') > -1:
        return code.reply(code.color('red','Incorrect input!'))
    txt = uc.encode(txt)
    query = uc.decode(txt) 
    response = ''
    #response = '[' + code.color('blue', 'IP/Host Lookup ') + '] '
    page = web.get(base + txt)
    try:
        results = json.loads(page)
    except:
        print str(page)
        return code.color('red', code.reply('Couldn\'t receive information for %s' % code.bold(txt)))
    if results:
        print 'query', str(query)
        print 'matches', re_ip.findall(query)
        if re_ip.findall(query):
            ## IP Address
            try:
                hostname = socket.gethostbyaddr(query)[0]
            except:
                hostname = code.bold(code.color('red', 'Unknown Host'))
            response += code.color('blue', 'Hostname: ') + str(hostname)
        else:
            ## Host name
            response += code.bold(code.color('blue','IP: ')) + results['ip']
        spacing = ' ' + code.bold('|')
        for param in results:
            if not results[param]:
                results[param] = code.bold(code.color('red', 'N/A'))
        if 'city' in results:
            response += '%s %s %s' % (spacing, code.bold(code.color('blue', 'City:')), results['city'])
        if 'region_name' in results:
            response += '%s %s %s' % (spacing, code.bold(code.color('blue', 'State:')), results['region_name'])
        if 'country_name' in results:
            response += '%s %s %s' % (spacing, code.bold(code.color('blue', 'Country:')), results['country_name'])
        if 'zip' in results:
            response += '%s %s %s' % (spacing, code.bold(code.color('blue', 'ZIP:')), results['zip'])
        response += '%s %s %s' % (spacing, code.bold(code.color('blue', 'Latitude:')), results['latitude'])
        response += '%s %s %s' % (spacing, code.bold(code.color('blue', 'Longitude:')), results['longitude'])
    code.reply(response)
Beispiel #38
0
def get_results(text, manual=False):
    if not text:
        return list()
    a = re.findall(url_finder, text)
    k = len(a)
    i = 0
    display = list()
    passs = False
    while i < k:
        url = uc.encode(a[i][0])
        url = uc.decode(url)
        url = uc.iriToUri(url)
        url = remove_nonprint(url)
        domain = getTLD(url)
        if '//' in domain:
            domain = domain.split('//')[1]

        bitly = url

        if not url.startswith(EXCLUSION_CHAR):
            if not manual:
                if bitly_loaded:
                    bitly = short(url)
                    if bitly:
                        bitly = bitly[0][1]
            passs, page_title = find_title(url)
            if not passs:
                passs, page_title = find_title(url)
            display.append([page_title, url, bitly, passs])
        else:
            ## has exclusion character
            if manual:
                ## only process excluded URLs if .title is used
                url = url[1:]
                passs, page_title = find_title(url)
                display.append([page_title, url, bitly, passs])
        i += 1

    ## check to make sure at least 1 URL worked correctly
    overall_pass = False
    for x in display:
        if x[-1] == True:
            overall_pass = True

    return overall_pass, display
Beispiel #39
0
def fucking_weather(jenni, input):
    """.fw (ZIP|City, State) -- provide a ZIP code or a city state pair to hear about the f*****g weather"""
    text = input.group(2)
    if not text:
        jenni.reply(
            'INVALID F*****G INPUT. PLEASE ENTER A F*****G ZIP CODE, OR A F*****G CITY-STATE PAIR.'
        )
        return
    new_text = str()

    new_text = uc.encode(text)
    search = urllib.quote((new_text).strip())

    url = 'http://thefuckingweather.com/?where=%s' % (search)
    try:
        page = web.get(url)
    except:
        return jenni.say("I COULDN'T ACCESS THE F*****G SITE.")
    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)
    location = re_location.findall(page)
    response = str()
    if location and location[0]:
        response += 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 += "I CAN'T FIND THAT SHIT ON THEIR PAGE."
    if conditions:
        response += ' ' + conditions[0]
    if flavor:
        response += ' -- ' + flavor[0].replace('  ', ' ')
    jenni.say(response)
Beispiel #40
0
def get_results(text, manual=False):
    if not text:
        return list()
    a = re.findall(url_finder, text)
    k = len(a)
    i = 0
    display = list()
    passs = False
    while i < k:
        url = uc.encode(a[i][0])
        url = uc.decode(url)
        url = uc.iriToUri(url)
        url = remove_nonprint(url)
        domain = getTLD(url)
        if '//' in domain:
            domain = domain.split('//')[1]

        bitly = url

        if not url.startswith(EXCLUSION_CHAR):
            if not manual:
                if bitly_loaded:
                    bitly = short(url)
                    if bitly:
                        bitly = bitly[0][1]
            passs, page_title = find_title(url)
            if not passs:
                passs, page_title = find_title(url)
            display.append([page_title, url, bitly, passs])
        else:
            ## has exclusion character
            if manual:
                ## only process excluded URLs if .title is used
                url = url[1:]
                passs, page_title = find_title(url)
                display.append([page_title, url, bitly, passs])
        i += 1

    ## check to make sure at least 1 URL worked correctly
    overall_pass = False
    for x in display:
        if x[-1] == True:
            overall_pass = True

    return overall_pass, display
Beispiel #41
0
def ep(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:
            meep = str()
    try:
        return uc.decode(meep)
    except:
        return uc.decode(uc.encode(meep))
Beispiel #42
0
def fucking_weather(jenni, input):
    """.fw (ZIP|City, State) -- provide a ZIP code or a city state pair to hear about the f*****g weather"""
    text = input.group(2)
    if not text:
        jenni.reply('INVALID F*****G INPUT. PLEASE ENTER A F*****G ZIP CODE, OR A F*****G CITY-STATE PAIR.')
        return
    new_text = str()

    new_text = uc.encode(text)
    search = urllib.quote((new_text).strip())

    url = 'http://thefuckingweather.com/?where=%s' % (search)
    try:
        page = web.get(url)
    except:
        return jenni.say("I COULDN'T ACCESS THE F*****G SITE.")
    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)
    location = re_location.findall(page)
    response = str()
    if location and location[0]:
        response += 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 += "I CAN'T FIND THAT SHIT ON THEIR PAGE."
    if conditions:
        response += ' ' + conditions[0]
    if flavor:
        response += ' -- ' + flavor[0].replace('  ', ' ')
    jenni.say(response)
Beispiel #43
0
def ip_lookup(jenni, input):
    txt = input.group(2)
    if not txt:
        return jenni.reply("No search term!")
    txt = uc.encode(txt)
    query = uc.decode(txt)
    response = "[IP/Host Lookup] "
    page = web.get(base + txt)
    try:
        results = json.loads(page)
    except:
        print str(page)
        return jenni.reply('Did not receive proper JSON from %s' % (base))
    if results:
        print 'query', str(query)
        print 'matches', re_ip.findall(query)
        if re_ip.findall(query):
            ## IP Address
            try:
                hostname = socket.gethostbyaddr(query)[0]
            except:
                hostname = 'Unknown Host'
            response += 'Hostname: ' + str(hostname)
        else:
            ## Host name
            response += 'IP: ' + results['ip']
        spacing = ' |'
        for param in results:
            if not results[param]:
                results[param] = 'N/A'
        if 'city' in results:
            response += '%s City: %s' % (spacing, results['city'])
        if 'region_name' in results:
            response += '%s State: %s' % (spacing, results['region_name'])
        if 'country_name' in results:
            response += '%s Country: %s' % (spacing, results['country_name'])
        if 'zip' in results:
            response += '%s ZIP: %s' % (spacing, results['zip'])
        response += '%s Latitude: %s' % (spacing, results['latitude'])
        response += '%s Longitutde: %s' % (spacing, results['longitude'])
    jenni.reply(response)
Beispiel #44
0
    def edpoints(self, phenny, nick, chan, points):
        if not nick:
            return
        nick = uc.encode(nick.lower())
        if not nick:
            phenny.reply(self.STRINGS["cantadd"])
        else:
            nick = nick.lower()
            if chan not in self.scores_dict:
                self.scores_dict[chan] = {}
            if not nick in self.scores_dict[chan]:
                self.scores_dict[chan][nick] = [0, 0]

            # Add a point if points is TRUE, remove if FALSE
            if points:
                self.scores_dict[chan][nick][0] += 1
            else:
                self.scores_dict[chan][nick][1] += 1

            self.save()
            phenny.say(self.str_score(nick, chan))
Beispiel #45
0
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))
Beispiel #46
0
 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))
Beispiel #47
0
def ip_lookup(jenni, input):
    txt = input.group(2)
    if not txt:
        return jenni.reply("No search term!")
    txt = uc.encode(txt)
    query = uc.decode(txt)
    response = "[IP/Host Lookup] "
    page = web.get(base + txt)
    try:
        results = json.loads(page)
    except:
        return jenni.reply('Did not receive proper JSON from %s' % (base))
    if results:
        if re_ip.findall(query):
            ## IP Address
            try:
                hostname = socket.gethostbyaddr(query)[0]
            except:
                hostname = 'Unknown Host'
            response += 'Hostname: ' + str(hostname)
        else:
            ## Host name
            response += 'IP: ' + results['ip']
        spacing = ' |'
        for param in results:
            if not results[param]:
                results[param] = 'N/A'
        if 'city' in results:
            response += '%s City: %s' % (spacing, results['city'])
        if 'region_name' in results:
            response += '%s State: %s' % (spacing, results['region_name'])
        if 'country_name' in results:
            response += '%s Country: %s' % (spacing, results['country_name'])
        if 'zip' in results:
            response += '%s ZIP: %s' % (spacing, results['zip'])
        response += '%s Latitude: %s' % (spacing, results['latitude'])
        response += '%s Longitude: %s' % (spacing, results['longitude'])
    jenni.reply(response)
Beispiel #48
0
    def editpoints(self, phenny, input, nick, points):
        if not nick:
            return
        nick = uc.encode(nick.lower())
        if not nick:
            phenny.reply(self.STRINGS["cantadd"])
        elif (input.nick).lower() == nick:
            phenny.reply(self.STRINGS["denied"])
        else:
            nick = nick.lower()
            if input.sender not in self.scores_dict:
                self.scores_dict[input.sender] = {}
            if not nick in self.scores_dict[input.sender]:
                self.scores_dict[input.sender][nick] = [0, 0]

            # Add a point if points is TRUE, remove if FALSE
            if points:
                self.scores_dict[input.sender][nick][0] += 1
            else:
                self.scores_dict[input.sender][nick][1] += 1

            self.save()
            chan = input.sender
            phenny.say(self.str_score(nick, chan))
Beispiel #49
0
    def editpoints(self, jenni, input, nick, points):
        if not nick:
            return
        nick = uc.encode(nick.lower())
        if not nick:
            jenni.reply(self.STRINGS["cantadd"])
        elif (not input.admin) and (input.nick).lower() == nick:
            jenni.reply(self.STRINGS["denied"])
        else:
            nick = nick.lower()
            chan = (input.sender).lower()
            if chan not in self.scores_dict:
                self.scores_dict[chan] = {}
            if not nick in self.scores_dict[chan]:
                self.scores_dict[chan][nick] = [0, 0]

            # Add a point if points is TRUE, remove if FALSE
            if points:
                self.scores_dict[chan][nick][0] += 1
            else:
                self.scores_dict[chan][nick][1] += 1

            self.save()
            jenni.say(self.str_score(nick, chan))
Beispiel #50
0
    def editpoints(self, jenni, input, nick, points):
        if not nick:
            return
        nick = uc.encode(nick.lower())
        if not nick:
            jenni.reply(self.STRINGS["cantadd"])
        elif (not input.admin) and (input.nick).lower() == nick:
            jenni.reply(self.STRINGS["denied"])
        else:
            nick = nick.lower()
            chan = (input.sender).lower()
            if chan not in self.scores_dict:
                self.scores_dict[chan] = {}
            if not nick in self.scores_dict[chan]:
                self.scores_dict[chan][nick] = [0, 0]

            # Add a point if points is TRUE, remove if FALSE
            if points:
                self.scores_dict[chan][nick][0] += 1
            else:
                self.scores_dict[chan][nick][1] += 1

            self.save()
            jenni.say(self.str_score(nick, chan))
Beispiel #51
0
def forecastio_current_weather(jenni, input):
    if not hasattr(jenni.config, 'forecastio_apikey'):
        return jenni.say('Please sign up for a darksky.net API key at https://darksky.net/')

    txt = input.group(2)
    if not txt:
        return jenni.say('Please provide a location.')

    #name, county, region, countryName, lat, lng = location(txt)
    name, lat, lng = location(txt)
    if name == 'ImportError' and not lat and not lng:
        return install_geopy

    url = 'https://api.darksky.net/forecast/%s/%s,%s'

    url = url % (jenni.config.forecastio_apikey, urllib.quote(lat), urllib.quote(lng))

    ## do some error checking
    try:
        ## if the Internet is working this should work, \o/
        page = web.get(url)
    except:
        ## well, crap, check your Internet, and if you can access darksky.net
        return jenni.say('Could not acess https://api.darksky.net/')

    try:
        ## we want tasty JSON
        data = json.loads(page)
    except:
        ## that wasn't tasty
        return jenni.say('The server did not return anything that was readable as JSON.')


    if 'currently' not in data:
        ## doesn't happen until the GPS coords are completely bonkers
        return jenni.say('No information obtained from darksky.net for the given location: %s,' % (name, lat, lng,) )

    ## let the fun begin!!

    today = data['currently']

    cond = today['icon']  ## 0.17
    cover = today['cloudCover']
    temp = today['temperature']
    dew = today['dewPoint']
    pressure = today['pressure']
    speed = today['windSpeed']
    degrees = today['windBearing']
    humidity = today['humidity']
    APtemp = today['apparentTemperature']

    ## this code is different than the section in the previous sectio
    ## as darksky.net uses more precise measurements than NOAA
    if cover >= 0.8:
        cover_word = 'Overcast'
    elif cover >= 0.5:
        cover_word = 'Cloudy'
    elif cover >= 0.2:
        cover_word = 'Scattered'
    else:
        cover_word = 'Clear'

    temp_c = (temp - 32) / 1.8
    temp = u'%.1f\u00B0F (%.1f\u00B0C)'.encode('utf-8') % (temp, temp_c)

    dew_c = (dew - 32) / 1.8
    dew = u'%.1f\u00B0F (%.1f\u00B0C)'.encode('utf-8') % (dew, dew_c)

    APtemp_c = (APtemp - 32) / 1.8
    APtemp = u'%.1f\u00B0F (%.1f\u00B0C)'.encode('utf-8') % (APtemp, APtemp_c)

    humidity = str(int(float(humidity) * 100)) + '%'

    pressure = '%.2fin (%.2fmb)' % (pressure * 0.0295301, pressure)
    cond = cond.replace('-', ' ')
    cond = cond.title()

    description = speed_desc(speed)

    degrees = wind_dir(degrees)

    ## value taken from, https://is.gd/3dNrbW
    speedC = 1.609344 * speed
    wind = '%s %.1fmph (%.1fkmh) (%s)' % (description, speed, speedC, degrees)

    ## ISO-8601 ftw, https://xkcd.com/1179/
    time = datetime.datetime.fromtimestamp(int(today['time'])).strftime('%Y-%m-%d %H:%M:%S')

    ## build output string.
    ## a bit messy, but better than other alternatives
    output = str()
    output += '\x1FCover\x1F: ' + cover_word
    output += ', \x1FTemp\x1F: ' + str(temp)
    output += ', \x1FDew Point\x1F: ' + str(dew)
    output += ', \x1FHumidity\x1F: ' + str(humidity)
    output += ', \x1FApparent Temp\x1F: ' + str(APtemp)
    output += ', \x1FPressure\x1F: ' + pressure
    if cond:
        output += ', \x1FCondition\x1F: ' + (cond).encode('utf-8')
    output += ', \x1FWind\x1F: ' + wind
    output += ' - '
    output += uc.encode(name)
    output + '; %s UTC' % (time)

    ## required according to ToS by darksky.net
    output += ' (Powered by Dark Sky, darksky.net)'
    jenni.say(output)
Beispiel #52
0
def forecast(jenni, input):
    if not hasattr(jenni.config, 'forecastio_apikey'):
        return jenni.say(
            'Please sign up for a forecast.io API key at https://forecast.io/ or try .wx-noaa or .weather-noaa'
        )

    txt = input.group(2)

    name, county, region, countryName, lat, lng = location(txt)

    url = 'https://api.forecast.io/forecast/%s/%s,%s'

    url = url % (jenni.config.forecastio_apikey, urllib.quote(lat),
                 urllib.quote(lng))

    ## do some error checking
    try:
        ## if this fails, we got bigger problems
        page = web.get(url)
    except:
        return jenni.say('Could not acess https://api.forecast.io/')

    ## we want some tasty JSON
    try:
        data = json.loads(page)
    except:
        return jenni.say(
            'The server did not return anything that was readable as JSON.')

    if 'daily' not in data or 'data' not in data['daily']:
        return jenni.say(
            'Cannot find usable info in information returned by the server.')

    ## here we go...

    days = data['daily']['data']

    new_name = uc.decode(name)
    new_region = uc.decode(region)
    new_countryName = uc.decode(countryName)
    new_county = uc.decode(county)

    output = u'[%s, %s, %s] ' % (new_name, region, new_countryName)

    if 'alerts' in data:
        ## TODO: modularize the colourful parsing of alerts from nws.py so this can be colourized
        for alert in data['alerts']:
            jenni.say(
                alert['title'] + ' Expires at: ' +
                str(datetime.datetime.fromtimestamp(int(alert['expires']))))

    k = 1
    units = data['flags']['units']

    second_output = str()
    second_output = u'[%s, %s, %s] ' % (new_name, region, new_countryName)

    for day in days:
        ## give me floats with only one significant digit
        form = u'%.1f'

        hi = form % (day['temperatureMax'])
        low = form % (day['temperatureMin'])
        dew = form % (day['dewPoint'])

        ## convert measurements in F to C
        hiC = form % ((float(day['temperatureMax']) - 32) * (5.0 / 9.0))
        lowC = form % ((float(day['temperatureMin']) - 32) * (5.0 / 9.0))
        dewC = form % ((float(day['dewPoint'] - 32)) * (5.0 / 9.0))

        ## value taken from, http://is.gd/3dNrbW
        windspeedC = form % (float(day['windSpeed']) * 1.609344)
        windspeed = form % (day['windSpeed'])
        summary = day['summary']
        dotw_day = datetime.datetime.fromtimestamp(int(
            day['sunriseTime'])).weekday()
        dotw_day_pretty = u'\x0310\x02\x1F%s\x1F\x02\x03' % (dotw[dotw_day])

        line = u'%s: \x02\x0304%sF (%sC)\x03\x02 / \x02\x0312%sF (%sC)\x03\x02, \x1FDew\x1F: %sF (%sC), \x1FWind\x1F: %smph (%skmh), %s | '

        ## remove extra whitespace
        dotw_day_pretty = (dotw_day_pretty).strip()
        hi = (hi).strip()
        low = (low).strip()
        dew = (dew).strip()
        windspeed = (windspeed).strip()
        summary = (summary).strip()

        ## toggle unicode nonsense
        dotw_day_pretty = uc.encode(dotw_day_pretty)
        hi = uc.encode(hi)
        low = uc.encode(low)
        dew = uc.encode(dew)
        windspeed = uc.encode(windspeed)
        summary = uc.encode(summary)

        ## more unicode nonsense
        dotw_day_pretty = uc.decode(dotw_day_pretty).upper()
        hi = uc.decode(hi)
        low = uc.decode(low)
        dew = uc.decode(dew)
        windspeed = uc.decode(windspeed)
        summary = uc.decode(summary)

        ## only show 'today' and the next 3-days.
        ## but only show 2 days on each line
        if k <= 2:
            output += line % (dotw_day_pretty, hi, hiC, low, lowC, dew, dewC,
                              windspeed, windspeedC, summary)
        elif k <= 4:
            second_output += line % (dotw_day_pretty, hi, hiC, low, lowC, dew,
                                     dewC, windspeed, windspeedC, summary)
        else:
            break

        k += 1

    ## chomp off the ending |
    if output.endswith(' | '):
        output = output[:-3]

    ## say the first part
    jenni.say(output)

    if second_output.endswith(' | '):
        second_output = second_output[:-3]

    ## required according to ToS by forecast.io
    second_output += ' (Powered by Forecast, forecast.io)'
    jenni.say(second_output)
Beispiel #53
0
def find_title(url):
    """
    This finds the title when provided with a string of a URL.
    """

    for item in IGNORE:
        if item in url:
            return False, 'ignored'

    if not re.search('^((https?)|(ftp))://', url):
        url = 'http://' + url

    if '/#!' in url:
        url = url.replace('/#!', '/?_escaped_fragment_=')

    if 'i.imgur' in url:
        a = url.split('.')
        url = a[0][:-1] + '.'.join(a[1:-1])

    if 'zerobin.net' in url:
        return True, 'ZeroBin'

    url = uc.decode(url)

    msg = str()
    k = 0
    status = False

    while not status:
        k += 1
        if k > 3:
            break

        msg = dict()

        try:
            status, msg = proxy.get_more(url)
        except:
            try:
                status, msg = get_page_backup(url)
            except:
                continue

        if type(msg) == type(dict()) and 'code' in msg:
            status = msg['code']
        else:
            continue

        time.sleep(0.5)

    if not status:
        return False, msg

    useful = msg

    info = useful['headers']
    page = useful['read']

    try:
        mtype = info['content-type']
    except:
        print 'failed mtype:', str(info)
        return False, 'mtype failed'
    if not (('/html' in mtype) or ('/xhtml' in mtype)):
        return False, str(mtype)

    content = page
    regex = re.compile('<(/?)title( [^>]+)?>', re.IGNORECASE)
    content = regex.sub(r'<\1title>', content)
    regex = re.compile('[\'"]<title>[\'"]', re.IGNORECASE)
    content = regex.sub('', content)
    start = content.find('<title>')
    if start == -1:
        return False, 'NO <title> found'
    end = content.find('</title>', start)
    if end == -1:
        return False, 'NO </title> found'
    content = content[start + 7:end]
    content = content.strip('\n').rstrip().lstrip()
    title = content

    if len(title) > 200:
        title = title[:200] + '[...]'

    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))

    title = r_entity.sub(e, title)

    title = title.replace('\n', '')
    title = title.replace('\r', '')

    def remove_spaces(x):
        if '  ' in x:
            x = x.replace('  ', ' ')
            return remove_spaces(x)
        else:
            return x

    title = remove_spaces(title)

    new_title = str()
    for char in title:
        unichar = uc.encode(char)
        if len(list(uc.encode(char))) <= 3:
            new_title += uc.encode(char)
    title = new_title

    title = re.sub(r'(?i)dcc\ssend', '', title)

    title += '\x0F'

    if title:
        return True, title
    else:
        return False, 'No Title'
Beispiel #54
0
Datei: url.py Projekt: J3RN/jenni
def find_title(url):
    """
    This finds the title when provided with a string of a URL.
    """

    for item in IGNORE:
        if item in url:
            return False, 'ignored'

    if not re.search('^((https?)|(ftp))://', url):
        url = 'http://' + url

    if '/#!' in url:
        url = url.replace('/#!', '/?_escaped_fragment_=')

    if 'i.imgur' in url:
        a = url.split('.')
        url = a[0][:-1] + '.'.join(a[1:-1])

    if 'zerobin.net' in url:
        return True, 'ZeroBin'

    url = uc.decode(url)

    msg = str()
    k = 0
    status = False

    while not status:
        k += 1
        if k > 3:
            break

        msg = dict()

        try:
            status, msg = proxy.get_more(url)
        except:
            try:
                status, msg = get_page_backup(url)
            except:
                continue

        if type(msg) == type(dict()) and 'code' in msg:
            status = msg['code']
        else:
            continue

        time.sleep(0.5)


    if not status:
        return False, msg

    useful = msg

    info = useful['headers']
    page = useful['read']

    try:
        mtype = info['content-type']
    except:
        print 'failed mtype:', str(info)
        return False, 'mtype failed'
    if not (('/html' in mtype) or ('/xhtml' in mtype)):
        return False, str(mtype)

    content = page
    regex = re.compile('<(/?)title( [^>]+)?>', re.IGNORECASE)
    content = regex.sub(r'<\1title>', content)
    regex = re.compile('[\'"]<title>[\'"]', re.IGNORECASE)
    content = regex.sub('', content)
    start = content.find('<title>')
    if start == -1:
        return False, 'NO <title> found'
    end = content.find('</title>', start)
    if end == -1:
        return False, 'NO </title> found'
    content = content[start + 7:end]
    content = content.strip('\n').rstrip().lstrip()
    title = content

    if len(title) > 200:
        title = title[:200] + '[...]'

    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))

    title = r_entity.sub(e, title)

    title = title.replace('\n', '')
    title = title.replace('\r', '')

    def remove_spaces(x):
        if '  ' in x:
            x = x.replace('  ', ' ')
            return remove_spaces(x)
        else:
            return x

    title = remove_spaces(title)

    new_title = str()
    for char in title:
        unichar = uc.encode(char)
        if len(list(uc.encode(char))) <= 3:
            new_title += uc.encode(char)
    title = new_title

    title = re.sub(r'(?i)dcc\ssend', '', title)

    title += '\x0F'

    if title:
        return True, title
    else:
        return False, 'No Title'
Beispiel #55
0
def write_addquote(text):
    fn = open('quotes.txt', 'a')
    output = uc.encode(text)
    fn.write(output)
    fn.write('\n')
    fn.close()