Finish checkRevisedItems, apply dict directly to api call, add error checking to switchDateRange

checkRevisedItems will return a list called itemIDs inside the apiResponse - This list is all the items that need getItems again
instead of needlessly pulling the response from the api into a variable and applying a dict there, apply directly to the api call
switchDateRange will now output a dict containing a range ending today if no arguments are given
This commit is contained in:
David Todd 2016-03-25 15:28:08 -05:00
parent b9d65b7d64
commit a01ffceb86
No known key found for this signature in database
GPG Key ID: 48E847F18074C953

58
ebay.py
View File

@ -23,6 +23,8 @@ csv_path = ''
csv_delimiter = ',' csv_delimiter = ','
csv_quote = '\'' csv_quote = '\''
# We can probably put the DateRange functions into a class
# Creates a dateRange list for use with glue # Creates a dateRange list for use with glue
def setDateRange(days=None, start=None, rangeType=None): def setDateRange(days=None, start=None, rangeType=None):
# Default to searching for listings ending today # Default to searching for listings ending today
@ -75,8 +77,21 @@ def setDateRange(days=None, start=None, rangeType=None):
# We call this a couple times, so it gets its own function # We call this a couple times, so it gets its own function
def switchDateRange(list=None, range=None): def switchDateRange(list=None, range=None):
# Switch statement to check which type of dateRange to search by # Switch statement to check which type of dateRange to search by
# Will always return a valid dateRange for use as various itemArgs
# Defaults to a dateRange ending today
if (range == None) | (isinstance(range, dict) != True):
# Sets the default range (items that are ending today)
range = setDateRange()
if (list == None) | (isinstance(list, dict) != True):
list = {}
# This can be start/mod/end # This can be start/mod/end
def rangeType(condition): def rangeType(condition=None):
# No condition to check, defaults to End
if condition == None:
return 'End'
# Cast the condition to a string # Cast the condition to a string
condition = str(condition) condition = str(condition)
switch = { switch = {
@ -87,7 +102,6 @@ def switchDateRange(list=None, range=None):
# Return the approiate text or End if no ID matches # Return the approiate text or End if no ID matches
return switch.get(condition, 'End') return switch.get(condition, 'End')
# Override the dates in sellerList with values from dateRange, if provided # Override the dates in sellerList with values from dateRange, if provided
if ('from' in range) & ('type' in range): if ('from' in range) & ('type' in range):
# Remove the list of keys from sellerList - We can only have one search range # Remove the list of keys from sellerList - We can only have one search range
@ -142,11 +156,7 @@ def getSeller(api=None, list=None):
res['error']['msg'] = 'either "EndTimeFrom" or "EndTimeTo" is not set in list' res['error']['msg'] = 'either "EndTimeFrom" or "EndTimeTo" is not set in list'
return res return res
# run the actual command res['apiResponse'] = api.execute('GetSellerEvents', list).dict()
apiResponse = api.execute('GetSellerEvents', list)
# Store the response in a dict and unset response
res['apiResponse'] = apiResponse.dict()
apiResponse = None
# Verify that the search returned information # Verify that the search returned information
if res['apiResponse']['ItemArray'] != None: if res['apiResponse']['ItemArray'] != None:
@ -205,10 +215,7 @@ def getItems(api=None, itemList=None, itemArgs=None):
if(k['SellingStatus']['ListingStatus'] != 'Active'): if(k['SellingStatus']['ListingStatus'] != 'Active'):
continue continue
response = api.execute('GetItem', itemArgs) res['apiResponse'][k['ItemID']] = api.execute('GetItem', itemArgs).dict()
res['apiResponse'][k['ItemID']] = response.dict()
#res['apiResponse'] = { k['ItemID']: response.dict() }
response = None
# We want the error code to only be changed on the first successful iteration # We want the error code to only be changed on the first successful iteration
if res['error']['code'] == None: if res['error']['code'] == None:
@ -223,7 +230,7 @@ def checkRevisedItems(api=None, itemArgs=None, dateRange=None):
# api is the connection to the Trading API # api is the connection to the Trading API
# itemList is a list containing itemIDs to check for updates # itemList is a list containing itemIDs to check for updates
# itemArgs is an optional dict that contains extra details to refine the search returned by ebay # itemArgs is an optional dict that contains extra details to refine the search returned by ebay
res = { 'error': { 'code': None, 'msg': None, 'fnc': 'checkRevisedItems' }, 'apiResponse': {} } res = { 'error': { 'code': None, 'msg': None, 'fnc': 'checkRevisedItems' }, 'apiResponse': { 'itemIDs': [] } }
if api == None: if api == None:
res['error']['code'] = '1' res['error']['code'] = '1'
res['error']['msg'] = 'api is not set' res['error']['msg'] = 'api is not set'
@ -233,19 +240,30 @@ def checkRevisedItems(api=None, itemArgs=None, dateRange=None):
res['error']['msg'] = 'itemArgs doesn\'t exist or is of wrong type, must be dict' res['error']['msg'] = 'itemArgs doesn\'t exist or is of wrong type, must be dict'
return res return res
if (dateRange == None) | (isinstance(dateRange, dict) != True): if (dateRange == None) | (isinstance(dateRange, dict) != True):
res['error']['code'] = '2' dateRange = setDateRange()
res['error']['msg'] = 'dateRange doesn\'t exist or is of wrong type, must be dict'
return res
itemArgs['NewItemFilter'] = 'True' itemArgs['NewItemFilter'] = 'True'
# Switch statement to select the proper dateRange # Switch statement to select the proper dateRange
itemArgs = switchDateRange(itemArgs, dateRange) itemArgs = switchDateRange(itemArgs, dateRange)
response = api.execute('GetSellerEvents', itemArgs).dict()
# try:
response = api.execute('GetSellerEvents', itemArgs) items = response['ItemArray']['Item']
for i in itemList: # Store the ItemIDs that need to be updated again with getItem
for i in items:
res['apiResponse']['itemIDs'].append(i['ItemID'])
except TypeError:
res['apiResponse'] = {'code': '1', 'msg': 'No items were revised in the selected dateRange'}
response = None
# No Errors found
if res['error']['code'] == None:
res['error']['code'] = '0'
return res
# storeItems uses the dict of Items provided by getItems and stores the information we want # storeItems uses the dict of Items provided by getItems and stores the information we want
@ -353,9 +371,7 @@ def glue(api=None, sellerList=None, dateRange=None):
res['error']['msg'] = 'itemList doesn\'t exist or is of wrong type, must be dict' res['error']['msg'] = 'itemList doesn\'t exist or is of wrong type, must be dict'
return res return res
if (dateRange == None) | (isinstance(dateRange, dict) != True): if (dateRange == None) | (isinstance(dateRange, dict) != True):
res['error']['code'] = '2' dateRange = setDateRange()
res['error']['msg'] = 'dateRange doesn\'t exist or is of wrong type, must be dict'
return res
# Switch statement to select the proper dateRange # Switch statement to select the proper dateRange
sellerList = switchDateRange(sellerList, dateRange) sellerList = switchDateRange(sellerList, dateRange)