|
|
@@ -215,7 +215,7 @@ def extract_title_or_error(product, selected_pt):
|
|
|
val = attr.get("attributeValue")
|
|
|
|
|
|
if desc == "Capacity":
|
|
|
- extracted_data[desc] = f"Capacity {val}"
|
|
|
+ extracted_data[desc] = f"Capacity {val}".replace(" in", "")
|
|
|
elif desc in ["Door Type", "Color"]:
|
|
|
extracted_data[desc] = val
|
|
|
elif desc in ["Width", "Depth", "Height"]:
|
|
|
@@ -295,13 +295,14 @@ def construct_dynamic_title(raw_data,selected_pt):
|
|
|
return "Could not found attribute name on product details page"
|
|
|
|
|
|
@login_required
|
|
|
-def title_creator_view(request):
|
|
|
+async def title_creator_view(request):
|
|
|
if request.method == 'POST' and request.FILES.get('file'):
|
|
|
excel_file = request.FILES['file']
|
|
|
selected_pt = request.POST.get('product_type')
|
|
|
fs = FileSystemStorage()
|
|
|
filename = fs.save(excel_file.name, excel_file)
|
|
|
file_path = fs.path(filename)
|
|
|
+ scraper = cloudscraper.create_scraper()
|
|
|
|
|
|
try:
|
|
|
# 1. Read Excel
|
|
|
@@ -312,11 +313,13 @@ def title_creator_view(request):
|
|
|
df['New_Generated_Title'] = ""
|
|
|
|
|
|
headers = {"User-Agent": "Mozilla/5.0"}
|
|
|
+ dynamic_token = await get_fresh_token(scraper)
|
|
|
results_for_ui = []
|
|
|
# Specific Headers for the Item# API
|
|
|
api_headers = {
|
|
|
"accept": "application/json, text/plain, */*",
|
|
|
- "authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJERVYifQ.uOFB7h7_Aw6jbA1HSqVJ44tKMO7E1ljz1kV_JddeKL64YCOH57-l1ZX2Lly-Jnhdnxk3xMAeW5FawAgymEaMKA",
|
|
|
+ "authorization": f"Bearer {dynamic_token}",
|
|
|
+ # "authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJERVYifQ.uOFB7h7_Aw6jbA1HSqVJ44tKMO7E1ljz1kV_JddeKL64YCOH57-l1ZX2Lly-Jnhdnxk3xMAeW5FawAgymEaMKA",
|
|
|
"client_id": "GEC",
|
|
|
"referer": "https://www.globalindustrial.com/"
|
|
|
}
|
|
|
@@ -391,7 +394,36 @@ def title_creator_view(request):
|
|
|
return render(request, 'title_creator_index.html', {'product_types': product_types})
|
|
|
# return render(request, 'title_creator_index.html')
|
|
|
|
|
|
-def process_excel_task(file_path, selected_pt, task_id):
|
|
|
+async def get_fresh_token(scraper):
|
|
|
+ """Hits the homepage once to extract the latest Bearer token."""
|
|
|
+ base_url = "https://www.globalindustrial.com"
|
|
|
+ try:
|
|
|
+ # One-time hit to the base URL
|
|
|
+ response = scraper.get(base_url, timeout=15)
|
|
|
+
|
|
|
+ # 1. Check Cookies for 'Authorization'
|
|
|
+ token = scraper.cookies.get('Authorization')
|
|
|
+ print("token found in cookies:----- ",token)
|
|
|
+ if token:
|
|
|
+ return token.replace('Bearer ', '').strip()
|
|
|
+
|
|
|
+ # 2. Check __NEXT_DATA__ script in HTML
|
|
|
+ soup = BeautifulSoup(response.content, 'html.parser')
|
|
|
+ script_tag = soup.find('script', id='__NEXT_DATA__')
|
|
|
+ if script_tag:
|
|
|
+ data = json.loads(script_tag.string)
|
|
|
+ # Standard Next.js path for auth tokens
|
|
|
+ token = data.get('props', {}).get('pageProps', {}).get('token')
|
|
|
+ if token:
|
|
|
+ return token
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ print(f"Token retrieval failed: {e}")
|
|
|
+
|
|
|
+ # Fallback to your hardcoded token if extraction fails
|
|
|
+ return "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJERVYifQ.uOFB7h7_Aw6jbA1HSqVJ44tKMO7E1ljz1kV_JddeKL64YCOH57-l1ZX2Lly-Jnhdnxk3xMAeW5FawAgymEaMKA"
|
|
|
+
|
|
|
+async def process_excel_task(file_path, selected_pt, task_id):
|
|
|
# Retrieve the task record from the database
|
|
|
scraper = cloudscraper.create_scraper() # This replaces requests.get
|
|
|
task = ProcessingTask.objects.get(task_id=task_id)
|
|
|
@@ -405,10 +437,11 @@ def process_excel_task(file_path, selected_pt, task_id):
|
|
|
df['New_Generated_Title'] = ""
|
|
|
|
|
|
headers = {"User-Agent": "Mozilla/5.0"}
|
|
|
+ dynamic_token = await get_fresh_token(scraper)
|
|
|
# Specific Headers for the Item# API
|
|
|
api_headers = {
|
|
|
"accept": "application/json, text/plain, */*",
|
|
|
- "authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJERVYifQ.uOFB7h7_Aw6jbA1HSqVJ44tKMO7E1ljz1kV_JddeKL64YCOH57-l1ZX2Lly-Jnhdnxk3xMAeW5FawAgymEaMKA",
|
|
|
+ "authorization": f"Bearer {dynamic_token}",#"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJERVYifQ.uOFB7h7_Aw6jbA1HSqVJ44tKMO7E1ljz1kV_JddeKL64YCOH57-l1ZX2Lly-Jnhdnxk3xMAeW5FawAgymEaMKA",
|
|
|
"client_id": "GEC",
|
|
|
"referer": "https://www.globalindustrial.com/"
|
|
|
}
|