Created rulings modules.

This commit is contained in:
Nanda Scott 2018-01-30 20:36:44 -05:00
parent d4cec35d16
commit 1a41ec72d5
5 changed files with 103 additions and 0 deletions

8
rulings/mtgo.py Normal file
View File

@ -0,0 +1,8 @@
from rulings_object import RulingsObject
class Mtgo(RulingsObject):
"""docstring for Mtgo."""
def __init__(self, _id):
self.id = _id
self.url = 'cards/mtgo/{}/rulings'.format(self.id)
super(Mtgo, self).__init__(self.url)

8
rulings/multiverse_id.py Normal file
View File

@ -0,0 +1,8 @@
from rulings_object import RulingsObject
class Multiverse(RulingsObject):
"""docstring for Multiverse."""
def __init__(self, _id):
self.id = str(_id)
self.url = 'cards/multiverse/{}/rulings'.format(self.id)
super(Multiverse, self).__init__(self.url)

70
rulings/rulings_object.py Normal file
View File

@ -0,0 +1,70 @@
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 object(self):
if self.__checkForKey('object') is None:
return KeyError('This ruling has no associated object key.')
return self.scryfallJson['object']
def has_more(self):
if self.__checkForKey('has_more') is None:
return KeyError('This ruling has no associated has_more key.')
return self.scryfallJson['has_more']
def data(self):
if self.__checkForKey('data') is None:
return KeyError('This ruling has no associated data key.')
return self.scryfallJson['data']
def data_length(self):
if self.__checkForKey('data') is None:
return KeyError('This ruling has no associated data key.')
return len(self.scryfallJson['data'])
def ruling_object(self, num):
return self.scryfallJson['data'][num]['object']
def ruling_source(self, num):
return self.scryfallJson['data'][num]['source']
def ruling_published_at(self, num):
return self.scryfallJson['data'][num]['published_at']
def ruling_comment(self, num):
return self.scryfallJson['data'][num]['comment']

8
rulings/scryfall_id.py Normal file
View File

@ -0,0 +1,8 @@
from rulings_object import RulingsObject
class Id(RulingsObject):
"""docstring for Id."""
def __init__(self, _id):
self.id = str(_id)
self.url = 'cards/{}/rulings'.format(self.id)
super(Id, self).__init__(self.url)

9
rulings/set_code.py Normal file
View File

@ -0,0 +1,9 @@
from rulings_object import RulingsObject
class Code(RulingsObject):
"""docstring for Code."""
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)