class Base(object): #Defining api key in constructor def __init__(self): self.products = Products(api_key="xx", api_secret="xx") #Getting product results from constructor def get_result_example(self): self.products.products_field("search", "Samsung Galaxy") results = self.products.get_products() return results def user_selection(self): print("1. Try Sample result for api") print("2. Get results in a csv file") print("3. Get results in a file(JSON Pretty)") print("4. Get results in a file(Normal JSON)") self.usr_input = input("Enter any choice") #return self.usr_input def switch_selection(self): if self.usr_input == 1: print b.get_result_example() elif self.usr_input == 2: print b.get_result_example() elif self.usr_input == 3: print b.get_result_example() elif self.usr_input == 3: print b.get_result_example() else: print "Please enter a number from 1 to 4"
def product_results(request, current_search): #variable setups products = Products( api_key = "SEM34D405CE2A6F4715E79457D08A21B4CEE", api_secret = "NmI0ZDkwZmJhMGVkNGU1NWI5Y2ZmYWNkMjgzNzUyZDg" ) current_user = User.objects.get(username=request.user) form = ProductSearchForm(initial={'Search': current_search}) #get user's last search and feed to Semantics3, render results query = products.products_field("name", current_search) response = products.get_products() results = response["results"] #get search query, add to database and session, redirect if request.method == 'GET': form = ProductSearchForm(request.GET) if form.is_valid(): this_search = form.cleaned_data['search'] request.session["search"] = this_search current_search = Search(query=this_search, user=current_user.id) current_search.save() return HttpResponseRedirect('/shopping/results/'+this_search) CONTEXT = {'results': results, 'search': current_search, 'form': form} return render(request, 'product_search/shopping_results.html', CONTEXT)
def make_query(item_name): products = Products( api_key='SEM3A92F1C506BB40F217AA3716D7C9A8815', api_secret='ZGU0MWJiNjY1ZTY1OTgxZDVhODFiYWNkYzNkOTBjZjA') products.products_field('name', item_name) result_query = products.get_products() code = result_query['code'] result_list = result_query['results'] result_count = result_query['results_count'] return code, result_list, result_count
def getData(self): # Set up a client to talk to the Semantics3 API using your Semantics3 API Credentials sem3 = Products(api_key=self.apikey, api_secret=self.apisecret) sem3.products_field("site", self.site) sem3.products_field("sitedetails_display", ("include", [self.site])) sem3.products_field("offset", self.offset) sem3.products_field("limit", self.limit) results = sem3.get_products() ds = json.dumps(results) self.writeData(ds) return ds
def getData(self): # Set up a client to talk to the Semantics3 API using your Semantics3 API Credentials sem3 = Products(api_key = self.apikey, api_secret = self.apisecret) sem3.products_field("site", self.site) sem3.products_field("sitedetails_display", ("include", [self.site])) sem3.products_field("offset", self.offset) sem3.products_field("limit", self.limit) results = sem3.get_products() ds = json.dumps(results) self.writeData(ds) return ds
def make_query(item_name): products = Products( api_key = 'SEM3A92F1C506BB40F217AA3716D7C9A8815', api_secret = 'ZGU0MWJiNjY1ZTY1OTgxZDVhODFiYWNkYzNkOTBjZjA') products.products_field('name', item_name) result_query = products.get_products() code = result_query['code'] result_list = result_query['results'] result_count = result_query['results_count'] return code, result_list, result_count
def import_shoes(apps, schema_editor): # We can't import the Person model directly as it may be a newer # version than this migration expects. We use the historical version. Shoe = apps.get_model("product", "Product") products = Products(api_key = "SEM383288C8F0304F7BB107E2E110B0420D3",api_secret = "MGYwZjgxZTVkOTVmZmY5OTEwYzlkMWI4MDAxMzk3YjA") products.products_field( "name", "Shoes" ) products.products_field( "limit", 100 ) results = products.get_products(); resultarr = results['results']; count = 1; for i,var in enumerate(resultarr): s = Shoe(id = count, name= var['name'], price= Decimal(var['price']), stock= 1, description= var['category']); s.save() count=count+1;
def user_profile(request): sem3 = Products( api_key = "SEM3CCB4BBBB383C73986C4B27B9BE4B3088", api_secret = "YmJjY2M1YzFlMGM0ZTg1OTdlNDFkYmY5MmRmZTg2ZDk" ) if request.method == 'POST': form = SearchForm(request.POST) if form.is_valid(): form.save() searchInput = form.cleaned_data.get('input') sem3.products_field("search", searchInput) results = sem3.get_products() return render(request, 'user_profile.html', results) else: return render(request, 'user_profile.html')
def isUpcExist(code): product = Products(SEMANTIC_PUBLIC, SEMANTIC_SECRET) product.products_field("upc", str(code)) results = product.get_products() # a = ['CD','DVD', 'CD()'] # yes = results['results'][0]['format'] # count = 0 # for x in range(1,len(a)): # if(yes == a[x]): # print('{} == {}'.format(a[:],yes)) # count += 1 # else: # print('Error') if results['results_count'] == 1: return results, True else: print("not exist") return results, False
def import_shoes(apps, schema_editor): # We can't import the Person model directly as it may be a newer # version than this migration expects. We use the historical version. Shoe = apps.get_model("product", "Product") products = Products( api_key="SEM383288C8F0304F7BB107E2E110B0420D3", api_secret="MGYwZjgxZTVkOTVmZmY5OTEwYzlkMWI4MDAxMzk3YjA") products.products_field("name", "Shoes") products.products_field("limit", 100) results = products.get_products() resultarr = results['results'] count = 1 for i, var in enumerate(resultarr): s = Shoe(id=count, name=var['name'], price=Decimal(var['price']), stock=1, description=var['category']) s.save() count = count + 1
def post(self, request, *args, **kwargs): product_serializer = ProductSerializer(data=request.data) if product_serializer.is_valid(): product_serializer.save() upc = product_serializer.data['upc'] ean = product_serializer.data['ean'] sem3 = Products( api_key="SEM358802643C8595EA80A4BA5F74DB35FD9", api_secret="ZDhkMDJiNmEzMzdlMzVmZmQwNmNmZGVhODE4ZjQ4ZGQ") if ean or len(ean) > 5: print("EAN in") sem3.products_field("ean", ean) elif upc or len(upc) > 5: print("UPC in") sem3.products_field("upc", upc) sem3.products_field("fields", ["name", "gtins"]) p = "product not found" # Run the request try: results = sem3.get_products() print(results) p = results['results'][0]['name'] except: print('An error occured.') # View the results of the request print(p) return Response({ "upc": product_serializer.data, "result": p }, status=status.HTTP_201_CREATED) else: return Response(product_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class SemanticsPuller(object): def __init__(self): self.api_key = 'SEM30A3983D93A26181838BA0FA228979B01' self.api_secret = 'OWFiNmE3YTU2NDljNDliNDU4ZDczZTk1ZmU3MTgzMDE' self.wrapper = Products(api_key=self.api_key, api_secret=self.api_secret) def get_product(self, upc): self.wrapper.products_field("upc", upc) resp = self.wrapper.get_products() if resp['results']: results = resp['results'][0] filtered_results = { k: v for k, v in results.items() if k in ['name', 'upc', 'images'] } filtered_results['images'] = filtered_results['images'][0] return filtered_results else: # raise Exception('Could not find the product') return {'upc': upc, 'images': None, 'name': None, 'quantity': 0}
from semantics3 import Products sem3 = Products(api_key="SEM310D8563D0E07BA3AC280829F8076605D", api_secret="ZTliZjI1NTg5ZDQ2ZjdhNDMyNmRmMTc4Mjg3NjJmN2M") product_list = ['laptop', 'phone'] results = [] all_products = [] for product in product_list: sem3.products_field("search", product) results = sem3.get_products() if product == 'laptop': for i, p in enumerate(results['results']): title = results['results'][i]['name'] price = results['results'][i]['price'] if 'color' in results['results'][i].keys(): color = results['results'][i]['color'] else: color = None image = results['results'][i]['images'] url = results['results'][i]['sitedetails'][0]['url'] product_data = { 'product': 'Laptop', 'title': title, 'price': price, 'color': color, 'image': image, 'url': url, } if product == 'phone':
def product_results( request, current_search, template="shopping/shopping_results.html", page_template="shopping/products.html" ): # variable setups products = Products( api_key="SEM34D405CE2A6F4715E79457D08A21B4CEE", api_secret="NmI0ZDkwZmJhMGVkNGU1NWI5Y2ZmYWNkMjgzNzUyZDg" ) current_user = User.objects.get(username=request.user) form = ProductSearchForm # get user's last search and feed to Semantics3, render results query = products.products_field("name", current_search) products.get_products() # iterate over products, add to results list (position 0 in results) results = [] for i in products.iter(): results.append(i) if len(results) == 10: break # get lowest prices for each item, add to separate list (position 1 in results) lowest_prices = [] for i in results: prices = [] for store in i["sitedetails"]: prices.append("%.02f" % float(store["latestoffers"][0]["price"])) prices = sorted(prices) lowest_prices.append(prices[0]) results_master = zip(results, lowest_prices) # Django Endless Pagination (Twitter Style Reload) if request.is_ajax(): request_page = utils.get_page_number_from_request(request) offset = (request_page - 1) * 10 products.products_field("offset", offset) products.get_products() # iterate over products, apply an offset based on page number, add to results list (position 0 in results_master) results = [] for i in products.iter(): results.append(i) if len(results) == 10 + offset: break # get lowest prices for each item, add to separate list (position 1 in results_master) lowest_prices = [] for i in results: prices = [] for store in i["sitedetails"]: prices.append("%.02f" % float(store["latestoffers"][0]["price"])) prices = sorted(prices) lowest_prices.append(prices[0]) results_master = zip(results, lowest_prices) template = page_template # get search query, add to database and session, redirect if request.method == "GET": form = ProductSearchForm(request.GET) if form.is_valid(): this_search = form.cleaned_data["search"] current_search = Search(query=this_search, user=current_user) current_search.save() return HttpResponseRedirect("/shopping/results/" + this_search) if request.method == "POST": return HttpResponseRedirect("/overview/") CONTEXT = { "results": results_master, "search": current_search, "form": form, "page_template": page_template, "lowest_prices": lowest_prices, } return render(request, template, CONTEXT)
from semantics3 import Products sem3 = Products( api_key = "SEM3188CBEA0C536322303E7C24718D47A3B", api_secret = "ZDI1NWU4NTQ3M2M4MjJiZDdlMWNlNDBmMTM0MTQ4OGU" ) userInputProduct = input("What product would you like to know about: ") sem3.products_field("search", userInputProduct) results = sem3.get_products() print(results)