Example #1
0
def main(dict):
    
    # Checking if user has typed a deck that this function is trying to find
    if not "deckname" in dict:
        return { "error_message": "Type value to 'deckname' to fetch specific deck's information." }
    
    # Connecting Cloudant client
    serviceUsername = "******"
    servicePassword = "******"
    serviceURL = "https://*****:*****@3e757df4-b583-42a5-9c83-67f2d1bc3d10-bluemix.cloudantnosqldb.appdomain.cloud"
    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()
    
    # Fetching JSON-file where decks are written
    databaseName = "deck-database"
    database = client.__getitem__(databaseName)
    deckfile = database.__getitem__("6ce8ea8c29e12ec8ec21e4a45787ad94")
    
    # Trying to find deck from database
    name_to_find = dict["deckname"]
    result = {}
    for deck in deckfile["decks"]:
        if name_to_find == deck["deckname"]:
            result = deck
            break
    else:
        return { "error_message": f"Couldn't find a deck named {name_to_find}." }
    
    client.disconnect()
    result["tag"] = "get_deck"
    result["message"] = "Deck's information is presented in Slack by MTG Deck Bot!"
    return result
Example #2
0
def main(dict):

    # Connecting Cloudant client
    serviceUsername = "******"
    servicePassword = "******"
    serviceURL = "https://*****:*****@3e757df4-b583-42a5-9c83-67f2d1bc3d10-bluemix.cloudantnosqldb.appdomain.cloud"
    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()

    # Fetching JSON-file where decks are written
    databaseName = "deck-database"
    database = client.__getitem__(databaseName)
    deckfile = database.__getitem__("6ce8ea8c29e12ec8ec21e4a45787ad94")

    # Creating data that will be sent back to client
    data = {}
    data["decks"] = []
    for deck in deckfile["decks"]:
        deckdata = {}
        deckdata["name"] = deck["deckname"]
        deckdata["value"] = deck["value"]
        deckdata["card_amount"] = deck["card_amount"]
        data["decks"].append(deckdata)
    data["tag"] = "get_decks"
    data["message"] = "Decks are presented in Slack by MTG Deck Bot."

    client.disconnect()

    return data
Example #3
0
def main(dict):

    # Checking if new deck has right values in it
    if not "deckname" in dict:
        return {"error_message": "A deck must have a name value ('deckname')"}
    if not "cardurls" in dict:
        return {
            "error_message":
            "A deck must have atleast one card (in 'cardurls'). A card must have values 'url' and 'amount'."
        }

    # Connecting Cloudant client
    serviceUsername = "******"
    servicePassword = "******"
    serviceURL = "https://*****:*****@3e757df4-b583-42a5-9c83-67f2d1bc3d10-bluemix.cloudantnosqldb.appdomain.cloud"
    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()

    # Fetching JSON-file where decks are written
    databaseName = "deck-database"
    database = client.__getitem__(databaseName)
    deckfile = database.__getitem__("6ce8ea8c29e12ec8ec21e4a45787ad94")

    # Checking if deckname is already in use
    if not deck_name_valid(dict["deckname"], deckfile):
        client.disconnect()
        return {"error_message": "Deck name is already in use."}

    # Getting value for the new deck
    currentval = 0.0
    amount_of_cards = 0
    for url in dict["cardurls"]:
        currentval += get_price(url["url"]) * url["amount"]
        amount_of_cards += url["amount"]
    dict["value"] = round(currentval, 2)
    dict["card_amount"] = amount_of_cards

    # Removing unnecassary stuff from dict
    if "__ow_headers" in dict:
        dict.pop("__ow_headers")
    if "__ow_method" in dict:
        dict.pop("__ow_method")
    if "__ow_path" in dict:
        dict.pop("__ow_path")

    # Adding new deck to the collection
    deckfile["decks"].append(dict)
    deckfile.save()

    # Disconnecting from client
    client.disconnect()

    # Informing user that adding succeeded
    return {
        'message': "A new deck was added succesfully!",
        'deckname': dict["deckname"],
        'amount': amount_of_cards,
        'value': dict["value"],
        'tag': "add"
    }
Example #4
0
def main(dict):

    # Connecting Cloudant client
    serviceUsername = "******"
    servicePassword = "******"
    serviceURL = "https://*****:*****@3e757df4-b583-42a5-9c83-67f2d1bc3d10-bluemix.cloudantnosqldb.appdomain.cloud"
    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()

    # Fetching JSON-file where decks are written
    databaseName = "deck-database"
    database = client.__getitem__(databaseName)
    deckfile = database.__getitem__("6ce8ea8c29e12ec8ec21e4a45787ad94")

    # Looping through decks and updating deck prices.
    try:
        for deck in deckfile["decks"]:
            currentval = 0.0
            for urls in deck["cardurls"]:
                currentval += get_price(urls["url"]) * urls["amount"]
            deck["value"] = round(currentval, 2)

        # Saving deckfile and closing client
        deckfile.save()
        client.disconnect()

        # Getting time when updating was done
        now = (datetime.now() + timedelta(hours=3)).strftime('%c')
        return {
            'message': 'Database updated!',
            'time': str(now),
            'tag': "update",
            'success': True
        }
    except HTTPError as err:
        client.disconnect()
        now = (datetime.now() + timedelta(hours=3)).strftime('%c')
        return {
            'message': 'Database updating failed!',
            'time': str(now),
            'tag': "update",
            'success': False
        }
Example #5
0
def main(dict):

    # Checking if user has typed a deck that this function is trying to find
    if not "delete" in dict:
        return {
            "error_message":
            "Type deck name to 'delete' to delete a deck from database."
        }

    # Connecting Cloudant client
    serviceUsername = "******"
    servicePassword = "******"
    serviceURL = "https://*****:*****@3e757df4-b583-42a5-9c83-67f2d1bc3d10-bluemix.cloudantnosqldb.appdomain.cloud"
    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()

    # Fetching JSON-file where decks are written
    databaseName = "deck-database"
    database = client.__getitem__(databaseName)
    deckfile = database.__getitem__("6ce8ea8c29e12ec8ec21e4a45787ad94")

    # Finding deck from database
    deck_to_delete = dict["delete"]
    new_deck_list = []
    success = False
    for deck in deckfile["decks"]:
        if deck_to_delete != deck["deckname"]:
            new_deck_list.append(deck)
        else:
            success = True
    deckfile["decks"] = new_deck_list
    deckfile.save()
    client.disconnect()

    if success:
        return {
            'delete': deck_to_delete,
            'tag': "delete",
            'message': f"A deck named {deck_to_delete} has been deleted!"
        }
    return {'error_message': 'Could not find deck to remove!'}