Spaces:
Running
Running
| from django.shortcuts import render | |
| from rest_framework.response import Response | |
| from rest_framework.decorators import api_view | |
| import http.client | |
| import json | |
| import requests | |
| from bs4 import BeautifulSoup | |
| # Create your views here. | |
| def sessionIdGenrator(): | |
| conn = http.client.HTTPSConnection("www.amazon.in") | |
| payload = '' | |
| headers = {} | |
| conn.request("GET", "/", payload, headers) | |
| res = conn.getresponse() | |
| data = res.read() | |
| response = data.decode("utf-8") | |
| ue_sid = response.split("ue_sid =")[1].split(',')[0].split("'")[1] | |
| ue_mid = response.split("ue_mid =")[1].split(',')[0].split("'")[1] | |
| return ue_sid, ue_mid | |
| def searchAPI(ue_sid, ue_mid, query): | |
| conn = http.client.HTTPSConnection("completion.amazon.in") | |
| payload = '' | |
| headers = {} | |
| conn.request("GET", "/api/2017/suggestions?prefix="+query.replace(" ", "+") + "&alias=aps&session-id="+ue_sid+"&mid="+ue_mid, payload, headers) | |
| res = conn.getresponse() | |
| data = res.read() | |
| response = data.decode("utf-8") | |
| return json.loads(response) | |
| def getAllProduct(query, page): | |
| url = "https://api.croma.com/searchservices/v1/search?currentPage="+str(page)+"&query="+query+"%3Arelevance&fields=FULL&channel=WEB&channelCode=382006&spellOpt=DEFAULT" | |
| print(url) | |
| response = requests.request("GET", url) | |
| products = response.json()["products"] | |
| data = [] | |
| for product in products: | |
| temp = {} | |
| temp["title"] = product["name"] | |
| temp["link"] = "https://www.croma.com"+product["url"] | |
| try: | |
| temp["price"] = product["price"]["value"] | |
| except: | |
| pass | |
| try: | |
| temp["fullPrice"] = product["mrp"]["value"] | |
| except: | |
| pass | |
| try: | |
| temp["symbol"] = product["price"]["formattedValue"][0] | |
| except: | |
| pass | |
| try: | |
| temp["image"] = product["plpImage"] | |
| except: | |
| pass | |
| try: | |
| temp["stars"] = product["averageRating"] | |
| except: | |
| pass | |
| try: | |
| temp["reviews"] = product["finalReviewRatingCount"] | |
| except: | |
| pass | |
| try: | |
| temp["discount"] = product["discountValue"] | |
| except: | |
| pass | |
| data.append(temp) | |
| return data | |
| def getProductsList(request): | |
| query = (request.GET.get('query')) | |
| try: | |
| page = (request.GET.get('page')) | |
| except: | |
| page = 0 | |
| if page == None: | |
| page = 0 | |
| data = getAllProduct(query, page) | |
| return Response({"data": data}) | |
| def getProductDetail(request): | |
| productId = request.GET.get('id') | |
| conn = http.client.HTTPSConnection("www.amazon.in") | |
| payload = '' | |
| headers = {} | |
| conn.request("GET", "/dp/"+productId+"/", payload, headers) | |
| res = conn.getresponse() | |
| data = res.read() | |
| response = data.decode("utf-8") | |
| data = {} | |
| soup = BeautifulSoup(response, features="html5lib") | |
| #title = response.split('id="productTitle"')[1].split(">")[1].split("</span")[0].strip() | |
| title = soup.find_all("span", {"class", "a-size-large product-title-word-break"})[0].text.strip() | |
| data['title'] = title | |
| symbol = soup.find_all("span", {"class", "a-price-symbol"})[0].text | |
| data["symbol"] = symbol | |
| savingsPercentage = soup.find_all("span", {"class", "savingsPercentage"})[0].text | |
| data["savingPercentage"] = savingsPercentage | |
| imgs = soup.find_all("img", {"class", "a-dynamic-image"}) | |
| imgArr = [] | |
| for i in imgs: | |
| imgArr.append("https://m.media-amazon.com/images/I/" +i["src"].split("/I/")[1].split(".")[0]+".jpg") | |
| data["images"] = imgArr | |
| scripts = soup.find_all("script") | |
| tempData = {} | |
| for i in scripts: | |
| try: | |
| temp = str(i).split("<script")[1].split(">")[1].split("</script")[0] | |
| for key, item in json.loads(temp).items(): | |
| if item != None or item != "nulll" or item != True or item != False: | |
| tempData[key] = item | |
| except: | |
| pass | |
| data["currencyCode"] = tempData["currencyCode"] | |
| data["productPrice"] = tempData["productPrice"] | |
| data["brand"] = tempData["brand"] | |
| data["category"] = tempData["buyBackCategory"] | |
| return Response({"data": data}) | |
| def searchQuery(request): | |
| query = request.GET.get('query') | |
| url = "https://api.croma.com/searchservices/v1/autocomplete?term="+query+"&fields=FULL" | |
| response = requests.request("GET", url) | |
| data = [] | |
| for i in response.json(): | |
| data.append(i["suggestedWord"]) | |
| data = {"data": data} | |
| return Response(data) | |