示例#1
0
def insert_api_data(db_type: str):
    # Insert database contents using osrsbox-api
    if db_type == "items":
        all_db_entries = items_api.load()
    elif db_type == "monsters":
        all_db_entries = monsters_api.load()
    elif db_type == "prayers":
        all_db_entries = prayers_api.load()

    count = 0
    print(f">>> Inserting {db_type} data...")
    for entry in all_db_entries:
        # Make a dictionary from the ItemProperties object
        entry_dict = entry.construct_json()

        # Dump dictionary to JSON for API parameter
        entry_json = json.dumps(entry_dict)

        # Send POST request
        status, response = perform_api_post(API_ENDPOINT + f"/{db_type}",
                                            entry_json)

        if response["_status"] == "ERR":
            status, response = perform_api_put(API_ENDPOINT + f"/{db_type}",
                                               entry_json)

        if response["_status"] == "ERR":
            print(response)
            print(">>> Data insertion error... Exiting.")
            quit()

        count += 1
        print(f"  > Processed: {count:05} of {len(all_db_entries)}", end="\r", flush=True)
示例#2
0
def insert_api_data(db_type: str):
    print(f">>> Inserting {db_type} data...")

    # Insert database contents using osrsbox-api
    if db_type == "items" or db_type == "icons_items":
        all_db_entries = items_api.load()
    elif db_type == "monsters":
        all_db_entries = monsters_api.load()
    elif db_type == "prayers" or db_type == "icons_prayers":
        all_db_entries = prayers_api.load()

    all_entries = list()
    bulk_entries = list()

    for entry in all_db_entries:
        # Check if we are processing icons, and strip to id, name
        if "icons" in db_type:
            new_entry = dict()
            new_entry["id"] = entry.id
            new_entry["icon"] = entry.icon
            entry = new_entry.copy()
        # Append to a list of all entries
        all_entries.append(entry)

    for db_entries in itertools.zip_longest(*[iter(all_entries)] * 50):
        # Remove None entries from the list
        db_entries = filter(None, db_entries)
        # Cast from filter object to list
        db_entries = list(db_entries)
        # Append to list of bulk entries
        bulk_entries.append(db_entries)

    for i, block in enumerate(bulk_entries):
        print(f"  > Processed: {i*50}")
        to_insert = list()
        for entry in block:
            # Make a dictionary from the *Properties object
            if not isinstance(entry, dict):
                entry = entry.construct_json()
            # Dump dictionary to JSON for API parameter
            entry_json = json.dumps(entry)
            # Append to the to_insert list
            to_insert.append(entry_json)

        # Convert list to JSON list
        to_insert = json.dumps(to_insert)

        # Send POST request, or PUT request if that fails
        status, response = perform_api_post(API_ENDPOINT + f"/{db_type}",
                                            to_insert)

        if response["_status"] == "ERR":
            status, response = perform_api_put(API_ENDPOINT + f"/{db_type}",
                                               to_insert)

        if response["_status"] == "ERR":
            print(response)
            print(">>> Data insertion error... Exiting.")
            quit()
示例#3
0
def insert_data(db_type: str):
    print(f">>> Inserting {db_type} data...")

    # Insert database contents using osrsbox-api
    if db_type == "items" or db_type == "icons_items":
        all_db_entries = items_api.load()
    elif db_type == "monsters":
        all_db_entries = monsters_api.load()
    elif db_type == "prayers" or db_type == "icons_prayers":
        all_db_entries = prayers_api.load()

    all_entries = list()
    bulk_entries = list()

    for entry in all_db_entries:
        # Check if we are processing icons, and strip to id, name
        if "icons" in db_type:
            new_entry = dict()
            new_entry["id"] = entry.id
            new_entry["icon"] = entry.icon
            entry = new_entry.copy()
        # Append to a list of all entries
        all_entries.append(entry)

    # Remove all entries in the collection
    collection = db[db_type]
    collection.remove()

    for db_entries in itertools.zip_longest(*[iter(all_entries)] * 50):
        # Remove None entries from the list
        db_entries = filter(None, db_entries)
        # Cast from filter object to list
        db_entries = list(db_entries)
        # Append to list of bulk entries
        bulk_entries.append(db_entries)

    for i, block in enumerate(bulk_entries):
        print(f"  > Processed: {i*50}")
        to_insert = list()
        for entry in block:
            # Make a dictionary from the *Properties object
            if not isinstance(entry, dict):
                entry = entry.construct_json()
                # Convert item ID to string for lookup
                entry["id"] = str(entry["id"])
            # Append to the to_insert list
            to_insert.append(entry)

        # Insert into MongoDB
        collection = db[db_type]
        collection.insert_many(to_insert)
示例#4
0
# gets item names without filtering non-unqiues
from osrsbox import monsters_api
all_db_monsters = monsters_api.load()
text_file = open("monsters.txt", "w")
text_file.write("[")
for monster in all_db_monsters:
    text_file.write('\'' + monster.name.replace('\'', '') + '\',')
text_file.write("]")
text_file.close()
示例#5
0
from fractions import Fraction

from osrsbox import monsters_api, items_api

monsters = monsters_api.load()
items = items_api.load()


def load_monster_from_api(monster):
    loaded_monster = [x for x in monsters if x.name.lower() == monster.lower()]
    if len(loaded_monster) == 0:
        loaded_monster = [x for x in monsters if monster.lower() in x.name.lower()]
    if len(loaded_monster) == 0:
        return False
    returned_monsters = []
    for x in loaded_monster:
        if x.name not in [x.name.lower() for x in returned_monsters]:
            if x.name not in [x.name for x in returned_monsters]:
                returned_monsters.append(x)
    return returned_monsters


def parse_monster_drops(drops):
    sorted_drops = []
    for x_drops in drops:
        for x_items in items:
            if x_items.name.lower() == x_drops.name.lower():
                rarity_string = (str(Fraction(x_drops.rarity).limit_denominator())).split('/')
                if len(rarity_string) > 1:
                    rarity_string = f"{rarity_string[0]}/{int(rarity_string[1]):,d}"
                else: