# 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",'gtran':'gtran'} 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 PROPERLY - needs kay and cx settings in " + self.conn.configpath + " 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&safe=high"%(self.conn.config.get("google",'key'),self.conn.config.get("google",'cx'),request)) data = sock.read() sock.close() if "items" in json.loads(data): 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)) def gtran(self): '''translate.google.com ; use FromLanguage ToLanguage Text params.''' try: if self.conn.recvinfo['message.params'].count(' ')<2: self.conn.reply('Not enough parameters.') return froml = self.conn.recvinfo['message.params'].split(' ')[0] tol = self.conn.recvinfo['message.params'].split(' ')[1] txt = ' '.join(self.conn.recvinfo['message.params'].split(' ')[2:]) if self.conn.config.get("google",'trkey')=="": self.conn.reply("google::trkey not set at " + self.conn.configpath + ". See http://code.google.com/apis/language/translate/v2/getting_started.html") return request = self.conn.recvinfo["message.params"].replace(" ","+") sock = urllib.urlopen("https://www.googleapis.com/language/translate/v2?key=%s&q=%s&source=%s&target=%s"%(self.conn.config.get("google",'trkey'),txt,froml,tol)) data = sock.read() sock.close() if "data" in json.loads(data): self.conn.reply("%s"%json.loads(data)["data"]["translations"][0]["translatedText"].encode('utf-8')) else: self.conn.cancel(str(json.loads(data))) except Exception, e: self.conn.reply(str(e))