procom / main /views.py
thejagstudio's picture
Upload 121 files
f87b88a
raw
history blame
No virus
7.82 kB
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.decorators import api_view
import json
from .models import Products, Categories
import os
import requests
from django.http import HttpResponse
from django.template import loader
from concurrent.futures import ThreadPoolExecutor
# Create your views here.
@api_view(['GET'])
def getProductData(request, category):
page = request.GET.get('page')
obj = Products.objects.filter(category=Categories.objects.filter(link=category).first()).all()
data = {"data": []}
for i in obj:
temp = {}
temp["name"] = i.name
temp["score"] = i.score
temp["image"] = i.image
temp["propGroups"] = i.propGroupsMini
temp["propScore"] = i.propScore
temp["link"] = i.link
data["data"].append(temp)
data["data"] = sorted(data["data"], key=lambda k: k['score'] if k['score'] is not None else 0, reverse=True)
if page != None:
start = (int(page)-1)*50
end = int(page)*50
data["data"] = data["data"][start:end]
else:
data["data"] = data["data"][:50]
if data["data"] == []:
return Response({"msg": "No Data Found"})
return Response(data)
@api_view(['GET'])
def searchQuery(request):
query = request.GET.get('query')
obj = Products.objects.filter(name__contains=query).all()
data = {"data": []}
for i in obj:
temp = {}
temp["name"] = i.name
temp["score"] = i.score
temp["image"] = i.image
temp["propGroups"] = i.propGroupsMini
temp["propScore"] = i.propScore
temp["link"] = i.link
data["data"].append(temp)
return Response(data)
@api_view(['GET'])
def fetchCategory(request):
obj = Categories.objects.all()
data = {"data": []}
for i in obj:
temp = {}
temp["name"] = i.name
temp["link"] = i.link
data["data"].append(temp)
return Response(data)
@api_view(['GET'])
def comparisionData(request):
try:
compare = request.GET.get('compare')
url = "https://versus.com/api/store/en/"+compare+"?ts=9980526090123&userId=&type=json"
response = requests.request("GET", url)
return HttpResponse(json.dumps(response.json()), content_type="application/json")
except:
return Response({"error": "No Data Found"})
@api_view(['GET'])
def fetchPriceData(request):
try:
compare = request.GET.get('compare')
url = "https://versus.com/api/prices/"+compare+"/IN?type=json"
headers = {
'Accept': 'application/json'
}
response = requests.request("GET", url, headers=headers)
return HttpResponse(json.dumps(response.json()), content_type="application/json")
except:
return Response({"error": "No Data Found"})
# path = "./data/"
# dir_list = os.listdir(path)
# print(dir_list)
# for path in dir_list:
# f = open('./data/'+path, "r")
# data = json.loads(f.read())
# for i in data["data"]:
# try:
# obj = Products(name=i["name"], score=i["score"], image=i["image"],
# propGroups=i["propGroups"], propScore=i["propScore"], category=path.split(".json")[0].replace("-", " "))
# obj.save()
# except:
# pass
# print(path)
# path = "./data/"
# dir_list = os.listdir(path)
# print(dir_list)
# for path in dir_list:
# obj = Categories(name=path.split(".json")[0].replace("-", " ").title())
# obj.save()
# print(path)
def getComparisonChart(request):
compare = request.GET.get('compare')
url = "https://versus.com/share/summary/en/"+compare
response = requests.get(url)
template ="<!doctype html><html lang='en' data-reactroot=''><head><link href='/static/chart/chartStyle.css' media='all' rel='stylesheet' type='text/css' /><script type='module' src='/static/chart/chartScript1.mjs'></script><script type='module' src='/static/chart/chartScript2.mjs'></script><script type='module' src='/static/chart/chartScript3.mjs'></script></head><body style='margin:0;min-height:100%;position:relative;font-family:-apple-system,BlinkMacSystemFont,Roboto,Oxygen,Ubuntu,Cantarell,sans-serif'><div id='root'></div><script id='reactInitData'>window.__data = '%%INITIAL_DATA%%';</script><script type='text/javascript'>(function() {var check = document.createElement('script');if (!('noModule' in check) && 'onbeforeload' in check) {var support = false;document.addEventListener('beforeload', function(e) {if (e.target === check) {support = true;} else if (!e.target.hasAttribute('nomodule') || !support) {return;}e.preventDefault();}, true);check.type = 'module';check.src = '.';document.head.appendChild(check);check.remove();}}());</script></body></html>"
data = (response.text)
data = data.split("<script id=\"reactInitData\">window.__data=")[1].split("</script>")[0]
template = template.replace("'%%INITIAL_DATA%%'", data)
return HttpResponse(template)
def dataManipulator(request):
# list all files from ./data/
path = "./data/"
dir_list = os.listdir(path)
categorys = Categories.objects.all()
count = 0
for i in dir_list:
products = Products.objects.filter(category=categorys[count]).all()
count += 1
data = json.loads(open(path+i, "r").read())
for product in products:
for j in data["data"]:
if j["name"] == product.name:
print(product.name)
product.propGroupsMini = j["propGroups"]
Products.objects.bulk_update(products, ["propGroupsMini"])
return HttpResponse("Done")
def threadFunc(product,count):
if product.terms == {}:
try:
url = "https://versus.com/api/store/en/"+product.link+"?ts=9980526090123&userId=&type=json"
response = requests.request("GET", url)
data = response.json()
product.terms = data["terms"]
product.suggestions = data["searchOverlay"]["suggestions"]
product.tldr = data["comparison"]["tldr"][0]
product.propGroups = data["comparison"]["propGroups"]
product.notApplicableProps = data["comparison"]["notApplicableProps"]
product.cheapAlternatives = data["comparison"]["cheapAlternatives"]
product.topProps = data["comparison"]["topProps"]
product.popularCompare = data["comparison"]["popular"]
product.toplist = data["comparison"]["toplist"]
print(count," : ",product.name)
except:
print("Error : ",product.name)
else:
print("Already Done : ",product.name)
import threading
def dataAdder(request):
products = Products.objects.order_by("name").all()
count = 0
for i in range(0,len(products),5):
count += 1
t1 = threading.Thread(target=threadFunc, args=(products[i],count))
count += 1
t2 = threading.Thread(target=threadFunc, args=(products[i+1],count))
count += 1
t3 = threading.Thread(target=threadFunc, args=(products[i+2],count))
count += 1
t4 = threading.Thread(target=threadFunc, args=(products[i+3],count))
count += 1
t5 = threading.Thread(target=threadFunc, args=(products[i+4],count))
t1.start()
t2.start()
t3.start()
t4.start()
t5.start()
t1.join()
t2.join()
t3.join()
t4.join()
t5.join()
if count > 14400:
products[i].save()
products[i+1].save()
products[i+2].save()
products[i+3].save()
products[i+4].save()
return HttpResponse("Done")