def read_rotation_files() -> Tuple[int, int, List[Card]]: runs_str = redis.get_str('decksite:rotation:summary:runs') runs_percent_str = redis.get_str('decksite:rotation:summary:runs_percent') cards = redis.get_list('decksite:rotation:summary:cards') if runs_str is not None and runs_percent_str is not None and cards is not None: return int(runs_str), int(runs_percent_str), [Card(c, predetermined_values=True) for c in cards] return rotation_redis_store()
def get_file_contents(file: str) -> List[str]: key = f'decksite:rotation:file:{file}' contents = redis.get_list(key) if contents is not None: return contents with open(file) as f: contents = f.readlines() redis.store(key, contents, ex=604800) return contents
def get_parents(repo: Repository, sha: str) -> List[str]: value = redis.get_list(f'github:parents:{repo.full_name}:{sha}') if value is None: # print(f'getting parents for {sha}') commit = repo.get_commit(sha) parents = [p.sha for p in commit.parents] redis.store(f'github:parents:{repo.full_name}:{sha}', list(parents), ex=604800) return parents return value
def search_scryfall(query: str, exhaustive: bool = False) -> Tuple[int, List[str]]: """Returns a tuple. First member is an integer indicating how many cards match the query total, second member is a list of card names up to the maximum that could be fetched in a timely fashion. Supply exhaustive=True to instead retrieve the full list (potentially very slow).""" if query == '': return False, [] redis_key = f'scryfall:query:{query}:' + ('exhaustive' if exhaustive else 'nonexhaustive') cached = redis.get_list(redis_key) result_data: List[Dict] if cached: total_cards, result_data = int(cached[0]), cached[1] else: url = 'https://api.scryfall.com/cards/search?q=' + fetch_tools.escape( query) result_data = [] while True: for _ in range(3): try: result_json = fetch_tools.fetch_json(url) break except FetchException as c: print(c) if 'code' in result_json.keys(): # The API returned an error if result_json['status'] == 404: # No cards found return False, [] print('Error fetching scryfall data:\n', result_json) return False, [] for warning in result_json.get( 'warnings', []): # scryfall-provided human-readable warnings print(warning) # Why aren't we displaying these to the user? result_data += result_json['data'] total_cards = int(result_json['total_cards']) if not exhaustive or len(result_data) >= total_cards: break sleep(0.1) url = result_json['next_page'] redis.store(redis_key, [total_cards, result_data], ex=3600) result_data.sort(key=lambda x: x['legalities']['penny']) def get_frontside(scr_card: Dict) -> str: """If card is transform, returns first name. Otherwise, returns name. This is to make sure cards are later found in the database""" # not sure how to handle meld cards if scr_card['layout'] in [ 'transform', 'flip', 'adventure', 'modal_dfc' ]: return scr_card['card_faces'][0]['name'] return scr_card['name'] result_cardnames = [get_frontside(obj) for obj in result_data] return total_cards, result_cardnames