| |
1 from modulecore import command_error |
| |
2 import hgapi |
| |
3 import urllib |
| |
4 import urllib2 |
| |
5 import json |
| |
6 |
| |
7 ModuleData = { |
| |
8 'commands': |
| |
9 [ |
| |
10 { |
| |
11 'name': 'idgames', |
| |
12 'description': 'Searches doomworld.com/idgames for wads', |
| |
13 'args': '<wad...>', |
| |
14 'level': 'normal', |
| |
15 }, |
| |
16 ] |
| |
17 } |
| |
18 |
| |
19 g_idgamesSearchURL = 'http://www.doomworld.com/idgames/api/api.php?action=search&query=%s&type=title&sort=date&out=json' |
| |
20 |
| |
21 def cmd_idgames (bot, args, replyto, **rest): |
| |
22 try: |
| |
23 url = g_idgamesSearchURL % urllib.quote (args['wad']) |
| |
24 response = urllib2.urlopen (url).read() |
| |
25 data = json.loads (response) |
| |
26 |
| |
27 if 'content' in data and 'file' in data['content']: |
| |
28 if type (data['content']['file']) is list: |
| |
29 files = data['content']['file'] |
| |
30 else: |
| |
31 files = [data['content']['file']] |
| |
32 |
| |
33 i = 0 |
| |
34 |
| |
35 for filedata in files: |
| |
36 if i >= 5: |
| |
37 break |
| |
38 |
| |
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']: |
| |
46 command_error (data['warning']['message']) |
| |
47 elif 'error' in data and 'message' in data['error']: |
| |
48 command_error (data['error']['message']) |
| |
49 else: |
| |
50 command_error ("Incomplete JSON response from doomworld.com/idgames") |
| |
51 except Exception as e: |
| |
52 command_error ('search failed: %s' % str (e)) |
| |
53 #tried |
| |
54 #enddef |