mod_idgames.py

changeset 114
0d1fd111cb22
parent 65
20bd76353eb5
child 124
7b2cd8b1ba86
equal deleted inserted replaced
113:08e9b1c1b324 114:0d1fd111cb22
16 ] 16 ]
17 } 17 }
18 18
19 g_idgamesSearchURL = 'http://www.doomworld.com/idgames/api/api.php?action=search&query=%s&type=title&sort=date&out=json' 19 g_idgamesSearchURL = 'http://www.doomworld.com/idgames/api/api.php?action=search&query=%s&type=title&sort=date&out=json'
20 20
21 def cmd_idgames (bot, args, replyto, **rest): 21 def cmd_idgames (bot, args, reply, **rest):
22 try: 22 try:
23 url = g_idgamesSearchURL % urllib.quote (args['wad']) 23 url = g_idgamesSearchURL % urllib.quote (args['wad'])
24 response = urllib2.urlopen (url).read() 24 response = urllib2.urlopen (url).read()
25 data = json.loads (response) 25 data = json.loads (response)
26 26
28 if type (data['content']['file']) is list: 28 if type (data['content']['file']) is list:
29 files = data['content']['file'] 29 files = data['content']['file']
30 else: 30 else:
31 files = [data['content']['file']] 31 files = [data['content']['file']]
32 32
33 i = 0
34
35 for filedata in files: 33 for filedata in files:
36 if i >= 5: 34 reply ('- %s: \'%s\' by \'%s\', rating: %s: %s' % \
37 break 35 (filedata['filename'], filedata['title'], filedata['author'],
38 36 filedata['rating'], filedata['url']))
39 bot.privmsg (replyto, '- %s: \'%s\' by \'%s\', rating: %s: %s' % \
40 (filedata['filename'], filedata['title'], filedata['author'], filedata['rating'], filedata['url']))
41
42 i += 1
43 #done
44 bot.privmsg (replyto, "(%d / %d results posted)" % (i, len(files)))
45 elif 'warning' in data and 'message' in data['warning']: 37 elif 'warning' in data and 'message' in data['warning']:
46 command_error (data['warning']['message']) 38 command_error (data['warning']['message'])
47 elif 'error' in data and 'message' in data['error']: 39 elif 'error' in data and 'message' in data['error']:
48 command_error (data['error']['message']) 40 command_error (data['error']['message'])
49 else: 41 else:
50 command_error ("Incomplete JSON response from doomworld.com/idgames") 42 command_error ("Incomplete JSON response from doomworld.com/idgames")
51 except Exception as e: 43 except Exception as e:
52 command_error ('search failed: %s' % str (e)) 44 command_error ('search failed: %s' % str (e))
53 #tried
54 #enddef

mercurial