Пример #1
0
def delete_files(files_to_delete):
    print('Deleting files %s' % files_to_delete)
    for file in files_to_delete:
        file_name = '%s/%s' % (OUTPUT_FILE_DIRECTORY_NAME, file)
        try:
            if path.exists(file_name):
                remove(file_name)
            else:
                info_print('%s does not exist' % file)
        except Exception as e:
            warning_print('Could not remove %s' % file)
            print(e)
Пример #2
0
def get_files_in_directory(directory):
    global all_input_files
    if (directory == INPUT_FILE_DIRECTORY_NAME):
        if (len(all_input_files) > 0):
            return all_input_files
    files = sorted(
        [file for file in listdir(directory) if isfile(join(directory, file))])
    if (directory == INPUT_FILE_DIRECTORY_NAME):
        filtered_files = list(filter(file_is_csv, files))
        if (filtered_files != files):
            info_print('Ignoring non .csv files')
            files = filtered_files
        all_input_files = files
    return files
Пример #3
0
def db_set_to_ck_set(old_set, card_number, set_info):
    abbr_set = None
    for set_data in set_info:
        set_name = set_data['name'].lower()
        if old_set.lower() == set_name:
            abbr_set = set_data['code']
            break

    if (not abbr_set):
        return old_set

    pricing_url = "https://api.scryfall.com/cards/collection"
    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json"
    }
    # check price
    card_request_body = {"identifiers": []}
    card_request_body['identifiers'].append({
        "collector_number": card_number,
        "set": abbr_set,
    })
    card_data = requests.request("POST",
                                 pricing_url,
                                 headers=headers,
                                 json=card_request_body)
    if (card_data.status_code != 200):
        info_print('~~~~ Error while getting card data ~~~~~')
        info_print('Status code ** %d ** for card Card Number: %s, Set: %s' %
                   (card_data.status_code, card_number, abbr_set))
        info_print('Response:\n%s\n\n' % card_data.text)
        return old_set
    card_data = json.loads(card_data.text)["data"][0] if json.loads(
        card_data.text)["data"] else []
    frame_data = card_data[
        'frame_effects'] if 'frame_effects' in card_data else []
    if (frame_data):
        if ('etched' in frame_data or 'extendedart' in frame_data
                or 'showcase' in frame_data):
            return '%s Variants' % old_set

    if (old_set == 'Amonkhet Invocations'):
        return 'Masterpiece Series: Invocations'
    if (old_set == 'Archenemy: Nicol Bolas'):
        return 'Archenemy - Nicol Bolas'
    if (old_set == 'Commander Anthology Volume Ii'
            or old_set == 'Commander Anthology Volume II'):
        return 'Commander Anthology Vol. II'
    if ('Extras' in old_set):
        return ''
    if (old_set == 'Ravnica: City of Guilds'):
        return 'Ravnica'
    if (old_set == 'The List' or old_set == 'Myster Booster'):
        return 'Mystery Booster/The List'
    if (old_set == 'Zendikar Rising Commander'):
        return 'Zendikar Rising Commander Decks'
    if (old_set == 'Ravnica Allegiance Guild Kit'):
        return 'Ravnica Allegiance: Guild Kits'

    return old_set
Пример #4
0
def get_input_files(has_exclusions):
    global main_input_file

    input_files = get_files_in_directory(INPUT_FILE_DIRECTORY_NAME)
    indexed_input_files = dict(
        (input_files.index(file), file) for file in input_files)
    output_pages = paginate_dict(indexed_input_files)

    while (not main_input_file):
        instruction_print('Please select the main input file')
        main_input_file = '%s' % get_paginated_input(output_pages)
        if (not main_input_file):
            warning_print('Main input file cannot be empty')

    more_exclusions = True

    while (has_exclusions and more_exclusions):
        instruction_print('Please select the file that includes exclusions')
        excluded_file = get_paginated_input(output_pages)
        if (excluded_file):
            if (not excluded_file in exclusion_files):
                exclusion_files.append(
                    '%s/%s' % (INPUT_FILE_DIRECTORY_NAME, excluded_file))
                continue
            warning_print('%s has already been selected' % excluded_file)
            info_print('Excluded files selected thus far: %s' %
                       exclusion_files)
        else:
            more_exclusions = False

    info_print('Main input file selected: %s/%s' %
               (INPUT_FILE_DIRECTORY_NAME, main_input_file))
    info_print('Files that include exclusions: %s' % exclusion_files)
Пример #5
0
    def to_card_kingdom(self, row_limit, output_file_name='ck_massaged'):
        # ck_fields = Card_kingdom.fields
        # csvreader = csv.reader(self.csvfile)

        count_index = self.csv_fields.index(fields['COUNT'])
        foil_index = self.csv_fields.index(fields['FOIL'])
        name_index = self.csv_fields.index(fields['NAME'])
        set_index = self.csv_fields.index(fields['SET'])
        card_number_index = self.csv_fields.index('Card Number')

        new_rows = []
        new_rows.append(ck_fields.values())
        for index, row in enumerate(self.csv_rows):
            complete_percent = round(float((index / len(self.csv_rows)) * 100),
                                     2)
            info_print(
                "========  {:.2f}% complete massaging deckbox file  ========".
                format(complete_percent), "\r")

            if (len(row) <= 0):
                continue
            time.sleep(.2)
            card_name, card_set = db_name_set_to_ck_name_set(
                row[name_index], row[set_index], row[card_number_index],
                self.set_info)

            if (not card_name or not card_set):
                continue

            def get_full_output_file_name(is_paged):
                if (not is_paged and not os.path.exists(
                        '%s_ck_massaged.csv' % output_file_name)):
                    return '%s_ck_massaged.csv' % output_file_name

                test_page = 0
                while (os.path.exists('%s_ck_massaged%d.csv' %
                                      (output_file_name, test_page))):
                    test_page += 1
                return '%s_ck_massaged%d.csv' % (output_file_name, test_page)

            if (row_limit):
                if (len(new_rows) > row_limit):
                    file_to_write = get_full_output_file_name(
                        not not row_limit)
                    with open(file_to_write, 'w') as csvfile_writer:
                        csvwriter = csv.writer(csvfile_writer)
                        csvwriter.writerows(new_rows)
                        info_print('Successfully wrote %s' % file_to_write)

                    new_rows = []
                    new_rows.append(ck_fields.values())

            new_rows.append([
                card_name, card_set, 'true' if row[foil_index] else 'false',
                row[count_index]
            ])

        full_file_name = get_full_output_file_name(not not row_limit)
        with open(full_file_name, 'w') as csvfile_writer:
            csvwriter = csv.writer(csvfile_writer)
            csvwriter.writerows(new_rows)
            info_print('Successfully wrote %s' % full_file_name)

        self.to_sell_rows = new_rows

        to_sell_ck = Card_kingdom()
        to_sell_ck.to_deckbox()
Пример #6
0
 def price_cards():
     pricing_url = "https://api.scryfall.com/cards/collection"
     headers = {
         "Accept": "application/json",
         "Content-Type": "application/json"
     }
     # check price
     price_request_body = {"identifiers": []}
     for card_to_price in cards_to_price:
         price_request_body['identifiers'].append({
             "collector_number":
             card_to_price[4],
             "set":
             card_to_price[2],
         })
     time.sleep(1)
     card_data = requests.request("POST",
                                  pricing_url,
                                  headers=headers,
                                  json=price_request_body)
     if (card_data.status_code != 200):
         info_print(card_data.json())
         return
     card_data = json.loads(card_data.text)["data"] if json.loads(
         card_data.text)["data"] else []
     for price in card_data:
         is_card_foil = False
         for card in cards_to_price:
             if (card[1] == price["name"] and card[2] == price["set"]
                     and card[4] == price["collector_number"]):
                 is_card_foil = card[3]
                 card_price = price["prices"][
                     "usd_foil"] if is_card_foil else price["prices"]["usd"]
                 if (card_price == None):
                     cards_with_no_data.append([
                         card[0],
                         card[1],
                         price["set_name"] or "SET NOT AVAILABLE",
                         price["mana_cost"]
                         if "mana_cost" in price.keys() else
                         ("%s // %s" %
                          (price["card_faces"][0]["mana_cost"] or 'NO COST',
                           price["card_faces"][1]["mana_cost"] or 'NO COST')
                          )
                         if price["card_faces"] else "COST NOT AVAILABLE",
                         price["rarity"] or "RARITY NOT AVAILABLE",
                         is_card_foil,
                         card[4],
                         card_price or 'PRICE NOT AVAILABLE',
                     ])
                 if (card_price and float(card_price) > minimum_value):
                     value_card_data.append([
                         card[0],
                         card[1],
                         price["set_name"],
                         price["mana_cost"]
                         if "mana_cost" in price.keys() else "%s // %s" %
                         (price["card_faces"][0]["mana_cost"] or 'NO COST',
                          price["card_faces"][1]["mana_cost"] or 'NO COST'),
                         price["rarity"],
                         is_card_foil,
                         card[4],
                         card_price,
                     ])
                 cards_to_price.remove(card)
                 break
Пример #7
0
def search_for_value(csv_fields, filtered_card_data):
    sets_url = "https://api.scryfall.com/sets"
    headers = {"Accept": "application/json"}
    set_info = json.loads(
        requests.request("GET", sets_url, headers=headers).text)['data']

    cards_to_price = []
    value_card_data = [[
        'Count', 'Name', 'Edition', 'Mana Cost', 'Rarity', 'Foil',
        'Card Number', 'Price'
    ]]
    cards_with_no_data = [[
        'Count', 'Name', 'Edition', 'Mana Cost', 'Rarity', 'Foil',
        'Card Number', 'Price'
    ]]

    count_index = csv_fields.index('Count')
    name_index = csv_fields.index('Name')
    set_index = csv_fields.index('Edition')
    foil_index = csv_fields.index('Foil')
    card_number_index = csv_fields.index('Card Number')

    def price_cards():
        pricing_url = "https://api.scryfall.com/cards/collection"
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/json"
        }
        # check price
        price_request_body = {"identifiers": []}
        for card_to_price in cards_to_price:
            price_request_body['identifiers'].append({
                "collector_number":
                card_to_price[4],
                "set":
                card_to_price[2],
            })
        time.sleep(1)
        card_data = requests.request("POST",
                                     pricing_url,
                                     headers=headers,
                                     json=price_request_body)
        if (card_data.status_code != 200):
            info_print(card_data.json())
            return
        card_data = json.loads(card_data.text)["data"] if json.loads(
            card_data.text)["data"] else []
        for price in card_data:
            is_card_foil = False
            for card in cards_to_price:
                if (card[1] == price["name"] and card[2] == price["set"]
                        and card[4] == price["collector_number"]):
                    is_card_foil = card[3]
                    card_price = price["prices"][
                        "usd_foil"] if is_card_foil else price["prices"]["usd"]
                    if (card_price == None):
                        cards_with_no_data.append([
                            card[0],
                            card[1],
                            price["set_name"] or "SET NOT AVAILABLE",
                            price["mana_cost"]
                            if "mana_cost" in price.keys() else
                            ("%s // %s" %
                             (price["card_faces"][0]["mana_cost"] or 'NO COST',
                              price["card_faces"][1]["mana_cost"] or 'NO COST')
                             )
                            if price["card_faces"] else "COST NOT AVAILABLE",
                            price["rarity"] or "RARITY NOT AVAILABLE",
                            is_card_foil,
                            card[4],
                            card_price or 'PRICE NOT AVAILABLE',
                        ])
                    if (card_price and float(card_price) > minimum_value):
                        value_card_data.append([
                            card[0],
                            card[1],
                            price["set_name"],
                            price["mana_cost"]
                            if "mana_cost" in price.keys() else "%s // %s" %
                            (price["card_faces"][0]["mana_cost"] or 'NO COST',
                             price["card_faces"][1]["mana_cost"] or 'NO COST'),
                            price["rarity"],
                            is_card_foil,
                            card[4],
                            card_price,
                        ])
                    cards_to_price.remove(card)
                    break
        # cards_to_price = []

    for index, row in enumerate(filtered_card_data):
        complete_percent = round(
            float((index / len(filtered_card_data)) * 100), 2)
        info_print(
            "========  {:.2f}% complete getting card value  ========".format(
                complete_percent), "\r")
        if (len(cards_to_price) >= 70):
            price_cards()
            cards_to_price = []
        else:
            card_set = row[set_index].lower()
            for set_data in set_info:
                set_name = set_data['name'].lower()
                if card_set == set_name:
                    cards_to_price.append([
                        row[count_index], row[name_index], set_data['code'],
                        row[foil_index], row[card_number_index]
                    ])
                    break

    if (len(cards_to_price) > 0):
        price_cards()
        cards_to_price = []
    if (len(value_card_data) > 0):
        with open('%s/value_output.csv' % OUTPUT_FILE_DIRECTORY_NAME,
                  'w') as csvfile_writer:
            csvwriter = csv.writer(csvfile_writer)
            csvwriter.writerows(value_card_data)
            info_print('Successfully wrote %s' % '%s/value_output.csv' %
                       OUTPUT_FILE_DIRECTORY_NAME)