mod_idgames.py

Thu, 15 Jan 2015 19:06:14 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Thu, 15 Jan 2015 19:06:14 +0200
changeset 118
dbf49689af0d
parent 114
0d1fd111cb22
child 124
7b2cd8b1ba86
permissions
-rw-r--r--

- added bridging functionality

from modulecore import command_error
import hgapi
import urllib
import urllib2
import json

ModuleData = {
	'commands':
	[
		{
			'name': 'idgames',
			'description': 'Searches doomworld.com/idgames for wads',
			'args': '<wad...>',
			'level': 'normal',
		},
	]
}

g_idgamesSearchURL = 'http://www.doomworld.com/idgames/api/api.php?action=search&query=%s&type=title&sort=date&out=json'

def cmd_idgames (bot, args, reply, **rest):
	try:
		url = g_idgamesSearchURL % urllib.quote (args['wad'])
		response = urllib2.urlopen (url).read()
		data = json.loads (response)

		if 'content' in data and 'file' in data['content']:
			if type (data['content']['file']) is list:
				files = data['content']['file']
			else:
				files = [data['content']['file']]

			for filedata in files:
				reply ('- %s: \'%s\' by \'%s\', rating: %s: %s' % \
					(filedata['filename'], filedata['title'], filedata['author'],
					filedata['rating'], filedata['url']))
		elif 'warning' in data and 'message' in data['warning']:
			command_error (data['warning']['message'])
		elif 'error' in data and 'message' in data['error']:
			command_error (data['error']['message'])
		else:
			command_error ("Incomplete JSON response from doomworld.com/idgames")
	except Exception as e:
		command_error ('search failed: %s' % str (e))

mercurial