[pybot-commits] CVS: pybot/pybot/modules google.py,NONE,1.1 freshmeat.py,1.8,1.9
Brought to you by:
niemeyer
|
From: Gustavo N. <nie...@us...> - 2003-05-29 05:11:32
|
Update of /cvsroot/pybot/pybot/pybot/modules
In directory sc8-pr-cvs1:/tmp/cvs-serv3743/pybot/modules
Modified Files:
freshmeat.py
Added Files:
google.py
Log Message:
* modules/google.py: Introduced basic google module using the
google SOAP api.
* pybot/util/SOAPpy: Added SOAPpy 0.10.1 to the source tree.
--- NEW FILE: google.py ---
# Copyright (c) 2000-2003 Gustavo Niemeyer <nie...@co...>
#
# This file is part of pybot.
#
# pybot 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 2 of the License, or
# (at your option) any later version.
#
# pybot 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 pybot; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from pybot import mm, hooks, config
from pybot.util import SOAPpy
import thread
import re
HELP = """
You may ask for google search results using "[search] google [<n>]:
<search>". Unless the parameter n is used, the default is to show the
first result found (with n=0).
"""
PERM_GOOGLE = """
The "google" permission allows users to ask for google search results.
Check "help google" for more information.
"""
class Google:
def __init__(self):
if config.has_option("global", "http_proxy"):
self.proxy = config.get("global", "http_proxy")
self.proxy = re.sub(".*://", "", self.proxy)
else:
self.proxy = None
self.key = config.get("google", "license_key")
hooks.register("Message", self.message)
# [search] google [<n>]: <search>
self.re1 = re.compile(r"(?:search\s+)?google(?:\s+(?P<n>\d+))?:\s*(?P<search>.+)$", re.I)
mm.register_help("(?:search\s+)?google(?:\s+search)?", HELP,
"google")
mm.register_perm("google", PERM_GOOGLE)
def unload(self):
hooks.unregister("Message", self.message)
mm.unregister_help(HELP)
mm.unregister_perm("google")
def search(self, msg, search, n):
try:
proxy = SOAPpy.SOAPProxy("http://api.google.com/search/beta2",
namespace="urn:GoogleSearch",
http_proxy=self.proxy)
result = proxy.doGoogleSearch(self.key, search, n, 1,
SOAPpy.booleanType(1), "",
SOAPpy.booleanType(0), "",
"", "")
if not len(result.resultElements):
msg.answer("%:", ["Nothing found",
"Google couldn't find anything",
"Google has its mind empty right now",
"Google has never seen such thing"],
[".", "!"])
else:
e = result.resultElements[0]
url = e.URL
title = re.sub("<.*?>", "", e.title)
snippet = re.sub("<.*?>", "", e.snippet)
msg.answer("%:", "%s <%s> - %s" % (title, url, snippet))
except SOAPpy.Errors.Error:
import traceback
traceback.print_exc()
msg.answer("%:", ["There was an error trying to ask google",
"Something wrong happened while trying to"
"ask google",
"I got some problem while trying to do that"],
[".", "!"])
def message(self, msg):
if not msg.forme:
return None
m = self.re1.match(msg.line)
if m:
if mm.hasperm(msg, "google"):
n = int(m.group("n") or 0)
search = m.group("search")
thread.start_new_thread(self.search, (msg, search, n))
else:
msg.answer("%:", ["You can't", "You're not allowed to",
"You're not good enough to"],
["do google actions",
"use google"], ["!", "."])
return 0
def __loadmodule__():
global mod
mod = Google()
def __unloadmodule__():
global mod
mod.unload()
del mod
# vim:ts=4:sw=4:et
Index: freshmeat.py
===================================================================
RCS file: /cvsroot/pybot/pybot/pybot/modules/freshmeat.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** freshmeat.py 12 May 2003 20:42:21 -0000 1.8
--- freshmeat.py 29 May 2003 05:11:29 -0000 1.9
***************
*** 38,46 ****
class Freshmeat:
def __init__(self):
! self.url = config.get("freshmeat", "url")
! if config.has_option("freshmeat", "proxy"):
! self.proxy = config.get("freshmeat", "proxy")
else:
self.proxy = None
self.interval = config.getint("freshmeat", "interval")
--- 38,46 ----
class Freshmeat:
def __init__(self):
! if config.has_option("global", "http_proxy"):
! self.proxy = config.get("global", "http_proxy")
else:
self.proxy = None
+ self.url = config.get("freshmeat", "url")
self.interval = config.getint("freshmeat", "interval")
|