2018-01-10 21:55:11 +00:00
|
|
|
import asyncio, aiohttp
|
|
|
|
|
2018-01-11 01:02:33 +00:00
|
|
|
class Autocomplete(object):
|
2018-01-31 00:32:39 +00:00
|
|
|
def __init__(self, query, **kwargs):
|
2018-01-10 21:55:11 +00:00
|
|
|
self.query = query
|
2018-02-09 22:46:42 +00:00
|
|
|
self.pretty = kwargs.get('pretty', 'None')
|
|
|
|
self.format = kwargs.get('format', 'None')
|
2018-01-10 21:55:11 +00:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
self.session = aiohttp.ClientSession(loop=loop)
|
|
|
|
|
|
|
|
async def getRequest(url, **kwargs):
|
|
|
|
async with self.session.get(url, **kwargs) as response:
|
|
|
|
return await response.json()
|
|
|
|
|
|
|
|
self.scryfallJson = loop.run_until_complete(getRequest(
|
|
|
|
url='https://api.scryfall.com/cards/autocomplete?',
|
|
|
|
params={'q':self.query, 'pretty':self.pretty, 'format':self.format}))
|
|
|
|
|
2018-01-11 03:04:03 +00:00
|
|
|
if self.scryfallJson['object'] == 'error':
|
|
|
|
self.session.close()
|
2018-01-30 15:30:14 +00:00
|
|
|
raise Exception(self.scryfallJson['details'])
|
2018-01-11 03:04:03 +00:00
|
|
|
|
2018-01-10 21:55:11 +00:00
|
|
|
self.session.close()
|
|
|
|
|
2018-01-30 15:30:14 +00:00
|
|
|
def __checkForKey(self, key):
|
|
|
|
try:
|
|
|
|
return self.scryfallJson[key]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
2018-01-10 21:55:11 +00:00
|
|
|
def object(self):
|
2018-01-30 15:30:14 +00:00
|
|
|
if self.__checkForKey('object') is None:
|
|
|
|
return KeyError('This card has no associated object key.')
|
|
|
|
|
2018-01-10 21:55:11 +00:00
|
|
|
return self.scryfallJson['object']
|
|
|
|
|
|
|
|
def total_items(self):
|
2018-01-30 15:30:14 +00:00
|
|
|
if self.__checkForKey('total_items') is None:
|
|
|
|
return KeyError('This card has no associated total items key.')
|
|
|
|
|
2018-01-10 21:55:11 +00:00
|
|
|
return self.scryfallJson['total_items']
|
|
|
|
|
|
|
|
def data(self):
|
2018-01-30 15:30:14 +00:00
|
|
|
if self.__checkForKey('data') is None:
|
|
|
|
return KeyError('This card has no associated data key.')
|
|
|
|
|
2018-01-10 21:55:11 +00:00
|
|
|
return self.scryfallJson['data']
|