Exemple #1
0
    def getrandom(self, times=3):
        """".how (times) - Gives you random instructions from wikiHow, by default 3 steps"""
        steps = []
        for i in xrange(times):
            page = getsoup("http://www.wikihow.com/Special:Randomizer")
            section = page.find("div", {"id": "steps"})
            if section:  # Only one 'method'
                allsteps = section.find("ol").findChildren("li",
                                                           recursive=False)
            else:  # Multiple 'methods', each with their own list of steps
                for x in xrange(1, 5):
                    try:
                        section = page.find("div",
                                            {"id": "steps_{}".format(x)})
                        try:
                            # Possible for a Method to have no actual steps, just a paragraph, so check for the list
                            allsteps = section.find("ol").findChildren(
                                "li", recursive=False)
                            break
                        except _notrap:
                            raise
                        except:
                            continue
                    except _notrap:
                        raise
                    except:
                        break

            steps.append(random.choice(allsteps))

        results = []
        for i, step in enumerate(steps):
            tag = step.find("b", {"class": "whb"})
            results.append(u'Step #{}: {}'.format(i + 1, decode(tag.text)))
        return results
Exemple #2
0
    def on_message(self, sender, channel, flags, message, private):
        req = Request(message=message)
        req.nick = sender.nickname
        req.private = private
        req.silc_sender = sender
        if private:
            req.addressed = True
            req.sendto = sender
            req.channel = u'privmsg'
        else:
            req.addressed = False
            req.sendto = channel
            req.channel = channel.channel_name

        req.message = decode(req.message)
        req.message = self.colorlib.strip_color(req.message)
        self.check_addressing(req)

        if req.message.startswith(u'^'):
            req.message = req.message[1:]
            req.colorize = True
        else:
            req.colorize = False

        self.process_message(req)
Exemple #3
0
 def calculator(self, query):
     """Try to use google calculator for given query"""
     opts = dict(self.calcopts)
     opts[u'q'] = query
     doc = self.ua.open(self.search, opts=opts)
     soup = BeautifulSoup(doc)
     values = []
     conv_left = soup.find('input', id='ucw_lhs_d')
     conv_right = soup.find('input', id='ucw_rhs_d')
     if not (conv_left is None or conv_right is None):
         left_value = conv_left['value'].strip()
         left_unit = conv_left.findNext('option').renderContents().strip()
         right_value = conv_right['value'].strip()
         right_unit = conv_right.findNext('option').renderContents().strip()
         values.append('(%s) %s = (%s) %s' % (left_unit, left_value, right_unit, right_value))
     calculation = soup.find('span', 'cwcot')
     if calculation is not None:
         values.append(calculation.renderContents())
     try:
         values.append(soup.find('h3', 'r').b.renderContents())
     except StandardError:
         pass
     #ipython()
     result = u', '.join(filter(None, (decode(strip_html(value)).strip() for value in values)))
     if result:
         return result
Exemple #4
0
 def wolfram_query(self, input, url=None, **opts):
     api_uri = URI.from_url(url) if url else self.default_api_uri
     query_string = urlencode(dict(opts, input=input))
     request_url = api_uri._replace(query=query_string).url
     response = urlopen(request_url)
     tree = etree.parse(response)
     if tree.xpath(XPaths.error):
         self.log_xml(tree)
         result = 'Error response from the server, check logs.'
     else:
         result = tree.xpath(XPaths.result)
         if result:
             result = result[0]
         else:
             result = 'No results.'
             suggests = ', '.join(tree.xpath(XPaths.suggests)).strip()
             if suggests:
                 result += ' Did you mean: ' + suggests
             else:
                 self.log_xml(tree)
                 result += ' This may be in error, check logs for XML response.'
     result = decode(result)
     for match in self.unicode_esc_re.finditer(result):
         result = result.replace(match.group(0), unichr(int(match.group(1), 16)), 1)
     return result.strip()
Exemple #5
0
    def getrandom(self, times=3):
        """".how (times) - Gives you random instructions from wikiHow, by default 3 steps"""
        steps = []
        for i in xrange(times):
            page = self.getsoup("http://www.wikihow.com/Special:Randomizer")
            section = page.find("div", {"id": "steps"})
            if section: # Only one 'method'
                allsteps = section.find("ol").findChildren("li", recursive=False)
            else: # Multiple 'methods', each with their own list of steps
                for x in xrange(1, 5):
                    try:
                        section  = page.find("div", {"id": "steps_{}".format(x)})
                        try:
                            # Possible for a Method to have no actual steps, just a paragraph, so check for the list
                            allsteps = section.find("ol").findChildren("li", recursive=False)
                            break
                        except _notrap:
                            raise
                        except:
                            continue
                    except _notrap:
                        raise
                    except:
                        break

            steps.append(random.choice(allsteps))

        results = []
        for i, step in enumerate(steps):
            tag = step.find("b", {"class": "whb"})
            results.append(u'Step #{}: {}'.format(i + 1, decode(tag.text)))
        return results
Exemple #6
0
    def response(self, nick, args, kwargs):
        message = encode(args[0])
        if kwargs['addressed']:
            match = score_request_re.search(message)
            if match is not None:
                start, end = match.groups()

                # asking for a username
                if end is None and start is not None:
                    return self.memebot.get_scores(name=start)
                start = ((int(start) if start is not None and start.isdigit()
                          else None) or 1) - 1
                end = (
                    (int(end) if end is not None and end.isdigit() else None)
                    or start + 10) - 1
                if start < 0:
                    start = 0
                if end < start:
                    end = start
                return self.memebot.get_scores(range=(start, end))

        match = url_re.search(message)
        if match is not None:
            url = decode(match.group(1))
            return self.memebot.process_url(url, nick, kwargs['channel'])
Exemple #7
0
    def on_message(self, sender, channel, flags, message, private):
        req = Request(message=message)
        req.nick = sender.nickname
        req.private = private
        req.silc_sender = sender
        if private:
            req.addressed = True
            req.sendto = sender
            req.channel = u'privmsg'
        else:
            req.addressed = False
            req.sendto = channel
            req.channel = channel.channel_name

        req.message = decode(req.message)
        req.message = self.colorlib.strip_color(req.message)
        self.check_addressing(req)

        if req.message.startswith(u'^'):
            req.message = req.message[1:]
            req.colorize = True
        else:
            req.colorize = False

        self.process_message(req)
Exemple #8
0
 def calculator(self, query):
     """Try to use google calculator for given query"""
     opts = dict(self.calcopts)
     opts[u'q'] = query
     doc = self.ua.open(self.search, opts=opts)
     soup = BeautifulSoup(doc)
     values = []
     conv_left = soup.find('input', id='ucw_lhs_d')
     conv_right = soup.find('input', id='ucw_rhs_d')
     if not (conv_left is None or conv_right is None):
         left_value = conv_left['value'].strip()
         left_unit = conv_left.findNext('option').renderContents().strip()
         right_value = conv_right['value'].strip()
         right_unit = conv_right.findNext('option').renderContents().strip()
         values.append('(%s) %s = (%s) %s' %
                       (left_unit, left_value, right_unit, right_value))
     calculation = soup.find('span', 'cwcot')
     if calculation is not None:
         values.append(calculation.renderContents())
     try:
         values.append(soup.find('h3', 'r').b.renderContents())
     except StandardError:
         pass
     #ipython()
     result = u', '.join(
         filter(None,
                (decode(strip_html(value)).strip() for value in values)))
     if result:
         return result
Exemple #9
0
 def run(self):
     """Runs madcow loop"""
     while self.running:
         self.check_response_queue()
         line = decode(raw_input('>>> '), sys.stdin.encoding).rstrip()
         req = Request(message=line)
         req.nick = os.environ['USER']
         req.channel = u'none'
         req.addressed = True
         req.private = True
         self.check_addressing(req)
         self.process_message(req)
Exemple #10
0
 def run(self):
     """Runs madcow loop"""
     while self.running:
         self.check_response_queue()
         line = decode(raw_input('>>> '), sys.stdin.encoding).rstrip()
         req = Request(message=line)
         req.nick = os.environ['USER']
         req.channel = u'none'
         req.addressed = True
         req.private = True
         self.check_addressing(req)
         self.process_message(req)
Exemple #11
0
 def response(self, nick, args, kwargs):
     try:
         url = args[0]
         uri = urlparse(url)
         if (uri.scheme.lower() in SCHEMES and
                 '.'.join(uri.netloc.lower().split('.')[-2:]) in DOMAINS and
                 os.path.split(os.path.normpath(uri.path))[-1] == 'watch' and
                 'v' in cgi.parse_qs(uri.query)):
             soup = getsoup(url)
             title = strip_html(decode(soup.title.renderContents())).replace(u' - YouTube', u'').strip()
             if title:
                 self.bot.output(title, kwargs['req'])
     except (KeyboardInterrupt, SystemExit):
         raise
     except:
         pass
Exemple #12
0
    def response(self, nick, args, kwargs):
        query = 'site\:youtube.com/watch '+args[0]
        #return u'{}: {}: {}'.format(nick, YOUTUBE, self.google.lucky(query))

        url = self.google.lucky(query)
        uri = urlparse(url)
        if (uri.scheme.lower() in SCHEMES and
                '.'.join(uri.netloc.lower().split('.')[-2:]) in DOMAINS and
                os.path.split(os.path.normpath(uri.path))[-1] == 'watch' and
                'v' in cgi.parse_qs(uri.query)):
            soup = getsoup(url)
            title = strip_html(decode(soup.title.renderContents())).replace(u' - YouTube', u'').strip()
            if title:
                response = u'{} - {}'.format(title, url)
                self.bot.output(response, kwargs['req'])
            else:
                return u'{} - {}'.format('Cant find youtube link, here is a google lucky search', url)
Exemple #13
0
    def response(self, nick, args, kwargs):
        query = args[0]
        sopa = getsoup(self.google.find(query))

        contador = 1  # Yay for the mexican dev
        myretval = u""

        for li in sopa.body("div", {"id": "ires"})[0].ol("li", {"class": "g"}):
            if contador > 3:
                break
            name = strip_html(decode(li.b.renderContents()))
            urlpluscrap = li.a["href"].replace("/url?q=", "")
            url = urlpluscrap.split("&sa")[0]
            myretval += u"{}: {} \n".format(name, url)
            contador += 1

        return myretval
Exemple #14
0
    def calculator(self, query):
        """Try to use google calculator for given query"""
        opts = dict(self.calcopts)
        opts[u'q'] = query
        doc = self.ua.open(self.search, opts=opts)
        soup = BeautifulSoup(doc)
        response = soup.find('img', src=self.calc_re).parent.findNext('h2').renderContents()
        response = ' '.join(response.splitlines())
        response = decode(response, 'utf-8')

        # turn super scripts into utf8
        parts = []
        for part in self.sup_re.split(response):
            if self.sup_re.match(part):
                part = superscript(part)
            parts.append(part)
        response = u''.join(parts)
        response = self.white_re.sub(' ', strip_html(response).strip())
        return response
Exemple #15
0
    def response(self, nick, args, kwargs):
        message = encode(args[0])
        if kwargs['addressed']:
            match = score_request_re.search(message)
            if match is not None:
                start, end = match.groups()

                # asking for a username
                if end is None and start is not None:
                    return self.memebot.get_scores(name=start)
                start = ((int(start) if start is not None and start.isdigit() else None) or 1) - 1
                end = ((int(end) if end is not None and end.isdigit() else None) or start + 10) - 1
                if start < 0:
                    start = 0
                if end < start:
                    end = start
                return self.memebot.get_scores(range=(start, end))

        match = url_re.search(message)
        if match is not None:
            url = decode(match.group(1))
            return self.memebot.process_url(url, nick, kwargs['channel'])
Exemple #16
0
    def data_received(self, data):
        self.buf += data
        if len(self.buf) > self.maxsize:
            raise InvalidPayload('payload exceeded max size')
        if not self.headers_done:
            if not self.headsep.search(self.buf):
                return
            try:
                # parse headers
                hdrs, self.buf = self.headsep.split(self.buf, 1)
                hdrs = decode(hdrs)
                hdrs = self.newline.split(hdrs)
                hdrs = [hdr.split(u':', 1) for hdr in hdrs]
                hdrs = [(k.lower(), v.lstrip()) for k, v in hdrs]
                hdrs = dict(hdrs)

                if u'size' in hdrs:
                    hdrs[u'size'] = int(hdrs[u'size'])

                # save data
                self.hdrs = hdrs
                self.headers_done = True
            except Exception, error:
                raise InvalidPayload(u'invalid payload: %s' % error)
Exemple #17
0
    def data_received(self, data):
        self.buf += data
        if len(self.buf) > self.maxsize:
            raise InvalidPayload('payload exceeded max size')
        if not self.headers_done:
            if not self.headsep.search(self.buf):
                return
            try:
                # parse headers
                hdrs, self.buf = self.headsep.split(self.buf, 1)
                hdrs = decode(hdrs)
                hdrs = self.newline.split(hdrs)
                hdrs = [hdr.split(u':', 1) for hdr in hdrs]
                hdrs = [(k.lower(), v.lstrip()) for k, v in hdrs]
                hdrs = dict(hdrs)

                if u'size' in hdrs:
                    hdrs[u'size'] = int(hdrs[u'size'])

                # save data
                self.hdrs = hdrs
                self.headers_done = True
            except Exception, error:
                raise InvalidPayload(u'invalid payload: %s' % error)
Exemple #18
0
def render(node, _newline_re=re.compile(r'(?:\r\n|[\r\n])')):
    """Render node to text"""
    data = node.renderContents()
    if isinstance(data, str):
        data = decode(data, 'utf-8')
    return _newline_re.sub(' ', strip_html(data)).strip()
Exemple #19
0
 def log_xml(self, tree):
     self.log.warn('WolframAlpha Response XML:')
     self.log.warn(decode(etree.tostring(tree.getroot())))
Exemple #20
0
 def decode(node):
     return decode(node.renderContents())
Exemple #21
0
 def decode(node):
     return decode(node.renderContents())
Exemple #22
0
 def render(self, node, _newline_re=re.compile(r'(?:\r\n|[\r\n])')):
     """Render node to text"""
     data = strip_html(decode(node.renderContents()))
     if self.madcow.proto != 'slack':
         data = _newline_re.sub(u' ', data).strip()
     return data