Spaces:
Sleeping
Sleeping
File size: 6,208 Bytes
0bd6e09 f87b88a 0bd6e09 f87b88a 0bd6e09 f87b88a 0bd6e09 f87b88a 0bd6e09 f87b88a 0bd6e09 f87b88a 0bd6e09 f87b88a 0bd6e09 f87b88a 0bd6e09 f87b88a 0bd6e09 f87b88a 0bd6e09 f87b88a 0bd6e09 f87b88a 0bd6e09 f87b88a 0bd6e09 f87b88a 0bd6e09 f87b88a 0bd6e09 f87b88a 0bd6e09 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
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):
conn = http.client.HTTPSConnection("www.amazon.in")
payload = ''
headers = {}
conn.request("POST", "/s/query?k="+query + "&page="+str(page), payload, headers)
res = conn.getresponse()
data = res.read()
response = data.decode("utf-8")
tempData = response.split("&&&")[3:-1]
data = []
for i in tempData:
try:
tempProduct = {}
tempHtml = json.loads(i)[2]["html"]
soup = BeautifulSoup(tempHtml, features="html5lib")
try:
stars = soup.find_all("span", {"class", "a-icon-alt"})[0].text
tempProduct["stars"] = stars
except:
pass
try:
imgs = soup.find_all("img", {"class", "s-image"})[0]['srcset']
tempProduct["imgs"] = imgs
except:
pass
try:
title = soup.find_all("img", {"class", "s-image"})[0]['alt'].replace("Sponsored Ad - ", "")
tempProduct["title"] = title
except:
pass
try:
link = soup.find_all("a")[0]["href"]
tempProduct["link"] = link
except:
pass
try:
symbol = soup.find_all("span", {"class", "a-price-symbol"})[0].text
tempProduct["symbol"] = symbol
except:
pass
try:
price = soup.find_all("span", {"class", "a-price-whole"})[0].text
tempProduct["price"] = price
except:
pass
try:
fullPrice = soup.find_all("span", {"class", "a-offscreen"})[1].text
tempProduct["fullPrice"] = fullPrice[1:]
except:
pass
try:
offer = soup.find_all("span", {"class", "a-truncate-full"})[0].text
tempProduct["offer"] = offer
except:
pass
try:
dataAsin = json.loads(i)[2]["asin"]
tempProduct["asin"] = dataAsin
except:
pass
if(tempProduct != {} and "title" in tempProduct.keys() and "asin" in tempProduct.keys()):
data.append(tempProduct)
except:
pass
return data
@api_view(['GET'])
def getProductsList(request):
query = (request.GET.get('query')).replace(" ", "+")
try:
page = (request.GET.get('page'))
except:
page = 1
if page == None:
page = 1
data = getAllProduct(query, page)
return Response({"data": data})
@api_view(['GET'])
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})
@api_view(['GET'])
def searchQuery(request):
query = request.GET.get('query')
if not ('ue_sid' in request.session and 'ue_mid' in request.session):
ue_sid, ue_mid = sessionIdGenrator()
request.session['ue_sid'] = ue_sid
request.session['ue_mid'] = ue_mid
else:
ue_sid = request.session['ue_sid']
ue_mid = request.session['ue_mid']
try:
tempData = searchAPI(ue_sid, ue_mid, query)
except:
ue_sid, ue_mid = sessionIdGenrator()
request.session['ue_sid'] = ue_sid
request.session['ue_mid'] = ue_mid
tempData = searchAPI(ue_sid, ue_mid, query)
values = []
for i in tempData["suggestions"]:
values.append(i["value"])
data = {"data": values}
return Response(data)
|