Scrython/scrython/rulings/rulings_object.py

109 lines
3.9 KiB
Python
Raw Normal View History

2018-01-31 01:36:44 +00:00
import asyncio
import aiohttp
import urllib.parse
2018-03-01 02:34:39 +00:00
from threading import Thread
2018-01-31 01:36:44 +00:00
class RulingsObject(object):
2018-10-22 14:19:38 +00:00
"""
Master class for all rulings objects.
2018-02-21 23:33:58 +00:00
2018-10-22 14:19:38 +00:00
Positional arguments:
No arguments required.
2018-02-21 23:33:58 +00:00
2018-10-22 14:19:38 +00:00
Optional arguments:
format : str ... Returns data in the specified method. Defaults to JSON.
face : str ... If you're using the `image` format, this will specify if
you want the front or back face.
version : str ... If you're using the `image` format, this will specify if
you want the small, normal, large, etc version of the image.
pretty : str ... Returns a prettier version of the json object. Note that
this may break functionality with Scrython.
2018-02-21 23:33:58 +00:00
2018-10-22 14:19:38 +00:00
Attributes:
object : str ...... Returns the type of object it is. (card, error, etc)
had_more : bool ... If true, this ruling object has more rules than it currently displays.
data : list .................................. A list of ruling objects.
data_length : int ....................... The length of the `data` list.
2018-02-21 23:33:58 +00:00
2018-10-22 14:19:38 +00:00
The following require an integer as an arg, which acts as a tuple.
ruling_object : str ............. The type of object for a given ruling.
ruling_source : str .......................... The source of the ruling.
ruling_published_at : str ...... The date when the ruling was published.
ruling_comment : str ............................. The effective ruling.
"""
def __init__(self, _url, **kwargs):
self.params = {
'format': kwargs.get('format', 'json'), 'face': kwargs.get('face', ''),
'version': kwargs.get('version', ''), 'pretty': kwargs.get('pretty', '')
}
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-01-31 01:36:44 +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-01-31 01:36:44 +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-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-01-31 01:36:44 +00:00
2018-10-22 14:19:38 +00:00
if self.scryfallJson['object'] == 'error':
raise Exception(self.scryfallJson['details'])
2018-01-31 01:36:44 +00:00
2018-10-22 14:19:38 +00:00
def _checkForKey(self, key):
if not key in self.scryfallJson:
raise KeyError('This object has no key \'{}\''.format(key))
2018-01-31 01:36:44 +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 ruling has no key \'{}\''.format(key))
2018-10-22 14:19:38 +00:00
def object(self):
self._checkForKey('object')
2018-01-31 01:36:44 +00:00
2018-10-22 14:19:38 +00:00
return self.scryfallJson['object']
2018-01-31 01:36:44 +00:00
2018-10-22 14:19:38 +00:00
def has_more(self):
self._checkForKey('has_more')
2018-01-31 01:36:44 +00:00
2018-10-22 14:19:38 +00:00
return self.scryfallJson['has_more']
2018-01-31 01:36:44 +00:00
2018-10-22 14:19:38 +00:00
def data(self):
self._checkForKey('data')
2018-01-31 01:36:44 +00:00
2018-10-22 14:19:38 +00:00
return self.scryfallJson['data']
2018-01-31 01:36:44 +00:00
2018-10-22 14:19:38 +00:00
def data_length(self):
self._checkForKey('data')
2018-01-31 01:36:44 +00:00
2018-10-22 14:19:38 +00:00
return len(self.scryfallJson['data'])
2018-01-31 01:36:44 +00:00
2018-10-22 14:19:38 +00:00
def ruling_object(self, num):
self._checkForTupleKey('data', num, 'object')
2018-10-22 14:19:38 +00:00
return self.scryfallJson['data'][num]['object']
2018-01-31 01:36:44 +00:00
2018-10-22 14:19:38 +00:00
def ruling_source(self, num):
self._checkForTupleKey('data', num, 'source')
2018-10-22 14:19:38 +00:00
return self.scryfallJson['data'][num]['source']
2018-01-31 01:36:44 +00:00
2018-10-22 14:19:38 +00:00
def ruling_published_at(self, num):
self._checkForTupleKey('data', num, 'published_at')
2018-10-22 14:19:38 +00:00
return self.scryfallJson['data'][num]['published_at']
2018-01-31 01:36:44 +00:00
2018-10-22 14:19:38 +00:00
def ruling_comment(self, num):
self._checkForTupleKey('data', num, 'comment')
2018-10-22 14:19:38 +00:00
return self.scryfallJson['data'][num]['comment']