mod_idgames.py

Fri, 24 Apr 2015 06:40:08 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Fri, 24 Apr 2015 06:40:08 +0300
changeset 131
7349e9c6176b
parent 124
7b2cd8b1ba86
child 144
b3d1b356e544
permissions
-rw-r--r--

Added sgn(x) to .calc

'''
	Copyright 2014-2015 Teemu Piippo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. The name of the author may not be used to endorse or promote products
	   derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''

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