Esempio n. 1
0
    def keyword_k(self, params=None, **kwargs):
        """Retrieve kernel.org Bugzilla bug information (ex: K12345)"""
        if params:
            params = utils.ensure_int(params)
            if not params:
                return

            query = urllib.urlencode({"id": params})
            url = "http://bugzilla.kernel.org/show_bug.cgi?%s" % query
            response = self.irc.fetch_url(url, self.name)
            if not response or not isinstance(params, int):
                return

            soup = BeautifulSoup(response.read())
            desc = utils.decode_entities(soup.head.title.string)

            try:
                status = soup.find("span", {"id":
                        "static_bug_status"}).string.strip().capitalize()
                assignee = utils.decode_entities(soup.findAll("span",
                        {"class": "vcard"})[0].contents[0].string)

                self.irc.reply("Kernel.org %s [Status: %s, Assignee: %s] %s" %
                        (desc, status, assignee, url))
            except TypeError:
                return
Esempio n. 2
0
    def keyword_k(self, params=None, **kwargs):
        """Retrieve kernel.org Bugzilla bug information (ex: K12345)"""
        if params:
            params = utils.ensure_int(params)
            if not params:
                return

            query = urllib.urlencode({"id": params})
            url = "http://bugzilla.kernel.org/show_bug.cgi?%s" % query
            response = self.irc.fetch_url(url, self.name)
            if not response or not isinstance(params, int):
                return

            soup = BeautifulSoup(response.read())
            desc = utils.decode_entities(soup.head.title.string)

            try:
                status = soup.find("span", {
                    "id": "static_bug_status"
                }).string.strip().capitalize()
                assignee = utils.decode_entities(
                    soup.findAll("span",
                                 {"class": "vcard"})[0].contents[0].string)

                self.irc.reply("Kernel.org %s [Status: %s, Assignee: %s] %s" %
                               (desc, status, assignee, url))
            except TypeError:
                return
Esempio n. 3
0
    def urban(self, params=None, **kwargs):
        """Search Urban Dictionary (ex: .urban <query>)"""
        if params:
            query = urllib.urlencode({"term": params})
            url = "http://www.urbandictionary.com/define.php?%s" % query
            response = self.irc.fetch_url(url, self.name)
            if not response:
                return

            soup = BeautifulSoup(response.read())
            results = soup.findAll("div", {"class": "definition"})

            urban = ""
            if len(results):
                urban = " ".join(str(x) for x in soup.findAll(
                        "div", {"class": "definition"})[0].contents)

            if len(urban) > 0:
                for i, line in enumerate(urban.split("<br/>")):
                    if i <= 4:
                        self.irc.reply(utils.decode_entities(line))
                    else:
                        self.irc.reply("[...] %s" % url)
                        break
            else:
                self.irc.reply("No results found: '%s'" % params)
        else:
            self.irc.reply(self.urban.__doc__)
Esempio n. 4
0
    def urban(self, params=None, **kwargs):
        """Search Urban Dictionary (ex: .urban <query>)"""
        if params:
            query = urllib.urlencode({"term": params})
            url = "http://www.urbandictionary.com/define.php?%s" % query
            response = self.irc.fetch_url(url, self.name)
            if not response:
                return

            soup = BeautifulSoup(response.read())
            results = soup.findAll("div", {"class": "definition"})

            urban = ""
            if len(results):
                urban = " ".join(
                    str(x) for x in soup.findAll(
                        "div", {"class": "definition"})[0].contents)

            if len(urban) > 0:
                for i, line in enumerate(urban.split("<br/>")):
                    if i <= 4:
                        self.irc.reply(utils.decode_entities(line))
                    else:
                        self.irc.reply("[...] %s" % url)
                        break
            else:
                self.irc.reply("No results found: '%s'" % params)
        else:
            self.irc.reply(self.urban.__doc__)
Esempio n. 5
0
    def imdb(self, params=None, **kwargs):
        """Search IMDb (ex: .imdb <query>)"""
        if params:
            query = urllib.urlencode({"q": params})
            url = "http://www.imdb.com/find?s=all&%s" % query
            response = self.irc.fetch_url(url, self.name)
            if not response:
                return

            soup = BeautifulSoup(response.read())
            results = soup.findAll("td", {"valign": "top"})

            i = 0
            for result in results:
                if len(result) > 3 and len(result.contents[2].attrs) > 0:
                    id = result.contents[2].attrs[0][1]
                    title = utils.decode_entities(
                        result.contents[2].contents[0])
                    year = result.contents[2].nextSibling.strip()[0:6]

                    if not title.startswith("aka") and len(year):
                        self.irc.reply("%s %s: http://www.imdb.com%s" %
                                       (title, year, id))
                        i += 1
                elif i >= 4:
                    break

            if i == 0:
                self.irc.reply("No results found: '%s'" % params)
        else:
            self.irc.reply(self.imdb.__doc__)
Esempio n. 6
0
    def imdb(self, params=None, **kwargs):
        """Search IMDb (ex: .imdb <query>)"""
        if params:
            query = urllib.urlencode({"q": params})
            url = "http://www.imdb.com/find?s=all&%s" % query
            response = self.irc.fetch_url(url, self.name)
            if not response:
                return

            soup = BeautifulSoup(response.read())
            results = soup.findAll("td", {"valign": "top"})

            i = 0
            for result in results:
                if len(result) > 3 and len(result.contents[2].attrs) > 0:
                    id = result.contents[2].attrs[0][1]
                    title = utils.decode_entities(
                            result.contents[2].contents[0])
                    year = result.contents[2].nextSibling.strip()[0:6]

                    if not title.startswith("aka") and len(year):
                        self.irc.reply("%s %s: http://www.imdb.com%s" % (
                                title, year, id))
                        i += 1
                elif i >= 4:
                    break

            if i == 0:
                self.irc.reply("No results found: '%s'" % params)
        else:
            self.irc.reply(self.imdb.__doc__)
Esempio n. 7
0
    def grouphug(self, params=None, **kwargs):
        """Display a random Group Hug (ex: .grouphug)"""
        url = "http://grouphug.us/random"
        response = self.irc.fetch_url(url, self.name)
        if not response:
            return

        soup = BeautifulSoup(response.read())
        grouphug = utils.decode_entities(
                soup.findAll(id=re.compile("node-\d+"))[2].p.contents[0])
        self.irc.reply(grouphug)
Esempio n. 8
0
    def grouphug(self, params=None, **kwargs):
        """Display a random Group Hug (ex: .grouphug)"""
        url = "http://grouphug.us/random"
        response = self.irc.fetch_url(url, self.name)

        html = response.read()
        r = re.compile("<div class=\"content\">\n\s+<p>(.*)</p>\n\s+</div>")
        m = r.search(html)
        if m:
            line = utils.decode_entities(m.group(1))
            self.irc.reply(line)
        else:
            self.irc.reply("Unable to parse Group Hug data")
Esempio n. 9
0
    def lastnight(self, params=None, **kwargs):
        """Display a random Text From Last Night (ex: .lastnight)"""
        url = ("http://www.textsfromlastnight.com/"
                "Random-Texts-From-Last-Night.html")
        response = self.irc.fetch_url(url, self.name)
        if not response:
            return

        soup = BeautifulSoup(response.read())
        lastnight = utils.decode_entities(
                soup.findAll(href=re.compile(
                        "/Text-Replies-\d+.html"))[0].contents[0])
        self.irc.reply(lastnight)
Esempio n. 10
0
    def _find_title(self, url):
        """Find the title of a given URL"""
        if not url.startswith("http://"):
            url = "http://" + url

        response = self.irc.fetch_url(url, self.name)
        if not response:
            return

        soup = BeautifulSoup(response.read())

        if soup.head:
            title = utils.decode_entities(soup.head.title.string)
            self.irc.reply(title)
        else:
            self.irc.reply("No title found for %s" % url)
Esempio n. 11
0
    def _find_title(self, url):
        """Find the title of a given URL"""
        if not url.startswith("http://"):
            url = "http://" + url

        response = self.irc.fetch_url(url, self.name).read()
        response = re.sub("\n", "", response)
        response = re.sub("  +", "", response)

        r = re.compile("<title>(.*)</title>")
        m = r.search(response)

        if m:
            title = utils.decode_entities(m.group(1))
            self.irc.reply(title)
        else:
            self.irc.reply("No title found for %s" % url)
Esempio n. 12
0
    def twitter(self, params=None, **kwargs):
        """Search Twitter (ex: .twitter <query>)"""
        if params:
            query = urllib.urlencode({"q": params, "rpp": 4})
            url = "http://search.twitter.com/search.json?%s" % query
            response = self.irc.fetch_url(url, self.name)

            json = simplejson.loads(response.read())
            results = json["results"]
            if results:
                for r in results:
                    self.irc.reply("@%s: %s" % (
                        r["from_user"],
                        utils.decode_entities(
                            r["text"].encode("ascii", "ignore"))))
            else:
                self.irc.reply("No results found: '%s'" % params)
        else:
            self.irc.reply(self.twitter.__doc__)
Esempio n. 13
0
    def _find_title(self, url):
        """Find the title of a given URL"""
        if not url.startswith(("http://", "https://")):
            url = "http://" + url

        response = self.irc.fetch_url(url, self.name)
        if not response:
            return

        soup = BeautifulSoup(response.read())
        if soup.head:
            title = utils.decode_entities(soup.head.title.string)
            content_type = response.headers.get("Content-Type").split(";",
                    1)[0]
            content_size = response.headers.get("Content-Length")
            content_size = content_size + " bytes" if content_size else "N/A"

            self.irc.reply("%s (%s, %s)" % (title, content_type, content_size))
        else:
            self.irc.reply("No title found for %s" % url)
Esempio n. 14
0
    def twitter(self, params=None, **kwargs):
        """Search Twitter (ex: .twitter <query>)"""
        if params:
            query = urllib.urlencode({"q": params, "rpp": 4})
            url = "http://search.twitter.com/search.json?%s" % query
            response = self.irc.fetch_url(url, self.name)
            if not response:
                return

            json_obj = json.loads(response.read())
            results = json_obj["results"]
            if results:
                for r in results:
                    self.irc.reply("@%s: %s" %
                                   (r["from_user"],
                                    utils.decode_entities(r["text"].encode(
                                        "ascii", "ignore"))))
            else:
                self.irc.reply("No results found: '%s'" % params)
        else:
            self.irc.reply(self.twitter.__doc__)
Esempio n. 15
0
    def urban(self, params=None, **kwargs):
        """Search Urban Dictionary (ex: .urban <query>)"""
        if params:
            query = urllib.urlencode({"term": params})
            url = "http://www.urbandictionary.com/define.php?%s" % query
            response = self.irc.fetch_url(url, self.name)

            html = response.read()
            if re.search("<i>%s</i>\nisn't defined" % params, html):
                self.irc.reply("No results found: '%s'" % params)
            else:
                r = (re.compile("<div class=\"definition\">(.*)</div>"
                                "<div class=\"example\">"))
                m = r.search(html)
                for i, line in enumerate(m.group(1).split("<br/>")):
                    if i <= 4:
                        line = utils.decode_entities(line)
                        self.irc.reply(line)
                    else:
                        self.irc.reply("[...] %s" % url)
                        break
        else:
            self.irc.reply(self.urban.__doc__)
Esempio n. 16
0
 def test_decode_entities_16(self):
     test_str = "&ndash;"
     self.assertEqual(utils.decode_entities(test_str), "-")
Esempio n. 17
0
 def test_decode_entities_15(self):
     test_str = "&#x26;"
     self.assertEqual(utils.decode_entities(test_str), "&")
Esempio n. 18
0
 def test_decode_entities_14(self):
     test_str = "&#x27;"
     self.assertEqual(utils.decode_entities(test_str), "'")
Esempio n. 19
0
 def test_decode_entities_13(self):
     test_str = "&#x22;"
     self.assertEqual(utils.decode_entities(test_str), '"')
Esempio n. 20
0
 def test_decode_entities_11(self):
     test_str = "<]*?>"
     self.assertEqual(utils.decode_entities(test_str), "")
Esempio n. 21
0
 def test_decode_entities_10(self):
     test_str = "<[lol^>"
     self.assertEqual(utils.decode_entities(test_str), "")
Esempio n. 22
0
 def test_decode_entities(self):
     test_str = "<foo>&#64;&amp;bar&amp;&#64;</foo>"
     self.assertEqual(utils.decode_entities(test_str), "@&bar&@")
Esempio n. 23
0
 def test_decode_entities_8(self):
     test_str = "&#8221;"
     self.assertEqual(utils.decode_entities(test_str), '"')
Esempio n. 24
0
 def test_decode_entities_3(self):
     test_str = "&amp;"
     self.assertEqual(utils.decode_entities(test_str), "&")
Esempio n. 25
0
 def test_decode_entities_4(self):
     test_str = "&quot;"
     self.assertEqual(utils.decode_entities(test_str), '"')
Esempio n. 26
0
 def test_decode_entities_2(self):
     test_str = "foo&nbsp;bar"
     self.assertEqual(utils.decode_entities(test_str), "foo bar")
Esempio n. 27
0
 def test_decode_entities(self):
     test_str = "<foo>bar</foo>"
     self.assertEqual(utils.decode_entities(test_str), "bar")
Esempio n. 28
0
 def test_decode_entities(self):
     test_str = "<foo>&#64;&amp;bar&amp;&#64;</foo>"
     self.assertEqual(utils.decode_entities(test_str), "@&bar&@")
Esempio n. 29
0
 def test_decode_entities_17(self):
     test_str = "&#64;"
     self.assertEqual(utils.decode_entities(test_str), "@")
Esempio n. 30
0
 def test_decode_entities_9(self):
     test_str = "&#8230;"
     self.assertEqual(utils.decode_entities(test_str), "...")
Esempio n. 31
0
 def test_decode_entities_5(self):
     test_str = "&#8212;"
     self.assertEqual(utils.decode_entities(test_str), "-")
Esempio n. 32
0
 def test_decode_entities_7(self):
     test_str = "&#8220;"
     self.assertEqual(utils.decode_entities(test_str), "\"")