From de546f3abb7b747703a3bbfaec0218accf775d04 Mon Sep 17 00:00:00 2001 From: Nanda Scott Date: Fri, 16 Feb 2018 19:06:09 -0500 Subject: [PATCH] Created sets modules. --- scrython/__init__.py | 3 + scrython/sets/__init__.py | 1 + scrython/sets/code.py | 6 ++ scrython/sets/sets_object.py | 114 +++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 scrython/sets/__init__.py create mode 100644 scrython/sets/code.py create mode 100644 scrython/sets/sets_object.py diff --git a/scrython/__init__.py b/scrython/__init__.py index e92d240..a01e0df 100644 --- a/scrython/__init__.py +++ b/scrython/__init__.py @@ -13,3 +13,6 @@ from scrython.rulings import Mtgo from scrython.rulings import Multiverse from scrython.rulings import Id from scrython.rulings import Code + +#Import classes from sets +from scrython.sets import Code diff --git a/scrython/sets/__init__.py b/scrython/sets/__init__.py new file mode 100644 index 0000000..95c792d --- /dev/null +++ b/scrython/sets/__init__.py @@ -0,0 +1 @@ +from .code import Code diff --git a/scrython/sets/code.py b/scrython/sets/code.py new file mode 100644 index 0000000..9ac8e7b --- /dev/null +++ b/scrython/sets/code.py @@ -0,0 +1,6 @@ +from .sets_object import SetsObject + +class Code(SetsObject): + def __init__(self, code): + self._url = 'sets/{}?'.format(code) + super(Code, self).__init__(self._url) diff --git a/scrython/sets/sets_object.py b/scrython/sets/sets_object.py new file mode 100644 index 0000000..0b1c8d5 --- /dev/null +++ b/scrython/sets/sets_object.py @@ -0,0 +1,114 @@ +import asyncio +import aiohttp +import urllib.parse + +class SetsObject(object): + def __init__(self, _url, **kwargs): + self.params = {'format': kwargs.get('format', 'json'), '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 + + 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) + + loop = asyncio.get_event_loop() + loop.run_until_complete(main(loop)) + + if self.scryfallJson['object'] == 'error': + raise Exception(self.scryfallJson['details']) + + def __checkForKey(self, key): + try: + return self.scryfallJson[key] + except KeyError: + return None + + def object(self): + if self.__checkForKey('object') is None: + raise KeyError('This object has no key \'object\'') + + return self.scryfallJson['object'] + + def code(self): + if self.__checkForKey('object') is None: + raise KeyError('This object has no key \'code\'') + + return self.scryfallJson['code'] + + def mtgo_code(self): + if self.__checkForKey('mtgo_code') is None: + raise KeyError('This object has no key \'mtgo_code\'') + + return self.scryfallJson['mtgo_code'] + + def name(self): + if self.__checkForKey('name') is None: + raise KeyError('This object has no key \'name\'') + + return self.scryfallJson['name'] + + def set_type(self): + if self.__checkForKey('set_type') is None: + raise KeyError('This object has no key \'set_type\'') + + return self.scryfallJson['set_type'] + + def released_at(self): + if self.__checkForKey('released_at') is None: + raise KeyError('This object has no key \'released_at\'') + + return self.scryfallJson['released_at'] + + def block_code(self): + if self.__checkForKey('block_code') is None: + raise KeyError('This object has no key \'block_code\'') + + return self.scryfallJson['block_code'] + + def block(self): + if self.__checkForKey('block') is None: + raise KeyError('This object has no key \'block\'') + + return self.scryfallJson['block'] + + def parent_set_code(self): + if self.__checkForKey('parent_set_code') is None: + raise KeyError('This object has no key \'parent_set_code\'') + + return self.scryfallJson['parent_set_code'] + + def card_count(self): + if self.__checkForKey('card_count') is None: + raise KeyError('This object has no key \'card_count\'') + + return self.scryfallJson['card_count'] + + def digital(self): + if self.__checkForKey('digital') is None: + raise KeyError('This object has no key \'digital\'') + + return self.scryfallJson['digital'] + + def foil(self): + if self.__checkForKey('foil') is None: + raise KeyError('This object has no key \'foil\'') + + return self.scryfallJson['foil'] + + def icon_svg_uri(self): + if self.__checkForKey('icon_svg_uri') is None: + raise KeyError('This object has no key \'icon_svg_uri\'') + + return self.scryfallJson['icon_svg_uri'] + + def search_uri(self): + if self.__checkForKey('search_uri') is None: + raise KeyError('This object has no key \'search_uri\'') + + return self.scryfallJson['search_uri']