2018-01-31 01:36:44 +00:00
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
2018-02-15 20:13:39 +00:00
|
|
|
import urllib.parse
|
2018-01-31 01:36:44 +00:00
|
|
|
|
|
|
|
class RulingsObject(object):
|
|
|
|
def __init__(self, _url, **kwargs):
|
2018-02-15 20:13:39 +00:00
|
|
|
self.params = {
|
|
|
|
'format': kwargs.get('format', 'json'), 'face': kwargs.get('face', ''),
|
|
|
|
'version': kwargs.get('version', ''), 'pretty': kwargs.get('pretty', '')
|
|
|
|
}
|
|
|
|
|
|
|
|
self.encodedParams = urllib.parse.urlencode(self.params)
|
|
|
|
self._url = 'https://api.scryfall.com/' + _url + "&" + self.encodedParams #Find a fix for this later
|
2018-01-31 01:36:44 +00:00
|
|
|
|
2018-02-15 20:13:39 +00:00
|
|
|
async def getRequest(client, url, **kwargs):
|
|
|
|
async with client.get(url, **kwargs) as response:
|
2018-01-31 01:36:44 +00:00
|
|
|
return await response.json()
|
|
|
|
|
2018-02-15 20:13:39 +00:00
|
|
|
async def main(loop):
|
|
|
|
async with aiohttp.ClientSession(loop=loop) as client:
|
|
|
|
self.scryfallJson = await getRequest(client, self._url)
|
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.run_until_complete(main(loop))
|
2018-01-31 01:36:44 +00:00
|
|
|
|
|
|
|
if self.scryfallJson['object'] == 'error':
|
|
|
|
raise Exception(self.scryfallJson['details'])
|
|
|
|
|
|
|
|
def __checkForKey(self, key):
|
|
|
|
try:
|
|
|
|
return self.scryfallJson[key]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
2018-01-31 02:17:18 +00:00
|
|
|
def __checkForTupleKey(self, parent, num, key):
|
|
|
|
try:
|
|
|
|
return self.scryfallJson[parent][num][key]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
2018-01-31 01:36:44 +00:00
|
|
|
def object(self):
|
|
|
|
if self.__checkForKey('object') is None:
|
2018-02-13 21:22:02 +00:00
|
|
|
return KeyError('This object has no key \'object\'')
|
2018-01-31 01:36:44 +00:00
|
|
|
|
|
|
|
return self.scryfallJson['object']
|
|
|
|
|
|
|
|
def has_more(self):
|
|
|
|
if self.__checkForKey('has_more') is None:
|
2018-02-13 21:22:02 +00:00
|
|
|
return KeyError('This object has no key \'has_more\'')
|
2018-01-31 01:36:44 +00:00
|
|
|
|
|
|
|
return self.scryfallJson['has_more']
|
|
|
|
|
|
|
|
def data(self):
|
|
|
|
if self.__checkForKey('data') is None:
|
2018-02-13 21:22:02 +00:00
|
|
|
return KeyError('This object has no key \'data\'')
|
2018-01-31 01:36:44 +00:00
|
|
|
|
|
|
|
return self.scryfallJson['data']
|
|
|
|
|
|
|
|
def data_length(self):
|
|
|
|
if self.__checkForKey('data') is None:
|
2018-02-13 21:22:02 +00:00
|
|
|
return KeyError('This object has no key \'data\'')
|
2018-01-31 01:36:44 +00:00
|
|
|
|
|
|
|
return len(self.scryfallJson['data'])
|
|
|
|
|
|
|
|
def ruling_object(self, num):
|
2018-01-31 02:17:18 +00:00
|
|
|
if self.__checkForTupleKey('data', num, 'object') is None:
|
2018-02-13 21:17:20 +00:00
|
|
|
return KeyError('This ruling has no key \'object\'')
|
2018-01-31 02:17:18 +00:00
|
|
|
|
2018-01-31 01:36:44 +00:00
|
|
|
return self.scryfallJson['data'][num]['object']
|
|
|
|
|
|
|
|
def ruling_source(self, num):
|
2018-01-31 02:17:18 +00:00
|
|
|
if self.__checkForTupleKey('data', num, 'source') is None:
|
2018-02-13 21:17:20 +00:00
|
|
|
return KeyError('This ruling has no key \'source\'')
|
2018-01-31 02:17:18 +00:00
|
|
|
|
2018-01-31 01:36:44 +00:00
|
|
|
return self.scryfallJson['data'][num]['source']
|
|
|
|
|
|
|
|
def ruling_published_at(self, num):
|
2018-01-31 02:17:18 +00:00
|
|
|
if self.__checkForTupleKey('data', num, 'published_at') is None:
|
2018-02-13 21:17:20 +00:00
|
|
|
return KeyError('This ruling has no key \'published_at\'')
|
2018-01-31 02:17:18 +00:00
|
|
|
|
2018-01-31 01:36:44 +00:00
|
|
|
return self.scryfallJson['data'][num]['published_at']
|
|
|
|
|
|
|
|
def ruling_comment(self, num):
|
2018-01-31 02:17:18 +00:00
|
|
|
if self.__checkForTupleKey('data', num, 'comment') is None:
|
2018-02-13 21:17:20 +00:00
|
|
|
return KeyError('This ruling has no key \'comment\'')
|
2018-01-31 02:17:18 +00:00
|
|
|
|
2018-01-31 01:36:44 +00:00
|
|
|
return self.scryfallJson['data'][num]['comment']
|