~deimos/madcow-bot

9106e1b05fcabfa4e7176d3b7378082624f27b5e — Chris Jones 8 years ago 737d296
fix broken modules: calc, safefilter check
12 files changed, 60 insertions(+), 44 deletions(-)

M madcow/conf/defaults.py
R madcow/modules/{broken/clock.py => clock.py}
R madcow/modules/{broken/cnn.py => cnn.py}
R madcow/modules/{broken/election.py => election.py}
M madcow/modules/livejournal.py
R madcow/modules/{broken/movie.py => movie.py}
M madcow/modules/slut.py
M madcow/modules/steam.py
R madcow/modules/{broken/sunrise.py => sunrise.py}
R madcow/modules/{broken/translate.py => translate.py}
R madcow/modules/{broken/yelp.py => yelp.py}
M madcow/util/google.py
M madcow/conf/defaults.py => madcow/conf/defaults.py +8 -4
@@ 4,7 4,7 @@
### MAIN CONFIG ###
###################

PROTOCOL = 'cli'  # irc, slack, aim, pysilc, shell, cli
PROTOCOL = 'cli'  # irc, slack, aim, pysilc, shell, cli, ipython
BOTNAME = 'madcow'  # will use this nickname in irc/silc and for addressing
ALIASES = ['!']  # list of other nicks the bot will also respond to
DETACH = False  # set to True to run as a daemon (UNIX only)


@@ 25,8 25,8 @@ PRIVATE_HELP = True  # if True, redirects "help" output to private message
###############

LOGGING_LEVEL = 'INFO'  # DEBUG, INFO, WARN, ERROR
LOGGING_FORMAT = '[%(time)s - %(level)s] %(message)s'
LOGGING_TIME_FORMAT = '%Y/%m/%d %H:%M:%S'
LOGGING_FORMAT = '%(time)s [%(level)s] %(message)s'
LOGGING_TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
LOGGING_ENCODING = ENCODING
UNIQUE_TIMESTAMP_FORMAT = '%Y%m%d'
UNIQUE_MAX_FILES = 1000


@@ 186,6 186,7 @@ POLLMAIL_USE_PASSWORD = False
POLLMAIL_PASSWORD = None
POLLMAIL_AUTOSTART = False
POLLMAIL_JSON_REGEX = r'{({.+})}'

IMAP_SERVER = 'localhost'
IMAP_PORT = 993
IMAP_USERNAME = None


@@ 216,6 217,7 @@ TWITTER_CONSUMER_KEY = None
TWITTER_CONSUMER_SECRET = None
TWITTER_ACCESS_TOKEN_KEY = None
TWITTER_ACCESS_TOKEN_SECRET = None

TWITTER_UPDATE_FREQ = 45
TWITTER_OUTPUT = 'ALL'
TWITTER_TWEET_FORMAT = u'>> tweet from {tweet.user.screen_name}: {tweet.text_clean} <<'


@@ 226,9 228,11 @@ TWITTER_SOFT_LIMIT = 10

# settings for modules that use http
HTTP_TIMEOUT = 10
HTTP_AGENT = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)'
HTTP_COOKIES = True

# warning: changing this will probably break certain modules
HTTP_AGENT = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)'

# for django memebot integration
DJMEMEBOT_SETTINGS_FILE = '/path/to/memebot/settings.py'


R madcow/modules/broken/clock.py => madcow/modules/clock.py +0 -0
R madcow/modules/broken/cnn.py => madcow/modules/cnn.py +0 -0
R madcow/modules/broken/election.py => madcow/modules/election.py +0 -0
M madcow/modules/livejournal.py => madcow/modules/livejournal.py +1 -0
@@ 17,6 17,7 @@ class Main(Module):
    error = u"Couldn't load the page LJ returned D:"

    def response(self, nick, args, kwargs):
        kwargs['req'].blockquoted = True
        try:
            user = args[0]
        except:

R madcow/modules/broken/movie.py => madcow/modules/movie.py +0 -0
M madcow/modules/slut.py => madcow/modules/slut.py +32 -35
@@ 13,11 13,6 @@ import re
from madcow.util import Module
from urlparse import urljoin

match_re = re.compile(r'About ([\d,]+) results')
filter_re = re.compile(r'The word <b>"(\w+)"</b> has been filtered from the search')
baseURL = u'http://www.google.com/'
searchURL = urljoin(baseURL, u'/search')

class WordFiltered(Exception):

    """Indicates a word has been filtered by google safe search"""


@@ 29,49 24,51 @@ class WordFiltered(Exception):
        return repr(self.word)


def cleanurl(url):
    return url.replace(u" ", u"+")
class Main(Module):

    enabled = True
    pattern = re.compile(u'^\s*slutcheck\s+(.+)')
    require_addressing = True
    help = u"slutcheck <phrase> - see how slutty the phrase is"
    error = u'I failed to perform that lookup'

def slutrating(phrase):
    match_re = re.compile(r'About ([\d,]+) results')
    filter_re = re.compile(r'The word <b>"(\w+)"</b> has been filtered from the search')
    base_url = u'http://www.google.com/'
    search_url = urljoin(base_url, u'/search')

    phrase = cleanurl(phrase)
    def cleanurl(self, url):
        return url.replace(u" ", u"+")

    try:
        data = self.geturl(searchURL, opts={u'q': phrase, u'safe': u'off'})
        unsafe = int(match_re.search(data).group(1).replace(u',', u''))
    except AttributeError:
        unsafe = 0
    def slutrating(self, phrase):
        phrase = self.cleanurl(phrase)

    try:
        data = self.geturl(searchURL, opts={u'q': phrase, u'safe': u'active'})
        try:
            filtered = filter_re.search(data).group(1)
            raise WordFiltered(filtered)
            data = self.geturl(self.search_url, q=phrase, safe='off')
            unsafe = int(self.match_re.search(data).group(1).replace(u',', u''))
        except AttributeError:
            pass
        safe = int(match_re.search(data).group(1).replace(u',', u''))
    except AttributeError:
        safe = 0

    value = float(unsafe - safe) / float(unsafe)
    if value < 0:
        value = 0
    return value

            unsafe = 0

class Main(Module):
        try:
            data = self.geturl(self.search_url, q=phrase, safe='active')
            try:
                filtered = self.filter_re.search(data).group(1)
                raise WordFiltered(filtered)
            except AttributeError:
                pass
            safe = int(self.match_re.search(data).group(1).replace(u',', u''))
        except AttributeError:
            safe = 0

    enabled = True
    pattern = re.compile(u'^\s*slutcheck\s+(.+)')
    require_addressing = True
    help = u"slutcheck <phrase> - see how slutty the phrase is"
    error = u'I failed to perform that lookup'
        value = float(unsafe - safe) / float(unsafe)
        if value < 0:
            value = 0
        return value

    def response(self, nick, args, kwargs):
        try:
            query = u" ".join(args)
            rating = slutrating(query)
            rating = self.slutrating(query)
            return u"%s is %.2f%% slutty." % (query, rating * 100)
        except TypeError, error:
            self.log.exception('what')

M madcow/modules/steam.py => madcow/modules/steam.py +1 -0
@@ 23,6 23,7 @@ class Main(Module):
        while page:
            url = self.group_url + '?p=%d' % page
            soup = self.getsoup(url)
            self.ipython()
            next = soup.body.find('div', 'pageLinks').find(text=self.next_re)
            if next is None:
                page = None

R madcow/modules/broken/sunrise.py => madcow/modules/sunrise.py +0 -0
R madcow/modules/broken/translate.py => madcow/modules/translate.py +0 -0
R madcow/modules/broken/yelp.py => madcow/modules/yelp.py +0 -0
M madcow/util/google.py => madcow/util/google.py +18 -5
@@ 1,19 1,32 @@
# Copyright (C) 2007, 2008 Christopher Jones
#
# This file is part of Madcow.
#
# Madcow is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Madcow is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Madcow.  If not, see <http://www.gnu.org/licenses/>.

"""Google interface"""

from urlparse import urljoin

import urllib2
import re

from BeautifulSoup import BeautifulSoup

from madcow.util import strip_html, superscript, ipython
from madcow.util.http import UserAgent
from madcow.util.text import encode, decode

# just some random real user agent so we don't appear as a bot..
AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:46.0) Gecko/20100101 Firefox/46.0'

AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1"

class NonRedirectResponse(Exception):