import os
import requests
import json
import ebaysdk
from ebaysdk.trading import Connection as Trading
from ebaysdk.finding import Connection as Finding
import time
import concurrent.futures
# (categoryId = women's shoes = 3034)
# Initialize loop to get number of pages needed in for loop
start = time.time()
fapi = Finding(config_file = "ebay.yaml")
tapi = Trading(config_file = 'ebay.yaml')

fresponse = fapi.execute(
        'findItemsAdvanced',
        {
            'itemFilter':{
                'name':'Seller',
                'value':'chesshoebuddy'
                },
            'paginationInput':{
                'entriesPerPage':'100',
                'pageNumber':'1'
                }
            }
        ).dict()

page_results = int(fresponse['paginationOutput']['totalPages'])

pages = []
for i in range(0, page_results):
    i += 1
    pages.append(i)

''' Begin definitions for getting ItemIds and SKU: '''

def id_up(n):
    ids = []
    fresponse = fapi.execute(
            'findItemsAdvanced',
            {
                'itemFilter':{
                    'name':'Seller',
                    'value':'chesshoebuddy'
                    },
                'paginationInput':{
                    'entriesPerPage':'100',
                    'pageNumber':str(n)
                    }
                }
            ).dict()
    for item in (fresponse['searchResult']['item']):
        itemID = item['itemId']
        #response = tapi.execute('GetItem',{'ItemID':itemID}).dict()
        ids.append(itemID)
    return ids

def main():
    ids = []
    skus = []
    with concurrent.futures.ThreadPoolExecutor() as executor:
        for future in executor.map(id_up, pages):
            ids.extend(future)

    with open('ebay_ids.txt', 'w') as outfile:
        json.dump(ids, outfile)

if __name__ == '__main__':
    main()
