def time(q): no_results_msg = 'Location unknown.' url = 'http://maps.googleapis.com/maps/api/geocode/json?address={q}&sensor=false'.format(q=internal.escape(q)) info = internal.fetch_json(url) try: location = info['results'][0]['geometry']['location'] except IndexError: return no_results_msg url = 'https://maps.googleapis.com/maps/api/timezone/json?location={lat},{lng}×tamp={timestamp}&sensor=false'.format(lat=internal.escape(str(location['lat'])), lng=internal.escape(str(location['lng'])), timestamp=internal.escape(str(dtutil.dt2ts(dtutil.now())))) timezone_info = internal.fetch_json(url) if timezone_info['status'] == 'ZERO_RESULTS': return no_results_msg return dtutil.now(dtutil.timezone(timezone_info['timeZoneId'])).strftime('%l:%M %p')
def mtgo_status(): try: return internal.fetch_json( 'https://magic.wizards.com/sites/all/modules/custom/wiz_services/mtgo_status.php' )['status'] except (FetchException, json.decoder.JSONDecodeError): return 'UNKNOWN'
def search_scryfall(query: str) -> 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.""" if query == '': return False, [] result_json = internal.fetch_json('https://api.scryfall.com/cards/search?q=' + internal.escape(query), character_encoding='utf-8') 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'] 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 result_json['total_cards'], result_cardnames
def time(q: str) -> str: if len(q) > 3: url = 'http://maps.googleapis.com/maps/api/geocode/json?address={q}&sensor=false'.format(q=internal.escape(q)) info = internal.fetch_json(url) try: location = info['results'][0]['geometry']['location'] except IndexError as e: raise TooFewItemsException(e) url = 'https://maps.googleapis.com/maps/api/timezone/json?location={lat},{lng}×tamp={timestamp}&sensor=false'.format(lat=internal.escape(str(location['lat'])), lng=internal.escape(str(location['lng'])), timestamp=internal.escape(str(dtutil.dt2ts(dtutil.now())))) timezone_info = internal.fetch_json(url) if timezone_info['status'] == 'ZERO_RESULTS': raise TooFewItemsException(timezone_info['status']) timezone = dtutil.timezone(timezone_info['timeZoneId']) else: try: timezone = dtutil.timezone(q.upper()) except pytz.exceptions.UnknownTimeZoneError: # type: ignore raise TooFewItemsException('Not a recognized timezone: {q}'.format(q=q)) return dtutil.now(timezone).strftime('%l:%M %p')
def search_scryfall(query): """Returns a tuple. First member is bool indicating whether there were too many cards to search, second member is a list of card names.""" max_n_queries = 2 #API returns 60 cards at once. Indicate how many pages max should be shown. if query == '': return False, [] result_json = internal.fetch_json('https://api.scryfall.com/cards/search?q=' + internal.escape(query), character_encoding='utf-8') if 'code' in result_json.keys(): #the API returned an error if result_json['status'] == 404: #no cards found print('Scryfall search yielded 0 results.') 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) too_many_cards = result_json['total_cards'] > max_n_queries * 60 result_data = result_json['data'] for _ in range(max_n_queries - 1): #fetch the remaining pages if not result_json['has_more']: break result_json = internal.fetch_json(result_json['next_page']) result_data.extend(result_json.get('data', [])) result_data.sort(key=lambda x: x['legalities']['penny']) def get_frontside(scr_card): """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'] == 'transform': return scr_card['all_parts'][0]['name'] if scr_card['layout'] == 'flip': return scr_card['card_faces'][0]['name'] return scr_card['name'] result_cardnames = [get_frontside(obj) for obj in result_data] return too_many_cards, result_cardnames
def whatsinstandard() -> Dict[str, Union[bool, List[Dict[str, str]]]]: return internal.fetch_json('http://whatsinstandard.com/api/v5/sets.json')
def sitemap(): return internal.fetch_json(decksite_url('/api/sitemap/'))
def scryfall_cards(): url = 'https://api.scryfall.com/cards' return internal.fetch_json(url)
def bugged_cards(): bugs = internal.fetch_json( 'https://pennydreadfulmtg.github.io/modo-bugs/bugs.json') if bugs is None: return None return bugs
def mtgjson_version() -> str: return cast(str, internal.fetch_json('https://mtgjson.com/json/version.json'))
def mtgjson_version(): return pkg_resources.parse_version(internal.fetch_json('https://mtgjson.com/json/version.json'))
def fetch_decks(): return fetcher_internal.fetch_json( 'https://tappedout.net/api/deck/latest/penny-dreadful/')
def whatsinstandard(): return internal.fetch_json('http://whatsinstandard.com/api/v5/sets.json')
def sitemap() -> List[str]: return internal.fetch_json(decksite_url('/api/sitemap/'))
def rulings(cardname: str) -> List[Dict[str, str]]: card = internal.fetch_json('https://api.scryfall.com/cards/named?exact={name}'.format(name=cardname)) return internal.fetch_json(card['uri'] + '/rulings')['data']
def person_data(person: Union[str, int]) -> Dict[str, Any]: try: data = internal.fetch_json('https://pennydreadfulmagic.com/api/person/{0}'.format(person)) except (FetchException, json.decoder.JSONDecodeError): return {} return data
def scryfall_cards() -> Dict[str, Any]: url = 'https://api.scryfall.com/cards' return internal.fetch_json(url)
def card_price(cardname): return internal.fetch_json('http://katelyngigante.com:5800/{0}/'.format(cardname.replace('//', '-split-')))
def bugged_cards() -> Optional[List[Dict[str, Any]]]: bugs = internal.fetch_json('https://pennydreadfulmtg.github.io/modo-bugs/bugs.json') if bugs is None: return None return bugs
def fetch_deck_details(raw_deck: DeckType) -> DeckType: return fetcher_internal.fetch_json('https://tappedout.net/api/collection/collection:deck/{slug}/'.format(slug=raw_deck['slug']))
def card_price(cardname: str) -> Dict[str, Any]: return internal.fetch_json('http://vorpald20.com:5800/{0}/'.format(cardname.replace('//', '-split-')))
def fetch_deck_details(raw_deck): return fetcher_internal.fetch_json( "https://tappedout.net/api/collection/collection:deck/{slug}/".format( slug=raw_deck['slug']))
def mtgjson_version(): return internal.fetch_json('https://mtgjson.com/json/version.json')