From 958c332e6579e11d6d93143d923569748993a8cc Mon Sep 17 00:00:00 2001 From: Nanda Scott Date: Tue, 13 Feb 2018 21:02:48 -0500 Subject: [PATCH] Changed a whole bunch of stuff. --- .gitignore | 1 + examples/get_and_format_card.py | 2 +- examples/spoiler_season.py | 76 +++++++++++++++++++++++++++++++ scrython/cards/cardid.py | 2 +- scrython/cards/cards_object.py | 21 +++++---- scrython/cards/collector.py | 2 +- scrython/cards/mtgo.py | 3 +- scrython/cards/multiverse.py | 3 +- scrython/cards/named.py | 10 ++-- scrython/cards/randomcard.py | 2 +- scrython/rulings/mtgo.py | 2 +- scrython/rulings/multiverse_id.py | 2 +- scrython/rulings/scryfall_id.py | 2 +- setup.py | 2 +- 14 files changed, 107 insertions(+), 23 deletions(-) create mode 100644 examples/spoiler_season.py diff --git a/.gitignore b/.gitignore index d919bb9..1997047 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__ .ropeproject scrython.egg-info dist +tests diff --git a/examples/get_and_format_card.py b/examples/get_and_format_card.py index dd3ab3c..57c03fe 100644 --- a/examples/get_and_format_card.py +++ b/examples/get_and_format_card.py @@ -1,6 +1,6 @@ import scrython -getCard = input("What card would you like me to search? ") +getCard = str(input("What card would you like me to search? ")) card = scrython.cards.Named(fuzzy=getCard) diff --git a/examples/spoiler_season.py b/examples/spoiler_season.py new file mode 100644 index 0000000..992cdef --- /dev/null +++ b/examples/spoiler_season.py @@ -0,0 +1,76 @@ +import scrython, time + +def keyCheck(key, _dict): + try: + _dict[key] + return True + except KeyError: + return False + +def nameBar(name, manaCost): + return "{} {}".format(name, manaCost) + +def typeBar(typeLine, rarity): + return "{} | {}".format(typeLine, rarity[:1].upper()) + +def powerAndToughness(power, toughness): + return "{}/{}".format(power, toughness) + +query = input("Enter a set: ") + +currentSetSize = 0 +lastList = [] +currentList = [] + +while query is not None: + + #Grab the data + spoilers = scrython.cards.Search(q="++e:{}".format(query), order='spoiled') + + #If the total cards has increased + if spoilers.total_cards() > currentSetSize: + + #Dump if currentList already exists + if currentList: + del currentList[:] + + #If there aren't enough spoilers we iterate through the whole list. + #Otherwise we only want the first 15 + if spoilers.total_cards() > 15: + maxIteration = 15 + else: + maxIteration = spoilers.total_cards() + + for i in range(maxIteration): + currentList.append(spoilers.data()[i]['id']) + + #Iterate through data + for card in reversed(spoilers.data()): + if card['id'] in lastList: + continue + print("~~~~~~~~~~\nNew card:") + + #Grab the relevant keys + if keyCheck('card_faces', card): + print(nameBar(card['name'], card['card_faces'][0]['mana_cost'])) + print(typeBar(card['card_faces'][0]['mana_cost'])) + else: + print(nameBar(card['name'], card['mana_cost'])) + print(typeBar(card['type_line'], card['rarity'])) + + if keyCheck('oracle_text', card): + print(card['oracle_text']) + + if keyCheck('power', card) and keyCheck('toughness', card): + print(powerAndToughness(card['power'], card['toughness'])) + + #Update card count + currentSetSize = spoilers.total_cards() + #Dump all in last list + if lastList: + del lastList[:] + #Add the first 15 ids in current list to last list + for i in range(maxIteration): + lastList.append(currentList[i]) + + time.sleep(300) diff --git a/scrython/cards/cardid.py b/scrython/cards/cardid.py index 4afef2e..d5390d0 100644 --- a/scrython/cards/cardid.py +++ b/scrython/cards/cardid.py @@ -5,5 +5,5 @@ class Id(CardsObject): if kwargs.get('id') is None: raise TypeError('No id provided to search by') - self.url = 'cards/' + str(kwargs.get('id')) + self.url = 'cards/{}?'.format(str(kwargs.get('id'))) super(Id, self).__init__(self.url) diff --git a/scrython/cards/cards_object.py b/scrython/cards/cards_object.py index fba562a..9fbe00f 100644 --- a/scrython/cards/cards_object.py +++ b/scrython/cards/cards_object.py @@ -1,9 +1,17 @@ import aiohttp import asyncio +import urllib.parse class CardsObject(object): def __init__(self, _url, **kwargs): - self._url = 'https://api.scryfall.com/' + _url + + self.params = { + 'format': kwargs.get('format', 'json'), 'face': kwargs.get('face', ''), + 'version': kwargs.get('version', ''), 'pretty': kwargs.get('pretty', '') + } + + self.encodedParams = urllib.parse.urlencode(self.params) + self._url = 'https://api.scryfall.com/' + _url + "&" + self.encodedParams #Find a fix for this later loop = asyncio.get_event_loop() self.session = aiohttp.ClientSession(loop=loop) @@ -11,14 +19,7 @@ class CardsObject(object): 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': kwargs.get('format', 'None'), - 'face': kwargs.get('face', 'None'), - 'version': kwargs.get('version', 'None'), - 'pretty': kwargs.get('pretty', 'None') - })) + self.scryfallJson = loop.run_until_complete(getRequest(url = self._url)) if self.scryfallJson['object'] == 'error': self.session.close() @@ -154,7 +155,7 @@ class CardsObject(object): def set_code(self): if self.__checkForKey('set') is None: - raise KeyError("This card has no key \'set_code\'") + raise KeyError("This card has no key \'set\'") return self.scryfallJson['set'] diff --git a/scrython/cards/collector.py b/scrython/cards/collector.py index e09b62c..5e8e54e 100644 --- a/scrython/cards/collector.py +++ b/scrython/cards/collector.py @@ -5,5 +5,5 @@ class Collector(CardsObject): if kwargs.get('code') is None: raise TypeError('No code provided to search by') - self.url = 'cards/{}/{}'.format(kwargs.get('code'), str(kwargs.get('collector_number'))) + self.url = 'cards/{}/{}?'.format(kwargs.get('code'), str(kwargs.get('collector_number'))) super(Collector, self).__init__(self.url) diff --git a/scrython/cards/mtgo.py b/scrython/cards/mtgo.py index 0875ba2..694262e 100644 --- a/scrython/cards/mtgo.py +++ b/scrython/cards/mtgo.py @@ -5,5 +5,6 @@ class Mtgo(CardsObject): def __init__(self, **kwargs): if kwargs.get('id') is None: raise TypeError('No id provided to search by') - self.url = 'cards/mtgo/' + str(kwargs.get('id')) + + self.url = 'cards/mtgo/{}?'.format(str(kwargs.get('id'))) super(Mtgo, self).__init__(self.url) diff --git a/scrython/cards/multiverse.py b/scrython/cards/multiverse.py index 85c325b..819ae44 100644 --- a/scrython/cards/multiverse.py +++ b/scrython/cards/multiverse.py @@ -4,5 +4,6 @@ class Multiverse(CardsObject): def __init__(self, **kwargs): if kwargs.get('id') is None: raise TypeError('No id provided to search by') - self.url = 'cards/multiverse/' + kwargs.get('id') + + self.url = 'cards/multiverse/{}?'.format(str(kwargs.get('id'))) super(Multiverse, self).__init__(self.url) diff --git a/scrython/cards/named.py b/scrython/cards/named.py index f30549b..c28a42f 100644 --- a/scrython/cards/named.py +++ b/scrython/cards/named.py @@ -4,11 +4,15 @@ import urllib.parse class Named(CardsObject): def __init__(self, **kwargs): self.dict = { - 'exact':kwargs.get('exact', ''), - 'fuzzy':kwargs.get('fuzzy', ''), - 'set':kwargs.get('set', 'none') + 'set':kwargs.get('set', '') } + if kwargs.get('exact') is not None: + self.dict['exact'] = kwargs.get('exact') + + if kwargs.get('fuzzy') is not None: + self.dict['fuzzy'] = kwargs.get('fuzzy') + self.args = urllib.parse.urlencode(self.dict) self.url = 'cards/named?' + self.args super(Named, self).__init__(self.url) diff --git a/scrython/cards/randomcard.py b/scrython/cards/randomcard.py index cf1f8af..7b2ea82 100644 --- a/scrython/cards/randomcard.py +++ b/scrython/cards/randomcard.py @@ -3,5 +3,5 @@ from .cards_object import CardsObject class Random(CardsObject): def __init__(self): - self.url = 'cards/random' + self.url = 'cards/random?' super(Random, self).__init__(self.url) diff --git a/scrython/rulings/mtgo.py b/scrython/rulings/mtgo.py index 7ddc9ce..21fc15d 100644 --- a/scrython/rulings/mtgo.py +++ b/scrython/rulings/mtgo.py @@ -3,7 +3,7 @@ from .rulings_object import RulingsObject class Mtgo(RulingsObject): def __init__(self, **kwargs): if kwargs.get('id') is None: - raise TypeError('No id provided to search by') + raise TypeError('No id provided to search by') self.url = 'cards/mtgo/{}/rulings'.format(str(kwargs.get('id'))) super(Mtgo, self).__init__(self.url) diff --git a/scrython/rulings/multiverse_id.py b/scrython/rulings/multiverse_id.py index 97e6a5c..c7de8d2 100644 --- a/scrython/rulings/multiverse_id.py +++ b/scrython/rulings/multiverse_id.py @@ -3,7 +3,7 @@ from .rulings_object import RulingsObject class Multiverse(RulingsObject): def __init__(self, **kwargs): if kwargs.get('id') is None: - raise TypeError('No id provided to search by') + raise TypeError('No id provided to search by') self.url = 'cards/multiverse/{}/rulings'.format(str(kwargs.get('id'))) super(Multiverse, self).__init__(self.url) diff --git a/scrython/rulings/scryfall_id.py b/scrython/rulings/scryfall_id.py index efe2433..9bbcf3a 100644 --- a/scrython/rulings/scryfall_id.py +++ b/scrython/rulings/scryfall_id.py @@ -3,7 +3,7 @@ from .rulings_object import RulingsObject class Id(RulingsObject): def __init__(self, **kwargs): if kwargs.get('id') is None: - raise TypeError('No id provided to search by') + raise TypeError('No id provided to search by') self.url = 'cards/{}/rulings'.format(str(kwargs.get('id'))) super(Id, self).__init__(self.url) diff --git a/setup.py b/setup.py index fa533f1..6ea0427 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open('README.md') as f: setup( name='scrython', packages=['scrython', 'scrython.cards', 'scrython.rulings'], - version='0.2.0', + version='0.4.0', description='A wrapper for using the Scryfall API.', long_description=readme, url='https://github.com/NandaScott/Scrython',