Changed a whole bunch of stuff.

This commit is contained in:
Nanda Scott 2018-02-13 21:02:48 -05:00
parent d92aad46b6
commit 958c332e65
14 changed files with 107 additions and 23 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ __pycache__
.ropeproject
scrython.egg-info
dist
tests

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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']

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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',