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 ] lines = [] fs = files() if len(fs) == 0: if not os.path.isdir(configuration.get_str('legality_dir')): raise DoesNotExistException( 'Invalid configuration. Could not find legality_dir.') return (0, 0, []) latest_list = open(fs[-1], 'r').read().splitlines() for filename in fs: for line in get_file_contents(filename): line = text.sanitize(line) lines.append(line.strip()) scores = Counter(lines).most_common() runs = scores[0][1] runs_percent = round(round(runs / TOTAL_RUNS, 2) * 100) cs = oracle.cards_by_name() cards = [] for name, hits in scores: c = process_score(name, hits, cs, runs, latest_list) if c is not None: cards.append(c) redis.store('decksite:rotation:summary:runs', runs, ex=604800) redis.store('decksite:rotation:summary:runs_percent', runs_percent, ex=604800) redis.store('decksite:rotation:summary:cards', cards, ex=604800) return (runs, runs_percent, cards)
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 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_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, character_encoding='utf-8') 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']: 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
def get_pr_from_commit(repo: Repository, sha: str) -> PullRequest: cached = redis.get_list(f'github:head:{sha}') if cached: try: pr = repo.get_pull(cached) if pr.head.sha == sha and pr.state == 'open': return pr except UnknownObjectException: pass for pr in repo.get_pulls(): head = pr.head.sha redis.store(f'github:head:{head}', pr.number, ex=3600) if head == sha: return pr return None