2018-02-17 01:43:56 +00:00
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
|
|
|
import urllib.parse
|
2018-03-01 02:34:39 +00:00
|
|
|
from threading import Thread
|
2018-02-17 01:43:56 +00:00
|
|
|
|
|
|
|
class CatalogsObject(object):
|
2018-02-21 23:13:55 +00:00
|
|
|
"""
|
|
|
|
Master object for all catalog objects.
|
|
|
|
|
|
|
|
Positional Arguments:
|
|
|
|
No arguments are required.
|
|
|
|
|
|
|
|
Optional Arguments:
|
2018-02-22 00:00:01 +00:00
|
|
|
format : str ................... The format to return. Defaults to JSON.
|
2018-02-21 23:13:55 +00:00
|
|
|
pretty : bool ... Makes the returned JSON prettier. The library may not work properly with this setting.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
object : str ...... Returns the type of object it is. (card, error, etc)
|
|
|
|
uri : str .................. The API URI for the endpoint you've called.
|
|
|
|
total_values : int ..................... The number of items in `data()`
|
|
|
|
data : list .............. A list of all types returned by the endpoint.
|
|
|
|
"""
|
2018-02-17 01:43:56 +00:00
|
|
|
def __init__(self, _url, **kwargs):
|
|
|
|
self.params = {'format': kwargs.get('format', 'json'), 'pretty': kwargs.get('pretty', '')}
|
|
|
|
|
|
|
|
self.encodedParams = urllib.parse.urlencode(self.params)
|
2018-03-04 01:41:33 +00:00
|
|
|
self._url = 'https://api.scryfall.com/{0}&{1}'.format(_url, self.encodedParams)
|
2018-02-17 01:43:56 +00:00
|
|
|
|
|
|
|
async def getRequest(client, url, **kwargs):
|
|
|
|
async with client.get(url, **kwargs) as response:
|
|
|
|
return await response.json()
|
|
|
|
|
|
|
|
async def main(loop):
|
|
|
|
async with aiohttp.ClientSession(loop=loop) as client:
|
|
|
|
self.scryfallJson = await getRequest(client, self._url)
|
|
|
|
|
2018-03-01 02:34:39 +00:00
|
|
|
def do_everything():
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
loop.run_until_complete(main(loop))
|
|
|
|
|
|
|
|
t = Thread(target=do_everything)
|
|
|
|
t.run()
|
2018-02-17 01:43:56 +00:00
|
|
|
|
|
|
|
if self.scryfallJson['object'] == 'error':
|
|
|
|
raise Exception(self.scryfallJson['details'])
|
|
|
|
|
2018-03-03 23:09:56 +00:00
|
|
|
def _checkForKey(self, key):
|
2018-02-17 01:43:56 +00:00
|
|
|
try:
|
|
|
|
return self.scryfallJson[key]
|
2018-03-03 23:09:56 +00:00
|
|
|
except Exception:
|
|
|
|
raise KeyError('This card has no key \'{}\''.format(key))
|
2018-02-17 01:43:56 +00:00
|
|
|
|
|
|
|
def object(self):
|
2018-03-03 23:09:56 +00:00
|
|
|
self._checkForKey('object')
|
2018-02-17 01:43:56 +00:00
|
|
|
|
|
|
|
return self.scryfallJson['object']
|
|
|
|
|
|
|
|
def uri(self):
|
2018-03-03 23:09:56 +00:00
|
|
|
self._checkForKey('uri')
|
2018-02-17 01:43:56 +00:00
|
|
|
|
|
|
|
return self.scryfallJson['uri']
|
|
|
|
|
|
|
|
def total_values(self):
|
2018-03-03 23:09:56 +00:00
|
|
|
self._checkForKey('total_values')
|
2018-02-17 01:43:56 +00:00
|
|
|
|
|
|
|
return self.scryfallJson['total_values']
|
|
|
|
|
|
|
|
def data(self):
|
2018-03-03 23:09:56 +00:00
|
|
|
self._checkForKey('data')
|
2018-02-17 01:43:56 +00:00
|
|
|
|
|
|
|
return self.scryfallJson['data']
|