Scrython/scrython/bulk_data/bulk_data.py

209 lines
6.0 KiB
Python
Raw Normal View History

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
2020-06-09 03:48:40 +00:00
import warnings
2018-10-22 15:52:30 +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.
Args:
N/A
2018-10-23 21:32:20 +00:00
Returns:
object: The Scryfall endpoint object.
Raises:
Exception: Raised if Scryfall sends an error object.
Examples:
>>> data = scrython.bulk_data.BulkData()
>>> data.bulk_compressed_size()
2018-10-22 15:52:30 +00:00
"""
def __init__(self, **kwargs):
2018-10-22 15:52:30 +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 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-26 14:57:37 +00:00
super(BulkData, self)._checkForKey('object')
2018-10-22 15:52:30 +00:00
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-26 14:57:37 +00:00
super(BulkData, self)._checkForKey('has_more')
2018-10-22 15:52:30 +00:00
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-26 14:57:37 +00:00
super(BulkData, self)._checkForKey('data')
2018-10-22 15:52:30 +00:00
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-26 14:57:37 +00:00
super(BulkData, self)._checkForTupleKey('data', num, 'object')
2018-10-22 15:52:30 +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-26 14:57:37 +00:00
super(BulkData, self)._checkForTupleKey('data', num, 'id')
2018-10-22 15:52:30 +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-26 14:57:37 +00:00
super(BulkData, self)._checkForTupleKey('data', num, 'type')
2018-10-22 15:52:30 +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-26 14:57:37 +00:00
super(BulkData, self)._checkForTupleKey('data', num, 'updated_at')
2018-10-22 15:52:30 +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-26 14:57:37 +00:00
super(BulkData, self)._checkForTupleKey('data', num, 'name')
2018-10-22 15:52:30 +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-26 14:57:37 +00:00
super(BulkData, 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-26 14:57:37 +00:00
super(BulkData, self)._checkForTupleKey('data', num, 'compressed_size')
if human_readable:
before = self.scryfallJson['data'][num]['compressed_size']
2018-10-22 15:52:30 +00:00
for unit in ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB']:
if abs(before) < 1024.0:
return '{:3.1f}{}'.format(before, unit)
before /= 1024.0
2018-10-22 15:52:30 +00:00
return '{:.1f}{}'.format(before, 'YiB')
2018-10-22 15:52:30 +00:00
return self.scryfallJson['data'][num]['compressed_size']
2018-10-22 15:52:30 +00:00
def bulk_permalink_uri(self, num):
2020-06-09 03:48:40 +00:00
warnings.warn("This method has been renamed to bulk_uri as per https://scryfall.com/blog/updates-to-bulk-data-and-cards-deprecation-notice-217", DeprecationWarning)
return self.bulk_uri(num)
def bulk_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
"""
2020-06-09 03:48:40 +00:00
super(BulkData, self)._checkForTupleKey('data', num, 'uri')
2018-10-22 15:52:30 +00:00
2020-06-09 03:48:40 +00:00
return self.scryfallJson['data'][num]['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-26 14:57:37 +00:00
super(BulkData, self)._checkForTupleKey('data', num, 'content_type')
2018-10-22 15:52:30 +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-26 14:57:37 +00:00
super(BulkData, self)._checkForTupleKey('data', num, 'content_encoding')
2018-10-22 15:52:30 +00:00
return self.scryfallJson['data'][num]['content_encoding']