<commit_before>import urllib

from util import hook, http


def card_search(query):
    base_url = "https://api.deckbrew.com"
    name = urllib.quote_plus(query)
    search_url = base_url + "/mtg/cards?name=" + name

    return http.get_json(search_url)


@hook.command
def mtg(inp, say=None):
    '''.mtg <name> - Searches for Magic the Gathering card given <name>
    '''

    try:
        card = card_search(inp)[0]
    except IndexError:
        return "Card not found."

    results = {
        "name": card["name"],
        "types": ", ".join(t.capitalize() for t in card["types"]),
        "cost": card["cost"],
        "text": card["text"].rstrip(),
        "multiverse_id": card["editions"][0]["multiverse_id"],
    }
    
    return "{name} - {cost} | {types} - {text} | http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid={multiverse_id}".format(**results)


if __name__ == "__main__":
    print card_search("black lotus")
<commit_msg>Replaced newlines that get squashed with spaces. Fixed unicode string error where planeswalker cards would not get printed. Added a loop to detect a valid multiverse_id so the url to the Magic webpage will always be valid<commit_after>import urllib

from util import hook, http


def card_search(query):
    base_url = "https://api.deckbrew.com"
    name = urllib.quote_plus(query)
    search_url = base_url + "/mtg/cards?name=" + name

    return http.get_json(search_url)


@hook.command
def mtg(inp, say=None):
    '''.mtg <name> - Searches for Magic the Gathering card given <name>
    '''

    try:
        card = card_search(inp)[0]
    except IndexError:
        return "Card not found."

    for valid_edition in range(len(card["editions"])):
        if card["editions"][valid_edition]["multiverse_id"] != 0 :
            break
        else:
            continue

    results = {
        "name": card["name"],
        "types": ", ".join(t.capitalize() for t in card["types"]),
        "cost": card["cost"].replace('{','').replace('}',''),
        "text": card["text"].replace('\n',' '),
        "multiverse_id": card["editions"][valid_edition]["multiverse_id"],
    }
    
    return u"{name} - {types} - {cost} | {text} | http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid={multiverse_id}".format(**results)


if __name__ == "__main__":
    print card_search("Black Lotus")
    print mtg("Black Lotus")
{'fraction_non_alphanumeric': 0.15813758389261745, 'fraction_numerical': 0.0016778523489932886, 'mean_word_length': 3.198943661971831, 'pattern_counts': {'":': 12, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7609151', 'n_tokens_mistral': 804, 'n_tokens_neox': 780, 'n_words': 187}