2018-10-24 01:39:07 +00:00
|
|
|
import sys
|
|
|
|
sys.path.append('..')
|
|
|
|
from scrython.foundation import FoundationObject
|
2018-02-13 20:58:28 +00:00
|
|
|
from .cards_object import CardsObject
|
2018-02-07 02:52:21 +00:00
|
|
|
import urllib.parse
|
|
|
|
|
2018-10-24 01:39:07 +00:00
|
|
|
class Search(FoundationObject):
|
2018-10-22 14:19:38 +00:00
|
|
|
"""
|
|
|
|
cards/search
|
|
|
|
Uses a search query to gather relevant data.
|
2018-02-21 19:42:58 +00:00
|
|
|
|
2018-10-24 00:54:28 +00:00
|
|
|
Args:
|
|
|
|
q (string):
|
|
|
|
The query to search. This will be updated in the future.
|
|
|
|
order (string, optional):
|
|
|
|
Defaults to 'none'
|
|
|
|
The order you'd like the data returned.
|
|
|
|
unique (string, optional):
|
|
|
|
Defaults to 'none'
|
|
|
|
A way to filter similar cards.
|
|
|
|
dir (string, optional)
|
|
|
|
Defaults to 'none'
|
|
|
|
The direction you'd like to sort. (asc, desc, auto)
|
|
|
|
include_extras (boolean, optional):
|
|
|
|
Defaults to 'false'
|
|
|
|
Includes cards that are normally omitted from search results, like Un-sets.
|
|
|
|
include_multilingual (boolean, optional):
|
|
|
|
Defaults to 'false'
|
|
|
|
Includes cards that are in the language specified. (en, ja, etc).
|
|
|
|
page (integer, optional):
|
|
|
|
Defaults to '1'
|
|
|
|
The page number you'd like to search, if any.
|
|
|
|
format (string, optional):
|
|
|
|
Defaults to 'json'.
|
|
|
|
Returns data in the specified method.
|
|
|
|
face (string, optional):
|
|
|
|
Defaults to empty string.
|
|
|
|
If you're using the `image` format,
|
|
|
|
this will specify if you want the front or back face.
|
|
|
|
version (string, optional):
|
|
|
|
Defaults to empty string.
|
|
|
|
If you're using the `image` format, this will specify
|
|
|
|
if you want the small, normal, large, etc version of the image.
|
|
|
|
pretty (string, optional):
|
|
|
|
Defaults to empty string.
|
|
|
|
Returns a prettier version of the json object.
|
|
|
|
Note that this may break functionality with Scrython.
|
|
|
|
|
2018-10-26 21:24:34 +00:00
|
|
|
Returns:
|
|
|
|
N/A
|
|
|
|
|
2018-10-24 00:54:28 +00:00
|
|
|
Raises:
|
|
|
|
Exception: If the 'q' parameter is not provided.
|
|
|
|
Exception: If the object returned is an error.
|
2018-02-21 19:42:58 +00:00
|
|
|
|
2018-10-28 02:24:01 +00:00
|
|
|
Examples:
|
2018-10-22 14:19:38 +00:00
|
|
|
>>> search = scrython.cards.Search(q="++e:A25", order="spoiled")
|
|
|
|
>>> search.data()
|
|
|
|
"""
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
if kwargs.get('q') is None:
|
2018-10-24 00:54:28 +00:00
|
|
|
raise Exception('No query is specified.')
|
2018-02-13 21:51:03 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
self.dict = {
|
|
|
|
'q':kwargs.get('q'),
|
|
|
|
'order':kwargs.get('order', 'none'),
|
|
|
|
'unique':kwargs.get('unique', 'none'),
|
|
|
|
'dir':kwargs.get('dir', 'none'),
|
|
|
|
'include_extras':kwargs.get('include_extras', 'false'),
|
2018-10-23 16:17:01 +00:00
|
|
|
'include_multilingual':kwargs.get('include_multilingual', 'false'),
|
2018-10-22 14:19:38 +00:00
|
|
|
'page':kwargs.get('page', '1')
|
|
|
|
}
|
2018-02-07 02:52:21 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
self.args = urllib.parse.urlencode(self.dict)
|
|
|
|
self.url = 'cards/search?' + self.args
|
2018-02-07 02:52:21 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
super(Search, self).__init__(self.url)
|
2018-02-07 02:52:21 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
def object(self):
|
2018-10-24 00:54:28 +00:00
|
|
|
"""Returns the type of object it is.
|
|
|
|
(card, error, etc)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
string: The type of object
|
|
|
|
"""
|
2018-10-22 14:19:38 +00:00
|
|
|
super(Search, self)._checkForKey('object')
|
2018-02-07 02:52:21 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
return self.scryfallJson['object']
|
2018-02-07 02:52:21 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
def total_cards(self):
|
2018-10-24 00:54:28 +00:00
|
|
|
"""How many cards are returned from the query
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
integer: The number of cards returned
|
|
|
|
"""
|
2018-10-22 14:19:38 +00:00
|
|
|
super(Search, self)._checkForKey('total_cards')
|
2018-02-07 02:52:21 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
return self.scryfallJson['total_cards']
|
2018-02-07 02:52:21 +00:00
|
|
|
|
2018-10-26 15:08:56 +00:00
|
|
|
def data(self, index=None, key=None):
|
2018-10-24 00:54:28 +00:00
|
|
|
"""The data returned from the query
|
2018-10-26 15:08:56 +00:00
|
|
|
|
|
|
|
You may reference any keys that could be accessed in a card object.
|
|
|
|
There are far too many to list here, but you may find a list if applicable
|
|
|
|
keys in the documentation.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
index (integer, optional): Defaults to None. Access a specific index.
|
|
|
|
key (string, optional): Defaults to None. Returns the value of the given key. Requires the `index` argument.
|
2018-10-24 00:54:28 +00:00
|
|
|
|
|
|
|
Returns:
|
2018-10-26 15:08:56 +00:00
|
|
|
List: The full list of data.
|
|
|
|
Dictionary: If given an index.
|
|
|
|
String: If given an index and key.
|
2018-10-24 00:54:28 +00:00
|
|
|
"""
|
2018-10-22 14:19:38 +00:00
|
|
|
super(Search, self)._checkForKey('data')
|
2018-02-07 02:52:21 +00:00
|
|
|
|
2018-10-26 15:08:56 +00:00
|
|
|
if index is not None:
|
|
|
|
if key is not None:
|
|
|
|
super(Search, self)._checkForTupleKey('data', index, key)
|
|
|
|
return self.scryfallJson['data'][index][key]
|
|
|
|
|
2018-10-29 21:19:05 +00:00
|
|
|
return self.scryfallJson['data'][index]
|
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
return self.scryfallJson['data']
|
2018-02-07 02:55:24 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
def next_page(self):
|
2018-10-24 00:54:28 +00:00
|
|
|
"""The API URI to the next page of the query
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
string: A URI to the next page of the query
|
|
|
|
"""
|
2018-10-22 14:19:38 +00:00
|
|
|
super(Search, self)._checkForKey('next_page')
|
2018-02-07 02:55:24 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
return self.scryfallJson['next_page']
|
2018-02-07 02:55:24 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
def data_length(self):
|
2018-10-24 00:54:28 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
integer: The length of data returned
|
|
|
|
"""
|
2018-10-22 14:19:38 +00:00
|
|
|
super(Search, self)._checkForKey('data')
|
2018-03-03 17:29:37 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
return len(self.scryfallJson['data'])
|
2018-03-03 17:29:37 +00:00
|
|
|
|
2018-10-22 14:19:38 +00:00
|
|
|
def has_more(self):
|
2018-10-24 00:54:28 +00:00
|
|
|
"""Determines if there are more pages of results.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
boolean: True if there is more than 1 page of results
|
|
|
|
"""
|
2018-10-22 14:19:38 +00:00
|
|
|
super(Search, self)._checkForKey('has_more')
|
2018-04-14 18:21:31 +00:00
|
|
|
|
2018-10-26 15:08:56 +00:00
|
|
|
return self.scryfallJson['has_more']
|