2018-02-19 17:20:05 +00:00
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
|
|
|
import urllib.parse
|
2018-03-01 02:34:39 +00:00
|
|
|
from threading import Thread
|
2018-02-19 17:20:05 +00:00
|
|
|
|
|
|
|
class SymbologyObject(object):
|
2018-10-22 14:19:38 +00:00
|
|
|
"""
|
|
|
|
The master class for all symbology objects.
|
2018-02-22 00:17:22 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
Positional arguments:
|
|
|
|
No arguments required.
|
2018-02-22 00:17:22 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
Optional arguments:
|
|
|
|
format : str ................... The format to return. Defaults to JSON.
|
|
|
|
pretty : bool ... Makes the returned JSON prettier. The library may not work properly with this setting.
|
2018-02-22 00:17:22 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
Attributes:
|
|
|
|
No attributes to call.
|
|
|
|
"""
|
|
|
|
def __init__(self, _url, **kwargs):
|
|
|
|
self.params = {'format': kwargs.get('format', 'json'), 'pretty': kwargs.get('pretty', '')}
|
2018-02-19 17:20:05 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
self.encodedParams = urllib.parse.urlencode(self.params)
|
|
|
|
self._url = 'https://api.scryfall.com/{0}&{1}'.format(_url, self.encodedParams)
|
2018-02-19 17:20:05 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
async def getRequest(client, url, **kwargs):
|
|
|
|
async with client.get(url, **kwargs) as response:
|
|
|
|
return await response.json()
|
2018-02-19 17:20:05 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
async def main(loop):
|
|
|
|
async with aiohttp.ClientSession(loop=loop) as client:
|
|
|
|
self.scryfallJson = await getRequest(client, self._url)
|
2018-02-19 17:20:05 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
def do_everything():
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
loop.run_until_complete(main(loop))
|
2018-03-01 02:34:39 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
t = Thread(target=do_everything)
|
|
|
|
t.run()
|
2018-02-19 17:20:05 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
if self.scryfallJson['object'] == 'error':
|
|
|
|
raise Exception(self.scryfallJson['details'])
|
2018-04-19 00:55:11 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
def _checkForKey(self, key):
|
|
|
|
if not key in self.scryfallJson:
|
|
|
|
raise KeyError('This object ahs no key \'{}\''.format(key))
|
2018-04-19 00:55:11 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
def _checkForTupleKey(self, parent, num, key):
|
|
|
|
if not key in self.scryfallJson[parent][num]:
|
|
|
|
raise KeyError('This object has no key \'{}\''.format(key))
|