cobalt.py

changeset 2
1a24dd2d598e
parent 1
29c7e9d13a30
child 3
53486417a8e5
equal deleted inserted replaced
1:29c7e9d13a30 2:1a24dd2d598e
32 import time 32 import time
33 import sys 33 import sys
34 import traceback 34 import traceback
35 import re 35 import re
36 import json 36 import json
37 import urllib
38 import urllib2
37 from suds.xsd.doctor import Import 39 from suds.xsd.doctor import Import
38 from suds.xsd.doctor import ImportDoctor 40 from suds.xsd.doctor import ImportDoctor
39 from suds.client import Client 41 from suds.client import Client
40 42
41 try: 43 try:
45 print 'couldn\'t open cobalt.json: %s' % e 47 print 'couldn\'t open cobalt.json: %s' % e
46 quit() 48 quit()
47 49
48 g_admins = g_config['admins'] 50 g_admins = g_config['admins']
49 g_mynick = g_config['nickname'] 51 g_mynick = g_config['nickname']
52
53 g_idgamesSearchURL = 'http://www.doomworld.com/idgames/api/api.php?action=search&query=%s&type=title&sort=date&out=json'
50 54
51 # 55 #
52 # SOAP stuff 56 # SOAP stuff
53 # 57 #
54 suds_import = Import ('http://schemas.xmlsoap.org/soap/encoding/', \ 58 suds_import = Import ('http://schemas.xmlsoap.org/soap/encoding/', \
162 self.send ("%s\n" % line) 166 self.send ("%s\n" % line)
163 self.send_buffer = [] 167 self.send_buffer = []
164 168
165 def handle_read (self): 169 def handle_read (self):
166 lines = self.recv (4096).splitlines() 170 lines = self.recv (4096).splitlines()
167 for line in lines: 171 for utfline in lines:
172 line = utfline.decode("utf-8").encode("ascii","ignore")
168 print "[%s] -> %s" % (self.name, line) 173 print "[%s] -> %s" % (self.name, line)
169 174
170 if line.startswith ("PING :"): 175 if line.startswith ("PING :"):
171 self.write ("PONG :%s" % line[6:]) 176 self.write ("PONG :%s" % line[6:])
172 continue 177 continue
239 elif command == "msg": 244 elif command == "msg":
240 check_admin (sender, ident, host, command) 245 check_admin (sender, ident, host, command)
241 if len(args) < 2: 246 if len(args) < 2:
242 raise logical_exception ("usage: .%s <target> <message...>" % command) 247 raise logical_exception ("usage: .%s <target> <message...>" % command)
243 self.privmsg (args[0], " ".join (args[1:])) 248 self.privmsg (args[0], " ".join (args[1:]))
244 elif command == "ticket": 249 elif command == 'ticket':
245 if len(args) != 1: 250 if len(args) != 1:
246 raise logical_exception ("usage: .%s <ticket>" % command) 251 raise logical_exception ("usage: .%s <ticket>" % command)
247 self.get_ticket_data (replyto, args[0], True) 252 self.get_ticket_data (replyto, args[0], True)
253 elif command == 'idgames':
254 try:
255 if len(args) < 1:
256 raise logical_exception ('usage: .%s <keywords>' % command)
257
258 url = g_idgamesSearchURL % urllib.quote (" ".join (args[0:]))
259 response = urllib2.urlopen (url).read()
260 data = json.loads (response)
261
262 if 'content' in data and 'file' in data['content']:
263 if type (data['content']['file']) is list:
264 files = data['content']['file']
265 else:
266 files = [data['content']['file']]
267
268 i = 0
269 for filedata in files:
270 if i >= 5:
271 break
272
273 self.privmsg (replyto, '- %s: \'%s\' by \'%s\', rating: %s: %s' % \
274 (filedata['filename'], filedata['title'], filedata['author'], filedata['rating'], filedata['url']))
275
276 i += 1
277 self.privmsg (replyto, "(%d / %d results posted)" % (i, len(files)))
278 elif 'warning' in data and 'message' in data['warning']:
279 raise logical_exception (data['warning']['message'])
280 elif 'error' in data and 'message' in data['error']:
281 raise logical_exception (data['error']['message'])
282 else:
283 raise logical_exception ("Incomplete JSON response from doomworld.com/idgames")
284 except logical_exception as e:
285 raise e
286 except Exception as e:
287 raise logical_exception ('Search failed: %s' % `e`)
248 else: 288 else:
249 raise logical_exception ("unknown command `.%s`" % command) 289 raise logical_exception ("unknown command `.%s`" % command)
250 290
251 def handle_error(self): 291 def handle_error(self):
252 excepterm (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)) 292 excepterm (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))

mercurial