diff --git a/__init__.py b/__init__.py index 2855c22..2b79c3d 100644 --- a/__init__.py +++ b/__init__.py @@ -1,7 +1,14 @@ +#Import classes from cards from scrython.cards import Autocomplete from scrython.cards import Collector from scrython.cards import Id from scrython.cards import Mtgo from scrython.cards import Multiverse from scrython.cards import Named -from scrython.cards import RandomCard +from scrython.cards import Random + +#Import classes from rulings +from scrython.rulings import Mtgo +from scrython.rulings import Multiverse +from scrython.rulings import Id +from scrython.rulings import Code diff --git a/rulings/__init__.py b/rulings/__init__.py new file mode 100644 index 0000000..a08cf90 --- /dev/null +++ b/rulings/__init__.py @@ -0,0 +1,5 @@ +from .mtgo import Mtgo +from .multiverse_id import Multiverse +from .rulings_object import RulingsObject +from .scryfall_id import Id +from .set_code import Code diff --git a/rulings/mtgo.py b/rulings/mtgo.py new file mode 100644 index 0000000..b0ab7ed --- /dev/null +++ b/rulings/mtgo.py @@ -0,0 +1,7 @@ +from .rulings_object import RulingsObject + +class Mtgo(RulingsObject): + def __init__(self, _id): + self.id = _id + self.url = 'cards/mtgo/{}/rulings'.format(self.id) + super(Mtgo, self).__init__(self.url) diff --git a/rulings/multiverse_id.py b/rulings/multiverse_id.py new file mode 100644 index 0000000..b290c4f --- /dev/null +++ b/rulings/multiverse_id.py @@ -0,0 +1,7 @@ +from .rulings_object import RulingsObject + +class Multiverse(RulingsObject): + def __init__(self, _id): + self.id = str(_id) + self.url = 'cards/multiverse/{}/rulings'.format(self.id) + super(Multiverse, self).__init__(self.url) diff --git a/rulings/rulings_object.py b/rulings/rulings_object.py new file mode 100644 index 0000000..dd433cc --- /dev/null +++ b/rulings/rulings_object.py @@ -0,0 +1,87 @@ +import asyncio +import aiohttp + +class RulingsObject(object): + def __init__(self, _url, **kwargs): + self.pretty = kwargs.get('pretty') + self.format = kwargs.get('format') + self._url = 'https://api.scryfall.com/' + _url + 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 = self._url, + params={ + 'format': self.format, + 'pretty': self.pretty + })) + + if self.scryfallJson['object'] == 'error': + self.session.close() + raise Exception(self.scryfallJson['details']) + + self.session.close() + + def __checkForKey(self, key): + try: + return self.scryfallJson[key] + except KeyError: + return None + + def __checkForTupleKey(self, parent, num, key): + try: + return self.scryfallJson[parent][num][key] + except KeyError: + return None + + def object(self): + if self.__checkForKey('object') is None: + return KeyError('This ruling object has no associated object key.') + + return self.scryfallJson['object'] + + def has_more(self): + if self.__checkForKey('has_more') is None: + return KeyError('This ruling object has no associated has_more key.') + + return self.scryfallJson['has_more'] + + def data(self): + if self.__checkForKey('data') is None: + return KeyError('This ruling object has no associated data key.') + + return self.scryfallJson['data'] + + def data_length(self): + if self.__checkForKey('data') is None: + return KeyError('This ruling object has no associated data key.') + + return len(self.scryfallJson['data']) + + def ruling_object(self, num): + if self.__checkForTupleKey('data', num, 'object') is None: + return KeyError('This ruling has no object key.') + + return self.scryfallJson['data'][num]['object'] + + def ruling_source(self, num): + if self.__checkForTupleKey('data', num, 'source') is None: + return KeyError('This ruling has no source key.') + + return self.scryfallJson['data'][num]['source'] + + def ruling_published_at(self, num): + if self.__checkForTupleKey('data', num, 'published_at') is None: + return KeyError('This ruling has no published_at key.') + + return self.scryfallJson['data'][num]['published_at'] + + def ruling_comment(self, num): + if self.__checkForTupleKey('data', num, 'comment') is None: + return KeyError('This ruling has no comment key.') + + return self.scryfallJson['data'][num]['comment'] diff --git a/rulings/scryfall_id.py b/rulings/scryfall_id.py new file mode 100644 index 0000000..04757de --- /dev/null +++ b/rulings/scryfall_id.py @@ -0,0 +1,7 @@ +from .rulings_object import RulingsObject + +class Id(RulingsObject): + def __init__(self, _id): + self.id = str(_id) + self.url = 'cards/{}/rulings'.format(self.id) + super(Id, self).__init__(self.url) diff --git a/rulings/set_code.py b/rulings/set_code.py new file mode 100644 index 0000000..08994b0 --- /dev/null +++ b/rulings/set_code.py @@ -0,0 +1,8 @@ +from .rulings_object import RulingsObject + +class Code(RulingsObject): + def __init__(self, code, number): + self.code = code.lower() + self.number = str(number) + self.url = 'cards/{}/{}/rulings'.format(self.code, self.number) + super(Code, self).__init__(self.url)