def GetItem(item_id, outputSelector=None, detailLevel = None, encoding="JSON"): #get the user auth token token = get_config_store().get("auth", "token") root = etree.Element("GetItemRequest", xmlns="urn:ebay:apis:eBLBaseComponents") #add it to the xml doc credentials_elem = etree.SubElement(root, "RequesterCredentials") token_elem = etree.SubElement(credentials_elem, "eBayAuthToken") token_elem.text = token itemID_elem = etree.SubElement(root, "ItemID") itemID_elem.text = str(item_id) if outputSelector: outputSelector_elem = etree.SubElement(root, "OutputSelector") outputSelector_elem.text = item if detailLevel: detailLevel_elem = etree.SubElement(root, "DetailLevel") detailLevel_elem.text = detailLevel #need to specify xml declaration and encoding or else will get error request = etree.tostring(root, pretty_print=False, xml_declaration=True, encoding="utf-8") response = get_response("GetItem", request, encoding) return response #NOTE: it returns an xml reposnse, not a dict
def search_items(cls, search_term=None, category=None, buyitnow=None, condition=None, max_price=None, Zip=None): logging.info(str(locals())) set_config_file(settings.EBAY_API_SETTINGS_FILE) if not search_term: return [] if not Zip: config = get_config_store() Zip = config.get("settings", "zip") logging.info(Zip) itemFilter = [] if max_price: itemFilter = [{'name': 'MaxPrice', 'value': str(float(max_price)), 'paramName': 'Currency', 'paramValue': 'USD'}] if condition: itemFilter.append({'name': 'Condition', 'value': condition}) outputSelector = ('PictureURLSuperSize', 'SellerInfo') json_str = findItemsByKeywords(keywords=search_term, buyerPostalCode=Zip, itemFilter=itemFilter, outputSelector=outputSelector, aspectFilter=[], domainFilter=[]) # logging.info(json_str) data = json.loads(json_str) items_count = int(cls.item_attr(data, ['findItemsByKeywordsResponse', 0, 'searchResult', 0, '@count'], '0')) if items_count == 0: return [] items = cls.item_attr(data, ['findItemsByKeywordsResponse', 0, 'searchResult', 0, 'item'], []) data_items = [] for item in items: try: processed_item = cls.process_item(item, search_term, category, buyitnow, max_price) except EbayItemError: processed_item = None if processed_item: data_items.append(processed_item) return data_items
def get_response(operation_name, data, encoding, **headers): config = get_config_store() globalId = config.get("call", "global_id") app_name = config.get("keys", "app_name") endpoint = config.get("endpoints", "finding") http_headers = { "X-EBAY-SOA-OPERATION-NAME": operation_name, "X-EBAY-SOA-SECURITY-APPNAME": app_name, "X-EBAY-SOA-GLOBAL-ID": globalId, "X-EBAY-SOA-RESPONSE-DATA-FORMAT": encoding} http_headers.update(headers) req = urllib2.Request(endpoint, data, http_headers) res = urllib2.urlopen(req) data = res.read() return data
# Example from Programming Collective Intelligence, Chapter 8 import httplib from xml.dom.minidom import parseString from ebay.utils import set_config_file, get_config_store # pip install python-ebay from ebay.finding import findItemsByKeywords import numpredict # Setting eBay configuration values, as necessary by python-ebay module, and reading them from the config file set_config_file("ebay.apikey") configStore = get_config_store() devKey = configStore.get("keys", "dev_name") appKey = configStore.get("keys", "app_name") certKey = configStore.get("keys", "cert_name") userToken = configStore.get("auth", "token") def getHeaders(apicall, siteID="0", compatibilityLevel="433"): headers = { "X-EBAY-API-COMPATIBILITY-LEVEL": compatibilityLevel, "X-EBAY-API-DEV-NAME": devKey, "X-EBAY-API-APPr-NAME": appKey, "X-EBAY-API-CERT-NAME": certKey, "X-EBAY-API-CALL-NAME": apicall, "X-EBAY-API-SITEID": siteID, "Content-Type": "text/xml", } return headers