# Copyright 2010, 2011 G24 # # This file is part of gpy. # # gpy 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. # # gpy 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 gpy. If not, see <http://www.gnu.org/licenses/>. import main import urllib import json class mod_main(main.clisten): """Web module.""" def addcmds(self): self.name = "web" self.cmds = {"ddg":"ddg","google":"google"} def ddg(self): '''www.duckduckgo.com web search.''' try: request = self.conn.recvinfo["message.params"].replace(" ","+") sock = urllib.urlopen("http://api.duckduckgo.com/?q=%s&o=json"%request) data = sock.read() sock.close() if json.loads(data)["AbstractText"]!="": self.conn.reply("%s... ( %s %s )"%(json.loads(data)["AbstractText"].encode('utf-8')[0:200],json.loads(data)["AbstractURL"].encode('utf-8'),json.loads(data)["Heading"].encode('utf-8'))) elif json.loads(data)["Definition"] != "": self.conn.reply("%s... ( %s )"%(json.loads(data)["Definition"].encode('utf-8'),json.loads(data)["DefinitionURL"].encode('utf-8'))) except Exception, e: self.conn.reply(str(e)) def google(self): '''www.google.com web search. Google fails - I output two first results instead of one.''' try: if self.conn.config.get("google",'key')=="" or self.conn.config.get("google",'cx')=="": self.conn.reply("GOOGLE PLUGIN NOT CONFIGURED PROERLY - needs kay and cx settings in main.cfg to work! See http://code.google.com/apis/customsearch/v1/using_rest.html") return request = self.conn.recvinfo["message.params"].replace(" ","+") sock = urllib.urlopen("https://www.googleapis.com/customsearch/v1?key=%s&cx=%s&q=%s&alt=json"%(self.conn.config.get("google",'key'),self.conn.config.get("google",'cx'),request)) data = sock.read() sock.close() if json.loads(data)["items"]!=[]: self.conn.reply("%s... ( %s ); %s... ( %s )"%(json.loads(data)["items"][0]["snippet"].encode('utf-8')[0:100],json.loads(data)["items"][0]["link"].encode('utf-8'),json.loads(data)["items"][1]["snippet"].encode('utf-8')[0:100],json.loads(data)["items"][1]["link"].encode('utf-8'))) except Exception, e: self.conn.reply(str(e))