2018-10-24 01:39:07 +00:00
|
|
|
import sys
|
|
|
|
sys.path.append('..')
|
|
|
|
from scrython.foundation import FoundationObject
|
2018-10-22 15:52:30 +00:00
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
|
|
|
import urllib.parse
|
|
|
|
from threading import Thread
|
|
|
|
|
2018-10-24 01:39:07 +00:00
|
|
|
class BulkData(FoundationObject):
|
2018-10-22 15:52:30 +00:00
|
|
|
"""
|
2018-10-23 21:32:20 +00:00
|
|
|
/bulk-data
|
|
|
|
Queries and creates an object relating to the /bulk-data endpoint.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
Exception: Raised if Scryfall sends an error object.
|
|
|
|
KeyError: Raised if you attempt to access a key that doesn't exist.
|
|
|
|
KeyError: Raise if you attempt to access a tuple key that doesn't exist.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
object: The Scryfall endpoint object.
|
2018-10-22 15:52:30 +00:00
|
|
|
"""
|
2018-10-23 19:55:08 +00:00
|
|
|
def __init__(self, **kwargs):
|
2018-10-22 15:52:30 +00:00
|
|
|
|
2018-10-24 01:39:07 +00:00
|
|
|
self.url = 'https://api.scryfall.com/bulk-data'
|
|
|
|
super(BulkData, self).__init__(self.url, True)
|
2018-10-22 15:52:30 +00:00
|
|
|
|
|
|
|
def _checkForKey(self, key):
|
2018-10-23 21:32:20 +00:00
|
|
|
"""Checks for a key in the scryfallJson object.
|
|
|
|
This function should be considered private, and
|
|
|
|
should not be accessed in production.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
key (string): The key to check
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
KeyError: If key is not found.
|
|
|
|
"""
|
2018-10-22 15:52:30 +00:00
|
|
|
if not key in self.scryfallJson:
|
|
|
|
raise KeyError('This card has no key \'{}\''.format(key))
|
|
|
|
|
|
|
|
def _checkForTupleKey(self, parent, num, key):
|
2018-10-23 21:32:20 +00:00
|
|
|
"""Checks for a key of an object in an array.
|
|
|
|
This function should be considered private, and
|
|
|
|
should not be accessed in production.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
parent (string): The key for the array to be accessed
|
|
|
|
num (int): The index of the array
|
|
|
|
key (string): The key to check
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
KeyError: If key is not found.
|
|
|
|
"""
|
2018-10-22 15:52:30 +00:00
|
|
|
if not key in self.scryfallJson[parent][num]:
|
|
|
|
raise KeyError('This tuple has no key \'{}\''.format(key))
|
|
|
|
|
|
|
|
def object(self):
|
2018-10-23 21:32:20 +00:00
|
|
|
"""Returns the type of object it is.
|
|
|
|
(card, error, etc)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
string: The type of object
|
|
|
|
"""
|
2018-10-22 15:52:30 +00:00
|
|
|
self._checkForKey('object')
|
|
|
|
|
|
|
|
return self.scryfallJson['object']
|
|
|
|
|
|
|
|
def has_more(self):
|
2018-10-23 21:32:20 +00:00
|
|
|
"""True if there is more than one page of results
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
boolean: True if there are more results
|
|
|
|
"""
|
2018-10-22 15:52:30 +00:00
|
|
|
self._checkForKey('has_more')
|
|
|
|
|
|
|
|
return self.scryfallJson['has_more']
|
|
|
|
|
|
|
|
def data(self):
|
2018-10-23 21:32:20 +00:00
|
|
|
"""A list of all types of types returned by the endpoints
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list: List of all types
|
|
|
|
"""
|
2018-10-22 15:52:30 +00:00
|
|
|
self._checkForKey('data')
|
|
|
|
|
|
|
|
return self.scryfallJson['data']
|
|
|
|
|
|
|
|
def bulk_object(self, num):
|
2018-10-23 21:32:20 +00:00
|
|
|
"""Returns the type of object the specified index is
|
|
|
|
|
|
|
|
Args:
|
|
|
|
num (int): The index of the object in the `data` key
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
string: The type of object
|
|
|
|
"""
|
2018-10-23 19:55:08 +00:00
|
|
|
self._checkForTupleKey('data', num, 'object')
|
2018-10-22 15:52:30 +00:00
|
|
|
|
2018-10-23 19:55:08 +00:00
|
|
|
return self.scryfallJson['data'][num]['object']
|
2018-10-22 15:52:30 +00:00
|
|
|
|
|
|
|
def bulk_id(self, num):
|
2018-10-23 21:32:20 +00:00
|
|
|
"""The unique ID of the bulk item
|
|
|
|
|
|
|
|
Args:
|
|
|
|
num (int): The index of the object in the `data` key
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
string: The Scryfall id of the object
|
|
|
|
"""
|
2018-10-23 19:55:08 +00:00
|
|
|
self._checkForTupleKey('data', num, 'id')
|
2018-10-22 15:52:30 +00:00
|
|
|
|
2018-10-23 19:55:08 +00:00
|
|
|
return self.scryfallJson['data'][num]['id']
|
2018-10-22 15:52:30 +00:00
|
|
|
|
|
|
|
def bulk_type(self, num):
|
2018-10-23 21:32:20 +00:00
|
|
|
"""The type of bulk data
|
|
|
|
|
|
|
|
Args:
|
|
|
|
num (int): The index of the object in the `data` key
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
string: The type of the data item
|
|
|
|
"""
|
2018-10-23 19:55:08 +00:00
|
|
|
self._checkForTupleKey('data', num, 'type')
|
2018-10-22 15:52:30 +00:00
|
|
|
|
2018-10-23 19:55:08 +00:00
|
|
|
return self.scryfallJson['data'][num]['type']
|
2018-10-22 15:52:30 +00:00
|
|
|
|
|
|
|
def bulk_updated_at(self, num):
|
2018-10-23 21:32:20 +00:00
|
|
|
"""The time the item was last updated
|
|
|
|
|
|
|
|
Args:
|
|
|
|
num (int): The index of the object in the `data` key
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
string: Timestamp
|
|
|
|
"""
|
2018-10-23 19:55:08 +00:00
|
|
|
self._checkForTupleKey('data', num, 'updated_at')
|
2018-10-22 15:52:30 +00:00
|
|
|
|
2018-10-23 19:55:08 +00:00
|
|
|
return self.scryfallJson['data'][num]['updated_at']
|
2018-10-22 15:52:30 +00:00
|
|
|
|
|
|
|
def bulk_name(self, num):
|
2018-10-23 21:32:20 +00:00
|
|
|
"""The name of the type of bulk data object
|
|
|
|
|
|
|
|
Args:
|
|
|
|
num (int): The index of the object in the `data` key
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
string: The name of the data item
|
|
|
|
"""
|
2018-10-23 19:55:08 +00:00
|
|
|
self._checkForTupleKey('data', num, 'name')
|
2018-10-22 15:52:30 +00:00
|
|
|
|
2018-10-23 19:55:08 +00:00
|
|
|
return self.scryfallJson['data'][num]['name']
|
2018-10-22 15:52:30 +00:00
|
|
|
|
|
|
|
def bulk_description(self, num):
|
2018-10-23 21:32:20 +00:00
|
|
|
"""A description of the object
|
|
|
|
|
|
|
|
Args:
|
|
|
|
num (int): The index of the object in the `data` key
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
string: The description of the data item
|
|
|
|
"""
|
2018-10-23 19:55:08 +00:00
|
|
|
self._checkForTupleKey('data', num, 'description')
|
|
|
|
|
|
|
|
return self.scryfallJson['data'][num]['description']
|
|
|
|
|
|
|
|
def bulk_compressed_size(self, num, human_readable=False):
|
2018-10-23 21:32:20 +00:00
|
|
|
"""The size of the file in bytes
|
|
|
|
|
|
|
|
Args:
|
|
|
|
num (int): The index of the object in the `data` key
|
|
|
|
human_readable (bool, optional): Defaults to False. Converts the bytes into a human readable format
|
|
|
|
|
|
|
|
Returns:
|
2018-10-24 02:33:33 +00:00
|
|
|
integer: Returns integer by default.
|
|
|
|
string: If human_readable is True, returns a string.
|
2018-10-23 21:32:20 +00:00
|
|
|
"""
|
2018-10-23 19:55:08 +00:00
|
|
|
self._checkForTupleKey('data', num, 'compressed_size')
|
|
|
|
|
|
|
|
if human_readable:
|
|
|
|
before = self.scryfallJson['data'][num]['compressed_size']
|
2018-10-22 15:52:30 +00:00
|
|
|
|
2018-10-23 19:55:08 +00:00
|
|
|
for unit in ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB']:
|
|
|
|
if abs(before) < 1024.0:
|
2018-10-23 20:08:23 +00:00
|
|
|
return '{:3.1f}{}'.format(before, unit)
|
2018-10-23 19:55:08 +00:00
|
|
|
before /= 1024.0
|
2018-10-22 15:52:30 +00:00
|
|
|
|
2018-10-23 20:08:23 +00:00
|
|
|
return '{:.1f}{}'.format(before, 'YiB')
|
2018-10-22 15:52:30 +00:00
|
|
|
|
2018-10-23 19:55:08 +00:00
|
|
|
return self.scryfallJson['data'][num]['compressed_size']
|
2018-10-22 15:52:30 +00:00
|
|
|
|
|
|
|
def bulk_permalink_uri(self, num):
|
2018-10-23 21:32:20 +00:00
|
|
|
"""The URL that hosts the bulk file
|
|
|
|
|
|
|
|
Args:
|
|
|
|
num (int): The index of the object in the `data` key
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
string: A URI to download the compressed data
|
|
|
|
"""
|
2018-10-23 19:55:08 +00:00
|
|
|
self._checkForTupleKey('data', num, 'permalink_uri')
|
2018-10-22 15:52:30 +00:00
|
|
|
|
2018-10-23 19:55:08 +00:00
|
|
|
return self.scryfallJson['data'][num]['permalink_uri']
|
2018-10-22 15:52:30 +00:00
|
|
|
|
|
|
|
def bulk_content_type(self, num):
|
2018-10-23 21:32:20 +00:00
|
|
|
"""The MIME type of the file
|
|
|
|
|
|
|
|
Args:
|
|
|
|
num (int): The index of the object in the `data` key
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
string: The MIME type
|
|
|
|
"""
|
2018-10-23 19:55:08 +00:00
|
|
|
self._checkForTupleKey('data', num, 'content_type')
|
2018-10-22 15:52:30 +00:00
|
|
|
|
2018-10-23 19:55:08 +00:00
|
|
|
return self.scryfallJson['data'][num]['content_type']
|
2018-10-22 15:52:30 +00:00
|
|
|
|
|
|
|
def bulk_content_encoding(self, num):
|
2018-10-23 21:32:20 +00:00
|
|
|
"""The encoding of the file
|
|
|
|
|
|
|
|
Args:
|
|
|
|
num (int): The index of the object in the `data` key
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
string: The encoding of the file
|
|
|
|
"""
|
2018-10-23 19:55:08 +00:00
|
|
|
self._checkForTupleKey('data', num, 'content_encoding')
|
2018-10-22 15:52:30 +00:00
|
|
|
|
2018-10-23 19:55:08 +00:00
|
|
|
return self.scryfallJson['data'][num]['content_encoding']
|