def startMainLoop(self, configDict): iteration = 1 while 1==1: useCases = ConfigUseCases() useCases.create(configDict) print("#{} Main HUB loop started".format(iteration)) print("# Config HUB -- {}".format(useCases.toString())) print("# Starting service - sites") servicesApi = MeLiApiService() # if configDict["services"]["sites"] == True: response = servicesApi.sites(useCases) print("# Service HUB -- {}".format(response)) # if configDict["services"]["categories"] == True: response = servicesApi.categories(useCases) print("# Service HUB -- {}".format(response)) # if configDict["services"]["catalog_listing"] == True: print("# Starting service - catalog listing") response = servicesApi.catalogListByQuery(useCases) print("# Service HUB -- {}".format(response)) # if configDict["services"]["csv_file"] == True: print("# Generating CSV file") response = servicesApi.catalogListByQuery(useCases) self.printCsv(response) # Geração de arquivo csv # iteration = iteration + 1 time.sleep(5)
def buildRouteAndGetHttp(self, useCase = ConfigUseCases(), routeApi = "", query = None): response = {} try: endpoint = str(useCase.meliApi.api['endpoint']) site_id = str(useCase.meliApi.api['site_id']) limit = str(useCase.meliApi.consumer['product_limit']) routeApi = routeApi.replace("${ENDPOINT}", endpoint) routeApi = routeApi.replace("${SITE_ID}", site_id) routeApi = routeApi.replace("${LIMIT}", limit) ####################################### # verifica se existe query para ser # usada ####################################### if query!=None and len(query) > 0: routeApi = routeApi.replace("${PRODUCT_DESC}", query) ####################################### # chama ML API remota ####################################### response_api = requests.get(routeApi).text ####################################### # monta o json ####################################### str_message = json.dumps(response, sort_keys=True) response['status'] = 200 response['message'] = str_message response['response_api'] = response_api except: response = {'status':500, 'message': 'Fail request'} print("Error to process URL") return response
def startMainLoop(self, configDict): iteration = 1 while 1==1: useCases = ConfigUseCases() useCases.create(configDict) print("#{} Main HUB loop started".format(iteration)) print("# Config HUB -- {}".format(useCases.toString())) print("# Starting service - sites") servicesApi = MeLiApiService() for service in configDict['services']: if service == "sites": response = servicesApi.sites(useCases) elif service == "categories": response = servicesApi.categories(useCases) elif service == "catalog_listing": response = servicesApi.catalogListByQuery(useCases) self.printCsv(response) #print("# Starting {}".format(service)) #print("# Service HUB -- {}".format(response)) iteration = iteration + 1 time.sleep(5)
def catalogListByQuery(self, useCase = ConfigUseCases()): routeSites = str(useCase.meliApi.api['catalog_listing']) query = "Motorola" response = self.buildRouteAndGetHttp(useCase, routeSites, query) response_api = json.loads(response["response_api"]) results = response_api['results'] products = [] for item in results: price = item['price'] description = r''.join(item['title']).encode('utf-8').strip() product = {} product['price'] = price product['description'] = description products.append(product) return products
def categories(self, useCase = ConfigUseCases()): routeCategories = str(useCase.meliApi.api['categories']) return self.buildRouteAndGetHttp(useCase, routeCategories)
def sites(self, useCase = ConfigUseCases()): routeSites = str(useCase.meliApi.api['sites']) return self.buildRouteAndGetHttp(useCase, routeSites)